I am working on a simple web application in NetBeans where I am getting FileNotFoundException. I have stored files in class path, so I need to use relative paths. When I tried with absolute path, it worked fine for me.
Below image shows my file system hierarchy.
I need to write content data in file DBList.txt.
My code is:
File file = new File("data/application/DBList.txt");
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
I have searched lot but not getting solution for reading file using relative path.
The path is relative to the working directory of the server, not your project in NetBeans. Given your FNFE I suspect that the directory structure data/application/ doesn't exist under the working directory.
What server are you running and how are you starting it? You can figure out the server's working directory by logging;
File wd = new File(".");
log.debug("working dir: " + wd.getAbsolutePath());
Edit:
The File class and the classpath are totally unrelated concepts. Don't confuse the two. If you are looking to use classpath resources have a look at the getResource() method in ClassLoader.
try to use "\\" instead of "/"
Related
I am trying to access a text file, output.txt which is inside a project folder. The image below shows the folder structure.
String file=".\\testFiles\\output.txt";
BufferedReader br=new BufferedReader(new FileReader(file));
When I try to access that file using the code above, I get the following exception.
java.io.FileNotFoundException: .\testFiles\output.txt (No such file or directory)
I have tried different file path formats but none have worked. I think the problem is with the file path format.
Thanks in advance.
If I remember correctly you can get a folder/file in the current directory like so:
File folder = new File("testFiles");
Then you can open the file by getting the absolutePath and creating a new file with it, like so:
File file = new File(folder.getAbsoluteFile() + File.separator + "output.txt");
I'm not sure but I think you can also do:
File file = new File("testFiles/output.txt");
I hope this helps :)
P.S. this is all untested so it might not work.
Judging by the fact that you have a webcontent folder i presume this is a web project, possibly packaged as a war? In this case what you will want to do is package the respective files along with the classes and access it with something like this:
Thread.currentThread().getContextClassLoader().getResourceAsStream("output.txt")
The code above will work if you add the testFiles folder as a source folder (this means it will get packaged with the classes and be available at runtime)
The good thing is that this way the path can stay relative, no need to go absolute
I believe that your problem is due to the fact that you rely on a relative path as your path starts with a dot which means that it will by relative to the user directory (value of the system property user.dir) so I believe that your user directory is not what you expect. What you could do to debug is simply this:
System.out.println(new File(file).getAbsolutePath());
Thanks to this approach you will be able to quickly know if the absolute path is correct.
You must declare your file as a new file:
File yourFile = new File("testFiles/output.txt");
Working directory in Eclipse looks like this:
I am trying to append to happyPreview.scm and have to call this method from SpeechPreview.java. I tried:
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(getClass().getResource("previews" + File.separator + "happyPreview.scm").toString(), true)));
I thought this would work since they're all in the same 'bash' package but it doesn't work? Nothing is appending!
class.getResource() -> root is the project folder, it is not the class current package folder..
try this
getClass().getResource("/bash/previews/happyPreview.scm")
You can't write to a class resource, only read, and even then, only streaming.
This is because your code will be packaged in a .jar file, that is on the classpath. The deployed .jar file is read-only. Sure, it's not packaged in a .jar file while developing, but it will be when you are done and need to deploy it.
If you want your code to ship with a default/initial resource, and update it, the updated file must be stored elsewhere. When next you try to read it, you first check that other location, and if not found, then you load from resource file.
I have an application that creates a temporary mp3-file and puts it in a directory like C:\
File tempfile = File.createTempFile("something", ".mp3", new File("C:\\));
I'm able to read it by just using that same tempfile again.
Everything works fine in the Eclipse IDE.
But when I export my project for as a Runnable jar, my files are still being made correctly (I can play them with some normal music player like iTunes) but I can't seem to read them anymore in my application.
I found out that I need to use something like getClass().getResource("/relative/path/in/jar.mp3") for using resource files that are in the jar. But this doesn't seem to work if I want to select a file from a certain location in my file system like C:\something.mp3
Can somebody help me on this one?
It seems you dont have file name of the temp files . When you was running your program in eclipse that instance was creating a processing files, but after you made a runable you are not able to read those file that instance in eclipse created, You runable file can create its own temp file and can process them,
To make temp files globe put there (path + name ) entries in some db or property file
For example of you will create a temp file from the blow code
File tempfile = File.createTempFile("out", ".txt", new File("D:\\"));
FileWriter fstream = new FileWriter(tempfile);//write in file
out = new BufferedWriter(fstream);
the out will not be out.txt file it will be
out6654748541383250156.txt // it mean a randum number will be append with file
and you code in runable jar is no able to find these temp files
getClass().getResource() only reads resources that are on your classpath. The path that is passed to getResource() is, in fact, a path relative to any paths on your current classpath. This sounds a bit confusing, so I'll give an example:
If your classpath includes a directory C:\development\resources, you would be able to load any file under this directory using getResource(). For example, there is a file C:\development\resources\mp3\song.mp3. You could load this file by calling
getClass().getResource("mp3/song.mp3");
Bottom line: if you want to read files using getResource(), you will need those files to be on your classpath.
For loading from both privileged JARs and the file system, I have had to use two different mechanisms:
getClass().getClassLoader().getResource(path), and if that returns null,
new File(path).toURI().toURL();
You could turn this into a ResourceResolver strategy that uses the classpath method and one or more file methods (perhaps using different base paths).
I want to open a file in Java Class in an AWS Java Web project via Eclipse.
I have my file in a folder called "res" in
I tried this
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("res\\txtFile.txt"), "UTF-8"));
but not working!
I got
java.io.FileNotFoundException: res\txtFile.txt (The system
cannot find the path specified)
If the file is inside the weapp, you want ServletContext.getResourceAsStream or Class.getResourceAsStream. If it is somewhere else on the filesystem you should probably use an absolute path. A relative path like you used is resolved relative to the directory your appserver started from which might not be what you want.
I solved it !!
used this code to get the absolute path of project anywhere
String AbsolutePath = new File("").getAbsolutePath();
then add the relative path you need.
I have a java application project in Netbeans. I have just one class.
I try to do this
FileReader fr = new FileReader("sal.html");
I have the file sal.html under the same package. But I get this error when I run:
Errorjava.io.FileNotFoundException: sal.html (The system cannot find the file specified)
My guess is that Netbeans is invoking the JVM from your project's root folder. Quoting a portion of the File Javadoc:
By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.
To verify relative path resolution you could try:
System.out.println(System.getProperty("user.dir"));
System.out.println(new File("sal.html").getAbsolutePath());
You could then move your file to wherever java is looking for it. Most probably your project's root folder.
You could also consider using the class loader to read files as resources inside packages using getClass().getResourceAsStream("sal.html");. This is the preferred way of accessing resources since you no longer have to worry about absolute vs. relative paths. If a resource is in your classpath, you can access it. See this answer for more.
Put your file to main project folder. Not to any sub folders like src, or bin etc. Then it will detect your file.
Click on file view in Netbeans. Move sal.html to the project folder. Such that you will see it like this
- JavaProject
+ build
+ lib
+ nbproject
+ src
+ build.xml
manifest.mf
sal.html
Now
FileReader fr = new FileReader("sal.html");
will work.
System.out.println(System.getProperty("user.dir"));
System.out.println(new File("sal.html").getAbsolutePath());
Then it will show where the JVM is retrieving the files from. Usually for linux in the /home/username/NetbeansProjects/ApplicationName/.
Put your resources or files to this path
I think your problem is in the relative path to the file. Try to declare FileReader with full path to file.
FileNotFoundException means file not found.
The build folder for the netbeans is different where there is no file sal.html.
Try using absolute path in place of using relative path.
This is not a "File not found" problem.
This is because each class hold its own resources (let it be file, image etc.) which can be accessed only through a resource loader statement which is as below:
InputStream in = this.getClass().getResourceAsStream("sal.html");
The only fix is that you will get an InputStream instead of a file.
Hope this helps.