Question:
help me debug java program?
sulav.aryal
2007-06-11 22:37:30 UTC
import java.io.*;
class CopyCharacters
{
public static void main(String args[]) throws IOException
{
File infile =new File("a");
File outfile=new File("b");
FileReader fr=null;
FileWriter fw=null;
try
{
fr=new FileReader(infile);
fw=new FileWriter(outfile);
int ch;
while((ch=fr.read())!=-1)
{
fw.write(ch);
System.out.println((char)ch);
}
}
catch(IOException e)
{
System.out.println(e);
}
finally
{
try
{
fr.close();
fw.close();
}
catch(IOException e)
{}
}
}
}
/*//error :- java.io.FileNotFoundException:in.txt( The system cannot find the file specified)
Exception in thread "main" java.lang.NullPointerException at CopyBytes.main(CopyBytes.java:29) */
Three answers:
Naina
2007-06-15 14:26:22 UTC
Instead of giving



File infile =new File("a");

File outfile=new File("b");



Give the location of file ,i.e complete directory name..

File infile =new File("c:/a.txt");

File outfile=new File("c:/b.txt");



It really working I tested it...Hmmm
anonymous
2007-06-11 22:45:01 UTC
Your file isn't in the right directory. The file needs to be in the directory that you are executing the file from.

Also its a bad idea to refer to a FileInputStream or an FileOutputStream before while in a try block that catches a FileNotFoundException.





java.lang.NullPointerException is becase new FileInputStream("in.txt"); returns null since it can't find the file.



good luck.
Zeus
2007-06-12 19:19:03 UTC
As the error message told you, it cant find you file ("a" file).



You may put the full path of your file, example:



File testFile = new File ("/home/test/filename.txt"); // in linux

or



File testFile = new File("c:\somepath\filename.txt"); // in windows



Note: Same case for both infile and outfile.



:D


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...