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());
}
Related
I have to write a Java 8 Swing app and a part of the application generates an output file (Excel spreadsheet). So at some point in the UI, the user will have to:
Select a directory on their file system, where the Excel file will be written to; and then
Enter the name of the Excel file (if they specify ".xls" or ".xlsx" in the file name, the app will output/write the file in the respective XLS/XLSX output; if they omit the file extension the default will be XLSX)
I'm interested in what a decent UX solution here is, and how that maps to Swing controls and their layout.
I know I can use JFileChooser to choose a directory or a specific file, but I've never used it to select a directory and enter the name of a new (doesn't exist on the file system yet) file name + extension.
Any ideas as to what solution I can offer here that is functional, elegant and simple/easy to use & understand?
You can use Apache's FileNameUtils, or implement you own extension extraction with String manipulation using substring and indexOf... I'll give you an example of first case.
After using a default JFileChooser as suggested, you can just check for specified file name, as JFileChooser will return a File object (or null if nothing is selected, so first check for null values):
JFileChooser fileChooser = new JFileChooser();
File selectedFile = fileChooser.getSelectedFile();
if (selectedFile != null) {
String givenExtension = FilenameUtils.getExtension(selectedFile.getName());
boolean noExtension = "".equals(givenExtension);
boolean xlsx = givenExtension.toLowerCase().contains("xlsx");
boolean xls = givenExtension.toLowerCase().contains("xls");
String newFileName = selectedFile.getName();
if (noExtension) {
newFileName += ".xlsx";
} else if (!xlsx && !xls) {
throw new Exception("Invalid name");
}
}
Remove toLowerCase() if you don't want different cases to be accepted.
This should do what you want ;)
I am doing a project for vehicle repair center. In this project I need to upload photos of the damaged vehicle so employees can watch them later.
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filePath = f.getAbsolutePath();
path.setText(filePath);
File imgFile = new File(filePath);
try {
FileInputStream fin = new FileInputStream(imgFile);
I tried above, but I can choose only one photo at one time, like this:
I need these kind of thin
ddddd
i don't do java but a quick glance at the manual suggests you need to enable selection of multiple files:
setMultiSelectionEnabled
public void setMultiSelectionEnabled(boolean b)
Sets the file chooser to allow multiple file selections.
Parameters:
b - true if multiple files may be selected
See Also:
isMultiSelectionEnabled()
https://docs.oracle.com/javase/8/docs/api/javax/swing/JFileChooser.html#setMultiSelectionEnabled-boolean-
JFileChooser filechooser = new JFileChooser();
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnValue = chooser.showOpenDialog(this);
if(returnValue == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this directory: " +
filechooser.getSelectedFile().getAbsolutePath());
this is the code i used to open a file and get its path printed but the thing is i want to get the path of a exe file which means path should end up with the file extension at the end. with the current code it won't even show the exe files.
If you need exe files, you can use a Filter, but with the good options, like that (your code is Directories oriented ):
JFrame frame=new JFrame();
JFileChooser filechooser = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("EXE File","exe");
filechooser.setFileFilter(filter);
filechooser.showOpenDialog(frame);
File file = filechooser.getSelectedFile();
System.out.println("YOU CHOOSE "+file.getAbsolutePath());
a usefull link on that question: FileFilter for JFileChooser
see this for the option filechooser.setFileSelectionMode:
JFileChooser select directory but show files
Of course it won't show files, you're using
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
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.
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" );