How to open file after user select path and save in Java? - java

I have a question about open csv file after save.
The situation is my program export data in .csv format and use JFileChooser to open dialog, after that user choose path and save.
Is there anyway to open exported file after save ? How can I get the path without fix it in code ?
Thank you for your help.

Related

Cannot write file to Documents folder

I'm trying to write to a file in Java, or create a new file if the file doesn't exist. (Using JDK 14). However, when I run the following code I get an IOException at the if statement condition that reads The system could not find the file specified if the file doesn't exist, and Access is denied if the file does.
File file = new File(filePath);
System.out.println(filePath); // C:\Users\username\Documents\test.txt
if (file.createNewFile()) {
System.out.println("File successfully created");
} else {
System.out.println("File already exists");
}
The code works when I attempt to save it to the desktop folder and saves the file successfully, but for whatever reason isn't allowed to touch Documents.
The user I'm running IntelliJ as has full access to all files on the computer, and running the IDE as administrator did not fix the problem. However, I can save to the user folder and the desktop. It is only Documents or child directories of it that I can't save to.
There are a few similar questions on the site such as this one, however the cause is not the same as in my case this is a permissions issue, and not an issue of a missing directory.
I've just hit this same issue. It seems that JFileChooser() on some Windows 10 installations doesn't tell the os that the user has selected a folder and as such Sandboxing, Malware Control, Access control blocks access to create a file even though the user had full access (permission checks are OK but file write fails with IOException 13 or Access Denied). However FileDialog() DOES work where JFileChooser fails...
Heres what I would do to try to debug this:
Should you be trying to save to 'My Documents' as opposed to 'Documents'?
Try to save the file to C:\Users\username\test.txt (I suspect that will work)
Try to save the file to C:\Users\username\My Documents\test.txt
If you really do want to save it to 'Documents' make sure the 'Documents' director exists.
If thats the case, open the properties on Documents under the security tab select Everyone under 'Group our user names' and 'Full control' under permissions. that will open it wide up and should allow file creation. you might want to take note of what the settings where so you can put them back as setting a directory to wide open permissions can be 'problematic'. try running the program again.
1
if you want write data to file just user any Writer For example:
FileWriter writer = new FileWriter(yourFile);
You can use stream filtering for faster action.
Show more your code. You've just showed condition for existing file.
2
if you want create file in Documents folder, get a path, then make a:
File file = new File(documentPath);
while(!file.exists())
file.createNewFile();
//condition for file existing...`
If I don't help, comment below, just I can't understand your question :). Good Luck

Added text file in eclipse but cannot find it

I have a java project in eclipse. I right clicked my package and used the option: "add -> new textfile" a textfile was created but I cannot see it in my package.
Where do I located the textfile I created?
If you used New->General->Untitled Text File (I'm using Luna) the file appears in the text editor but isn't saved to the filesystem until you explicitly save it, and then you are prompted for the location to save it to.
You need to save the file as File -> Save or File -> Save As to locate it in package / directory.
right click on you package New>Other>General>Untitled Text File

Java Desktop opens files with different extensions

I am using Desktop class to open files like .txt files, .pdf files, images, etc; I have a problem that when I open first file, lets say a .jpg file, the images are displayed in the image viewer, then I try to open a .pdf document, the Desktop object tries to open the .pdf file with the image viewer, and it fails.
When I open the .pdf file first, it opens correctly. After that when I try to open a .jpg file, it is opened in the pdf browser. And so it fails...
In short, the first program associated with the first file, is used to open all the other files.
how can I solve it ?

getting input from file in an android project

I am trying to make a dictionary in android.
But i am facing problem to get input from text file.
I have put the text file in res/raw directory.
and write the code:
InputStream inputStream=getResources().openRawResource(R.raw.Dictionary);
but having exception my IDE indicates a problem under raw word.
How can I fix this ?
Place the file in the assets folder and open it like this
getResources().getAssets().open("dictionary.txt");

Changing java security file in windows

I am trying to edit the java.security file in windows. When I add an entry to it and try to save, it says 'Access Denied'. How do I change the permissions to this file.
I have also tried by making notepad to Run as administrator but it didn't work.
Please help.
Try this and say if it works. It worked for me while opening hosts file.
http://www.labnol.org/software/edit-hosts-files-as-administrator/13673/
edit:
The relevant information from the linked page:
Step 1. Open your Windows start menu, search for the notepad application and then right click the notepad icon.
Step 2. Choose “Run as administrator” and then, while inside notepad, browse to folder (java.home\lib\security\java.security).
You can now edit and save that file in the same folder without any issues. To recap, the trick is that instead of directly opening a protected file in the associated application, you run the application first as an administrator and then open the file inside it.
1) Copy original java.security file to desktop.
2) Edit the file there and save it.
3) Copy the file to %JAVA_HOME%/\jre\lib\security
4) Replace the original file with modified one.

Categories