• Post Reply Bookmark Topic Watch Topic
  • New Topic

File Not Found Exception  RSS feed

 
RoshaniG Gopal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am facing this FileNotFoundException. I have the foll structure:
src
---copy1
----------Copybuild.xml
----------Hello.java
===========
Hello.java
public void copyLocation()
{
try{
File file2 =new File("c:\\plugintest\\copyLocation.txt");
file2.createNewFile();
File fromFile = new File(".\\Copybuild.xml");
File toFile = new File("c:\\plugintest\\hooray.xml");
FileInputStream from = null;
FileOutputStream to = null;

from = new FileInputStream(fromFile);
to = new FileOutputStream(toFile);
byte[] buffer = new byte[4096];
int bytesRead;
File file24 =new File("c:\\plugintest\\After_To_From.txt");
file24.createNewFile();

while ((bytesRead = from.read(buffer)) != -1)
to.write(buffer, 0, bytesRead);// write
File file22 =new File("c:\\plugintest\\After_Reading.txt");
file22.createNewFile();

}
catch(Exception e)
{
e.printStackTrace();
try{
BufferedWriter outfile = new BufferedWriter(new FileWriter("C:\\plugintest\\Exception.txt"));
outfile.write("Exception");
outfile.write(e.toString());
outfile.flush();
outfile.close();
}
catch(Exception ez)
{

}

}

====================
File fromFile = new File(".\\Copybuild.xml");
File fromFile = new File("\\copy1\\Copybuild.xml");
File fromFile = new File("\\Copybuild.xml");
File fromFile = new File("Copybuild.xml");
I have tried all formats, but it gives the same error. Pllease if you can help me put this file in some place where it can be read. Thanks in advance.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you give a relative path, it must be relative to the current working directory (CWD)of the process. In general, the CWD depends on how you launched the program. If you're working inan IDE, then you may be surprised to find out what the CWD actually is.

Run a little sample program which execucutes a line like this:

System.out.println(new File().getAbsolutePath());

and it will tell you the CWD.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should that match System.getProperty("user.dir") ?? I just switched from using the File trick to using the property in a small utility. Both seemed to get tehe same answer.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should be the same, yes. Assuming EFH meant new File("") rather than new File(). The File API describes this is the behavior "by default", but doesn't give any info on how it can be changed. Of course one could extend File to do something else, but that doesn't really change the behavior of File itself. And the behavior is all in constructors anyway, which can't be overridden in the same sense that methods can. I suppose the behavior may be system.dependent, in which case some JVM implementations could just ignore user.dir. But I don't see the point of that.
 
RoshaniG Gopal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest, Jim,
Your inputs were great! Actually I am doing plugin development, I am doing some custom installtion of a feature upon update of eclipse. (i am answering this for the IDE question on the working directory)

I had the same piece of code working for me. I dont see a reason why it should not work .
PS: The same code works if i keep files in C:\ .. Please help.

I want to copy a file from the exisiting directory to an absolute path on the system. Where should I place the file?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!