Save file dialog in griffon - java

i have a save file dialog in my griffon app. the code is like this:
String filename = siteCode+'-'+model.currentClient.name+'-'+computerName+'-'+date+'.csv'
File selFile = new File(filename)
def fc = new JFileChooser()
fc.setSelectedFile(selFile)
fc.showSaveDialog()
BufferedWriter out = new BufferedWriter(new FileWriter(selFile));
The problem is, regardless the path i choose to save file at, it alsways goes to the "Staging" directory in my app's folder.

Could it be that you're not reading back the selected file from the dialog but reusing the selFile variable? Make sure you call fc.getSelectedFile() instead.

Related

android - assets files path

I have problem with assets files path in android studio. I want give path of text file in assets folder like below:
File f = new File(path_of_text_file_in_assets);
fw = new FileWriter(f);
bw = new BufferedWriter(fw);
I have similar problem in other parts of this code, that I want give the path of images in subfolder of assets like below:
File ff = new File(path_of_image_in_subfolder_of_assets);
StringBuilder sbFeatureX = uniformQuantization( ff );
I have searched a lot on the internet, but no answer!!
Note: please consider I don't want open the file, so the getAssets().open() is not my answer!
You cannot use the File class for files in assets. File is for files in the file system.
The only way to handle assets files is indeed with that .open() function using the input stream.
Further you cannot write to files in assets. They are read only there.
FileWriter and FileReader both cannot be used.

How to give user choose folder option while downloading file in javaFX

I am building a desktop app using javafx. I need to download a file from FTP.
I want before downloading the user should be prompted with the windows/mac explorer window to choose the download location.
How can I achieve this in Javafx ??
I am downloading the file on the click of an button so I am using it inside my controller class.
You can choose a file with a FileChooser or choose a directory with a DirectoryChooser.
E.g.
DirectoryChooser dirChooser = new DirectoryChooser();
File chosenDir = dirChooser.showDialog(primaryStage);
Have you tried DirectoryChoser?
It opens an OS native dialog to select a directory, and returns it as a File object.
If you want to create a new file, you can read the Path of the selected directory, append a file name and create the new File object you want to save.
For example:
DirectoryChooser dirChooser = new DirectoryChooser();
dirChooser.setTitle("Select a folder");
File selectedDir = dirChooser.showDialog(primaryStage);
String selectedDirPath = dirChooser.showDialog(mainApp.getPrimaryStage()).getAbsolutePath();
File downloadedFile = new File(selectedDirPath + "/" + downloadedFileName);

Using BufferedWriter with JFileChooser to set save directory

For my ongoing project at work I have been asked to add a feature to my program. Currently the program allows a user to upload a file which is then manipulated. After being reformatted the file is saved into a new .ACH file (which is just a text file with strict formatting standards).
Currently the user is able to upload an ACH file from anywhere on their computer using this code:
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("ACH Files", "ach");
chooser.setFileFilter(filter);
chooser.setDialogTitle("Please choose ACH file to upload");
int returnVal = chooser.showOpenDialog(chooser);
chooser.setCurrentDirectory(null);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
Later in the program buffered reader/writer make edits to the file and then save it to the new location using this code:
br = new BufferedReader(new FileReader(chooser.getSelectedFile()));
bw = new BufferedWriter(new FileWriter(chooser.getCurrentDirectory()+"//NachaOutput.ACH"));
Finally, the user is informed of the file name and save location using this code:
JOptionPane.showMessageDialog(null, "Output file saved as NachaOutput.ach to " + chooser.getCurrentDirectory());
As you can see, the new file is saved to the same directory that the file was uploaded from. My manager has requested that I allow the user the choice of a directory to save the new file to. (The file name is not imporant nor does it need to be set by the user, only the directory).
I have tried and tried to research how to do this, to no avail. The only guide that I found was very lengthy and I'll confess was too complicated for me to understand. I have attempted to create a new JFileChooser and allow the user to set the directory only and use the code:
int saveVal = chooser2.showSaveDialog(chooser2);
However I find that when I attempt to pull the saved directory later like this:
bw = new BufferedWriter(new FileWriter(chooser2.getCurrentDirectory()+"//NachaOutput.ACH"
the file is saved to my computers standard directory. (C://Users or something to that affect.)
Is there a basic way that I can give the user a simple input to select a directory and then utilize it with the bw on the line I shown above?
Thanks in advance.
Is there a basic way that I can give the user a simple input to
select a directory
The basic way to do this is:
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
The rest maybe not that obvious (you have to ask for the chosen file to get the directory):
int returnVal = chooser.showDialog(chooser, "Directory to save");
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println(chooser.getSelectedFile());
}

How to Move file in the same storage by just changing the path? (Android)

My Question is how to move file not copy by just changing the path of file system level
in android file.nenameTo(newpath); this method works only when I have path like this
File f = new File(/storage/Folder1/Folder2/image.png);
File newfile = new File((/storage/Folder1/Folder3/image.png);
f.renameTo(newfile); // this method returns true
it works but when more then one parent folder change then it's not working
File f = new File(/storage/Folder1/Folder2/image.png);
File newfile = new File((/storage/Folder3/Folder4/image.png);
f.renameTo(newfile); // this method returns false
the following case also not work
File f = new File(/storage/Folder1/Folder2/image.png);
File newfile = new File((/storage/Folder3/image.png);
f.renameTo(newfile); // this method returns false
I want to move file like above
sorry for my English
You can only rename a file in Android if the src and dst are on the same mount point. You don't specify either way. Please consider using Files.move instead to avoid this potential issue and others.

Java File manipulation

So I have an application with a JFileChooser from which I select a file to read. Then I change some words and write a new file. The problem that I am having is that when I write the new file it's saved in the project directory. How do I save it in the same directory as the file that I chose using the JFileChooser. Note: I don't want to use the JFileChooser to choose the location. I just need to save the file in the same directory as the original file that I read.
You choose a file like this:
File fileToRead = JFileChooser.getSelectedFile();
Then you read and change the content and write it back to the same location with a different name:
File fileToWrite = new File( fileToRead.getParent(), "newName.txt" );

Categories

Resources