FileWriter - Write to .txt within project but outside current directory - java

So I am generating data and appending it to a text file.
Instead of using FileWriter fstream = new FileWriter("addressList.txt"); I need the file path to be in another package where I have created the addressList.txt file. It says System cannot find path error.

depending on how you are running this the path may be readable but not be writable. Resources in JAR files are generally static and should be treated as readonly.
If you are trying to create a list of per user settings (addresses stored in a list) you should consider using the System objects getenv(string) method to locate the users temporary storage and copy the default file there. After copying the default file you will be free to update the file exactly as normal.
Reference:
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#getenv(java.lang.String)

Related

File doesn't read after creating jar using netbeans

i have a small application which checks for values from a file and display the result in a jframe.
A file contain list of word to check. this file is placed in project folder "testing" and the main source testing.java file is present in location "testing\src\testing"
input file : c:\document..\netbeans\testing\
java file : c:\document..\netbeans\testing\src\testing\
when i place the input file inside folder "c:\document..\netbeans\testing\src\testing\
" the input file is not taken as input, it works only when kept on folder "c:\document..\netbeans\testing\"
so when a jar file is created it has not included the input file in that, even i manually input that is not taking the input file in and working.
some path setting issue? what can be done to solve this issue?
any help pls??
Once you create the jar, the file becomes an embedded resource. If you try to read it as a File it will no long be the same file system path as you originally use in the program. It must now be read from the class path.
To read the file from the class path, you will want to use getClass().getResourceAsStream(), which return an InputStream. If your file is in the same location (package) as your class file, then you should use
InputStream is = getClass().getResourceAsStream("input.txt");
Then you can read from the InputStream
BufferedReader reader = new BufferedReader (new InputStreamReader(is));
This generally happens, when you don't use absolute path...!
As when you run your program from IDE(Netbeans) then the HOME_FOLDER is your ProjectFolder. Relative to which you would have given the file_path(that has to be accessed in your program).
But after building, jar is present in ProjectFolder/dist. When you run the jar file the HomeFolder is not ProjectFolder rather it is ProjectFolder/dist.
So, to make it successful, to need to copy all files and folders from ProjectFolder/dist to ProjectFolder.
Then run the jar.. Hope it will fix the issue
Try putting double backslashes in your file paths. Like this:
c:\\document..\\netbeans\\testing\\src\\testing\\
This is the format that java normally requires it to be in

Java - read file from directory for jar

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).

Eclipse unable to locate file-- FileNotFoundException on File object usage

When I attempt to access a File object in Java using Eclipse, the system sends a FileNotFoundException whenever the file is accessed. I have created the file manually and placed it within Eclipse's file browser. The file does have content, so the problem isn't that it's empty. I am initializing the File with
File file = new File(this.getClass().getResource("save.txt").toString());
and accessing it with
PrintWriter p = new PrintWriter(file);
which sends the error. The stacktrace flows back through nothing except a path of initializations that create this class. The exception is
java.io.FileNotFoundException: file:\C:\Users\Nathaniel\Downloads\ERPGE-Global%20Equestria%20Workspace\Swarm\bin\main\save.txt (The filename, directory name, or volume label syntax is incorrect)
What could be causing this error, and how can it be fixed?
You need to make sure that the resource is included in the project's build path.
You can do this in one of two ways:
Put it in a folder (e.g. "res") then add that folder to the build path. That way, all the files in the folder will be added to the build path.
Put it inside (one of) your source folders. Be aware that if you put it inside a package folder, you'll need to include the relative path to that folder (see below).
Personally I prefer option #1.
Edit: You might also find it useful to have sub-directories of the res folder. If you have a file such as res/images/button.png and you've added the res folder to the build path, you need to access the file with the string "images/button.png".
Edit 2: Fixed some misleading info.
The file needs to be placed in the parent project folder itself and not the source folder. If a file is created without a specified file path, this is where it would be stored.
Provide the actual path to the file and get the file Object. Then you can use it.
File file = new File("C:\Users\Nathaniel\Downloads\save.txt");
PrintWriter p = new PrintWriter(file);
EDIT:
Externalising the File Name:
Create a properties file filedetails.properties in the external folder/package.
fileName=C:\Users\Nathaniel\Downloads\save.txt
Accessing the resource bundle.
ResourceBundle b=ResourceBundle.getBundle("external.filedetails") ;
String fileName=b.getString("fileName");
File file=new File(fileName);

How to get rid of Thumbs.db file in Windows NTFS file system?

My web based java application storing files in Local Drive(E.g: D:/AppData). It's scanning a folder for files(String[] nameOfFiles = dirName.list();) and displays all the files in the folder. The Thumbs.db also coming with them. How to omit that file? For now, i am deleting it before scanning the folder.
Is there any other way in java to skip that file from scanning?
Assuming that dirName is a File object then File.list() has an overloaded member that takes a FilenameFilter object which can be used to filter the list of files returned.

proper file path of a text file to read

I want to manipulate a file in my java program.The file to read must be paralled to my src folder.
What should I give as file path?
An elaborated example might help. From your question, what I get is,
Source Path : /home/user/project1/src/
File Path : /home/user/project1/src/
If this is the case, then once you build the project, the file path is not going to remain the same. So if you say that relative path for the file to open remains the same in built code, then you can use Class.getResourceAsStream(String path) which returns you the InputStream for given file. You can then construct the File object using it.
Refer this for details.
You should have a File object representing your src folder, and then create a new File object using that:
File textFile = new File(srcFolder, relativePath);
How you determine srcFolder really depends on the context.
EDIT: If you're just trying to read a file which is present at build time, you should include it in your built jar file and use either ClassLoader.getResourceAsStream or Class.getResourceAsStream to load it at execution time.
For example, if you have this structure:
src\
com\
xyz\
Foo.class
data\
input.txt
Then you could use Foo.class.getResourceAsStream("/data/input.txt") or Foo.class.getClassLoader.getResourceAsStream("data/input.txt"). Both will give you an InputStream you can use to load the data.

Categories

Resources