aspose file tools*
The moose likes I/O and Streams and the fly likes File Not Found Exception in IO Stream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "File Not Found Exception in IO Stream" Watch "File Not Found Exception in IO Stream" New topic
Author

File Not Found Exception in IO Stream

Sha Zay Rain
Greenhorn

Joined: May 04, 2012
Posts: 1
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();
}

}

}

}

Dhaval Vithalani
Greenhorn

Joined: Apr 08, 2012
Posts: 10

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

Joined: Apr 08, 2012
Posts: 10

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.");
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Not Found Exception in IO Stream