I am exploring Jfilechooser.I already get the file path in the Jfilechooser.Now I want to display the information like file name, file size, location, and access rights.Is their anyway to display those information using the file path only.Anyone can help me?I want it to display in a TextArea.
this is the how I pop up a Jfilechooser.
private void browsebuttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
fieldlocation.setText(filename);
}
Take a look at the JavaDoc for File:
File.getName()
Will return the name of the file
File.length()
Will return the size of the file in bytes
File.getAbsolutePath()
Will return the absolute path of the file
File.canRead()
File.canWrite()
File.canExecute()
Will return your access rights on the file.
One thing I would note about your code is that you don't check the return value from the file chooser. If the user clicks cancel you probably want to abort processing. The way to do this is to check the return value of JFileChoose.showOpenDialog(null); like so:
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
Straight from the JavaDoc.
In short I would suggest that you (re)?read the documentation for the APIs you are using. It will save you much time in the long run if you understand your own code.
Related
I have no problem opening multiple file types, however I want the option to be able to save multiple file types. I can't figure out how to get the option the user selected to save their file types as.
Here is my code:
//.txt and .encm files are allowed
final JFileChooser txtOrEncmChooser = new JFileChooser(new File(System.getProperty("user.dir")));
//only allow user to use .txt and .encm files
txtOrEncmChooser.addChoosableFileFilter(new FileNameExtensionFilter("Encrypted Data File (*.encm)", "encm"));
txtOrEncmChooser.addChoosableFileFilter(new FileNameExtensionFilter("Text File (*.txt)", "txt"));
txtOrEncmChooser.setAcceptAllFileFilterUsed(false);
//displays save file dialog
int returnVal = txtOrEncmChooser.showSaveDialog(FileEncryptionFilter.this);
//use chose to save file
if (returnVal == JFileChooser.APPROVE_OPTION)
{
//selects file
File fileName = txtOrEncmChooser.getSelectedFile();
/****The problem is here, how do I figure out what file type the user selected?****/
if .txt is selected{
String filePath = fileName.getPath() + ".txt"; //file path
}
else if .encm is selected
{
String filePath = fileName.getPath() + ".encm"; //file path
}
}
I have searched the forums for solutions, but I only found the solution for opening multiple file types, not saving multiple file types.
https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#getFileFilter()
FileNameExtensionFilter f = (FileNameExtensionFilter) txtOrEncmChooser.getFileFilter();
Since getExtensions() returns an array, you'll have to iterate through all the extensions. If you are sure that it only contains one element, of course, you won't need to do this. Just check f.getExtensions()[0].equals("txt"), for example. You could also create your file filters as local variables and then compare them with the selected one.
I have a program that (so far) searches through a file for certain keywords and prints all the lines that have that keyword in it. The problem is it can only search one text file at a time. How can I make it so it searches every text file inside of a folder?
This is the code for the Find File Button that opens just a text file
findFileButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
JFileChooser fileChoice = new JFileChooser();
fileChoice.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = fileChoice.showOpenDialog(AdminPanel.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fileChoice.getSelectedFile();
wantedFile = file.getAbsolutePath();
}
}
});
I tried switching the fileSelectionMode to FILES_AND_DIRECTORIES but when i clicked on a folder it would trigger my try/catch for a file wasn't found/specified.
Any help?
Thanks,
~Zmyth
To start with, when using FILES_AND_DIRECTORIES, you will either get a file OR directory. You need to check the type using File.isDirectory to determine what you should do. If it's a directory, then you need to list all the files within it and process them as required, if it's a file, you just need to process it as normal.
If you only want the user to be able to select directories, you could use DIRECTORIES_ONLY
To search the directory...
You could...
Use one of the File#listFiles methods to list all the files from within the selected directory.
This will only list the files for the current directory, if you want to do a recursive search, you need to implement this yourself, but's not to hard
You could...
Use Files#walkFileTree which can be used to walk the current directory and sub directories, depending how you code the FileVisitor
See Walking the File Tree for more details
Sorry if I was unclear in my original question, I wasn't sure how to go about searching for the files in a folder instead of just a file. I was able to get it eventually though with the help of the one answer. I would up vote it but I don't have enough reputation yet. This is what I was looking for:
findFileButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
JFileChooser fileChoice = new JFileChooser();
fileChoice.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fileChoice.showOpenDialog(Panel.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File folder = fileChoice.getCurrentDirectory();
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
wantedFilesList.add(listOfFiles[i].getAbsolutePath());
currentFilesList.add(listOfFiles[i].getName());
}
else if (listOfFiles[i].isDirectory())
{
// Blerg
}
}
}
}
});
I have code that is supposed to choose a folder from a JFileChooser, but it still is acting like its choosing a file, even if I use fs.setDialogType. I tried showSaveDialog and showOpenDialog, but they both don't work.
Here is my code:
public static String getFolder() {
JFileChooser fs = new JFileChooser();
fs.setDialogType(JFileChooser.DIRECTORIES_ONLY);
fs.showSaveDialog(null);
if (fs.getSelectedFile() != null)
return fs.getSelectedFile().getAbsolutePath();
return "null";
}
setDialogType is used to set the open, save or custom type for the dialog. Use setFileSelectionMode to specify the whether the dialog should select files or directories
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
I have a simple JFileChooser set up in the following manner
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setFileFilter(new FileFilter() {
...
});
int v = chooser.showOpenDialog(this);
if (v == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
System.out.println(file.getAbsolutePath());
}
As you can see, this FileChooser starts out in the current directory, which in my Netbeans project, is the root of the project folder. Here's the problem: When I select a file and it prints out the absolute path, it includes the "." in the path. For instance, the output I get is:
/Users/MyName/Folder1/Folder2/./Temp.xls
Of course, this is weird, especially since I'm displaying this to the user. Now, I could be hacky and do some fun post substring processing stuff to get rid of that "/./" portion. But...is there a non-lazy programmer way to fix this problem? Thanks in advance!
Use the system property "user.dir" as follows:
File workingDirectory = new File(System.getProperty("user.dir"));
chooser.setCurrentDirectory(workingDirectory);
I created a JFileChooser to open a file, but when I select a file and open it,for second the time that i want to select a file, the JFileChooser is not in the current directory.
How set the JFileChooser to open the current directory?
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
int result = fileChooser.showOpenDialog( this );
if ( result == JFileChooser.APPROVE_OPTION ){
File fileName = fileChooser.getSelectedFile();
File path=fileChooser.getCurrentDirectory();
if ( ( fileName == null ) || ( fileName.getName().equals( "" ) ) )
{
JOptionPane.showMessageDialog( this, "Invalid File Name",
"Invalid File Name", JOptionPane.ERROR_MESSAGE );
}
else{
currentPath=path.getPath()+"\\"+fileName.getName();}
}
Either pass the directory into the constructor via the File parameter (a File can also be a directory, FYI), or use the .setCurrentDirectory(File dir) method before you make the JFileChooser visible.
Also, to make the JFileChooser stay in the same folder, you need to save the folder of the file/directory chosen from last time, and use THAT value to control which folder it starts in the subsequent times via .setCurrentDirectory(File dir)
Make the chooser a class level attribute and create it only once. That way, it not only points to where it did when closed, but will have the same size, location, file filter selected etc.