I am making a simple java application using SWT GUI to save a txt file to a user defined location. When a button is clicked, I open the directories like this:
DirectoryDialog directoryDialog = new DirectoryDialog(shell);
directoryDialog.setFilterPath(selectedDir);
directoryDialog.setMessage("Please select a directory and click OK");
String dir = directoryDialog.open();
Now I need to be able to make a file and save it to that location. How can I make and save to the user given location?
Related
I have a text editor program that opens up a file chooser whenever you click on Save in a Menu.
In order to achieve this saving process I create a file and save it with the current path in the file chooser.
// jfc = JFileChooser
String path = jfc.getSelectedFile().toString();
File testFile = new File(path);
This seems to work when I select an existing file in the file chooser, I.E "bio.txt" gets saved as a String in my program with the path "C:/Rikard/bio.txt"
But whenever I type in a new file name in the File name section and click Save, it doesn't save as a String and i get a NullPointerException, for Example Rikard.txt. It says in the method description that getSelectedFile() gets the file from user input by typing.
What could be wrong with my code?
I am trying to open dialog that ask for name of the new file and the place to put it, and then create new file in the right place.
But, save dialog (which is the most similar dialog I saw) save the file that opened.
But, I want that if I right click on file and I choose to save part of the file in another place, a save dialog will open and it's will save part of file in the chosen place (I have function that create the specs I want in the new file).
I tried this :
String[] FILTER_NAMES = { "Spectra Files (*.spectra)"};
// These filter extensions are used to filter which files are displayed.
String[] FILTER_EXTS = { "*.spectra"};
// User has selected to save a file
FileDialog dlg = new FileDialog(shell, SWT.SAVE);
dlg.setFilterNames(FILTER_NAMES);
dlg.setFilterExtensions(FILTER_EXTS);
String fn = dlg.open();
but, when I press on save, I want it to create the new file in calling the function:
createSpectraFile();
That is creating a new FILE with the content I want.
There is a way to do it?
Not sure if I understand your question right, but if you want to create a file from the result of the FileDialog do this:
String fn = dlg.open();
if (fn != null) {
createSpectraFile(fn);
}
I am creating a program that applies a filter to an image and afterwards gives the user the option of darkening the image or making it lighter. Before the image is filtered the user picks a save location for the new image. Is there anyway I can get the filepath of that location they choose to save it to? I am using JFileChooser to accomplish this.
JFileChooser#getSelectedFile returns a java.io.File of the selected file (or null if they didn't pick something).
Have a look at How to Use File Choosers for more details
You could use File#getParentFile to return just the path element (without the file name) of the File object if all you want is the path, otherwise you have an abstract representation of the File which the user selected...
If you're using JFileChooser you can use getSelectedFile() and getPath() or getAbsolutePath()
How can I arbitrarily display the same file dialog that occurs from New -> File?
I have an Eclipse Action where I wish to display the project file path dialog, and not the system file path dialog, as seen in this image:
Also there is one catch: I want to display existing files too, as I will not be creating a new file, but instead may be overwriting/synchronizing a file. If this is NOT possible, I'll still want to know how to just display the same dialog as is in New -> File.
To display a workspace file picker, you will need to do something along these lines:
ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
shell,
new WorkbenchLabelProvider(),
new WorkbenchContentProvider());
dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
dialog.setAllowMultiple(false);
if (dialog.open() == Window.OK) {
IResource resource = (IResource) dialog.getFirstResult();
}
ElementTreeSelectionDialog is quite customizable, so you can tune the behavior to your needs.
I've created an application in Netbeans 6.9 where my ultimate aim is to create a .tcl file(or text file will do). When i run my application once I create the .tcl file and save it at a location. When I run the 2nd time and if I open that file in my application, then I am not able to get that file.I mean the data is not appending to the file,it is replacing the text.My main aim is to use just that text file that is created. But for creating that file in my application I've many options. Can anyone suggest what should I do,such that when I run my application the 2nd or later times then my options whatever I selected before(in the previous run) should be present(selected) and also the text should be appended to that file instead of replacing.
I am not writing the text directly.I have created buttons in my application.If i click on "new node" button then a dialog box appears and asks for the node name.When I click ok button, then some text is written in that file and the status is shown in a text field(in my application) that the node is created. I want that when I open an existing file then all my statuses like "new node created","new link created",etc. should be there. Also in my link button,I've got names of all the nodes created,so I want that all those node names should be present when I open an existing file.
FileWriter fstream = new FileWriter("out.txt",true);//note true here/ it will open file in Appendmode
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
out.close();
It has nothing to do with netbeans
Ref