• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File Not Found Exception in IO Stream

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

I am doing a very simple IO Stream program from Sun Java Tutorial Website. All of the codes go fine but the problem is got a File not found Exception. I have no idea about that. By the way, I am using Indigo Eclipse in Mac OS as may be this can also be the problem. The following is your reference. Please kindly see my codes and give me some valuable suggestions. Thanks and Regards.

Sha Zay
package basicIO;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyBytes {

public static void main(String[] args) throws IOException {

FileInputStream in = null;
FileOutputStream out = null;

try {

in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);

}
} catch (Exception e) {
e.printStackTrace();
System.out.println("File Cannot be found!!!");
} finally {
if (in != null) {
in.close();
}

if (out != null) {
out.close();
}

}

}

}

 
Greenhorn
Posts: 10
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sha Zay Rain wrote:Dear All,

I am doing a very simple IO Stream program from Sun Java Tutorial Website. All of the codes go fine but the problem is got a File not found Exception. I have no idea about that. By the way, I am using Indigo Eclipse in Mac OS as may be this can also be the problem. The following is your reference. Please kindly see my codes and give me some valuable suggestions. Thanks and Regards.

Sha Zay
package basicIO;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyBytes {

public static void main(String[] args) throws IOException {

FileInputStream in = null;
FileOutputStream out = null;

try {

in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);

}
} catch (Exception e) {
e.printStackTrace();
System.out.println("File Cannot be found!!!");
} finally {
if (in != null) {
in.close();
}

if (out != null) {
out.close();
}

}

}

}



i don't think so their is any problem in your code but still am put some file example for you.. refer that if.. i hope it is helpful to u...
 
Dhaval Vithalani
Greenhorn
Posts: 10
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dhaval Vithalani wrote:

Sha Zay Rain wrote:Dear All,

I am doing a very simple IO Stream program from Sun Java Tutorial Website. All of the codes go fine but the problem is got a File not found Exception. I have no idea about that. By the way, I am using Indigo Eclipse in Mac OS as may be this can also be the problem. The following is your reference. Please kindly see my codes and give me some valuable suggestions. Thanks and Regards.

Sha Zay
package basicIO;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyBytes {

public static void main(String[] args) throws IOException {

FileInputStream in = null;
FileOutputStream out = null;

try {

in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);

}
} catch (Exception e) {
e.printStackTrace();
System.out.println("File Cannot be found!!!");
} finally {
if (in != null) {
in.close();
}

if (out != null) {
out.close();
}

}

}

}



i don't think so their is any problem in your code but still am put some file example for you.. refer that if.. i hope it is helpful to u...



(1) Create A new file

import java.io.*;

public class CreateFile1{
public static void main(String[] args) throws IOException{
File f;
f=new File("myfile.txt");
if(!f.exists()){
f.createNewFile();
System.out.println("New file \"myfile.txt\" has been created
to the current directory");
}
}
}


(2) Construction a file name path

import java.io.*;

public class PathFile{
public static void main(String[] args) throws IOException{
File f;
f=new File("example" + File.separator + "myfile.txt");
f.createNewFile();
System.out.println("New file \"myfile.txt\"

has been created
to the specified location");
System.out.println("The absolute path of the file is: "
+f.getAbsolutePath());
}
}


(3) Read the file

import java.io.*;
class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("textfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}


(4) Write the file


import java.io.*;

public class FileWriter{

public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter the file name to create : ");
String file_name = in.readLine();
File file = new File(file_name);
boolean exist = file.createNewFile();
if (!exist)
{
System.out.println("File already exists.");
System.exit(0);
}
else
{
FileWriter fstream = new FileWriter(file_name);
BufferedWriter out = new BufferedWriter(fstream);
out.write(in.readLine());
out.close();
System.out.println("File created successfully.");
}
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic