I am creating program whose function is that reading "n" .txt or .java files and of these files creates a UML diagram. I have reading method, but I came to problem with load more same files. I would like to deny load same files, because it makes problems with creating a UML diagram.
I trying to solve it so that I store uploaded file into ArrayList and checking each load file with files saved in ArrayList, where are previous loaded files.
Next problem is that I click to button Yes or No when I choose same file, file is equally loaded.
And when I creating this answer I found next problem. When user select more then one file, ArrayList don't know how to add two files at once.
Is there anyone option how I would solve this problem more easily?
ArrayList<String> filenames = new ArrayList<String>();
JTabbedPane tabbedPaneUML_Files = new JTabbedPane();
private void readFiles() {
JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
FileNameExtensionFilter fileFilter =
new FileNameExtensionFilter("Only .txt a .java files",
"txt", "java");
fc.setFileFilter(fileFilter);
int returnValue = fc.showOpenDialog(this);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File[] files = fc.getSelectedFiles();
File file;
tabbedPaneUML_Files.addTab("UML diagram", panelUML);
for (int i = 0; i < files.length; i++) {
file = files[i];
for (int j = 0; j < filenames.size(); j++) {
if (filenames.get(i).equals(fc.getSelectedFile().getName())) {
Object[] options = {"Yes", "No"};
int answer = JOptionPane.showOptionDialog(this,
"Unable to load the same files! To retrieve the other files?",
"Load new file", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null, options, options[0]);
if(answer == 0) {
readFiles();
}
}
}
filenames.add(file.getName());
JTextArea loadCode = new JTextArea();
JScrollPane scrollingFile = new JScrollPane();
scrollingFile.setViewportView(loadCode);
tabbedPaneUML_Files.addTab("" + file.getName(), scrollingFile);
int ch;
try {
Reader charsReader =
new InputStreamReader(new FileInputStream(file),
"UTF-8");
while ((ch = charsReader.read()) != -1) {
loadCode.append(Character.toString((char) ch));
}
loadCode.setSelectionStart(0);
loadCode.setSelectionEnd(0);
loadCode.setEditable(false);
} catch (FileNotFoundException ex) {
Logger.getLogger(HlavniOkno.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(HlavniOkno.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Thanks for any advice. I already lost ideas.
Sorry for my English.
You can use the validatedFileChooser found here
and tweak it so instead of having a list of invalid filenames, have a list of the already chosen files.
Then you can just edit this part:
if (file.exists() && getDialogType() == SAVE_DIALOG) {
int confirm = JOptionPane.showConfirmDialog( this, file.getName() + " already exists! Would you like to overwrite it?", "File already exists", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE );
if (confirm != JOptionPane.YES_OPTION)
return;
}
to say getDialogType() == LOAD_DIALOG and the error message to: "file has already been loaded" for example.
As for the multiple filenames, do File[] files = chooser.getSelectedFiles(); to get the list of files chosen, iterate through them to get their names and then store them in the array for already selected filenames.
edit - sorry just saw you have already done File[] files = chooser.getSelectedFiles();, so all you need to do is add it to the array containing the already loaded filenames.
Related
I'm creating a program which will create files with different extensions. For that, i'm using the JFileChooser. I've set the FileFilter to accept only my desired extensions, but when I select one, I still have to add the extension in the name of the file myself. How can I solve that? Many thanks!
You basically have to add the extension yourself after the user closes the dialog.
This example allows the user to specify a file ending with ".foo" or ".bar" and will add that extension if the user did not do so.
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(false);
fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
fileChooser.setFileFilter(new FileNameExtensionFilter("Files ending in .foo", "foo"));
fileChooser.setFileFilter(new FileNameExtensionFilter("Files ending in .bar", "bar"));
int option = fileChooser.showSaveDialog(null);
if (option == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if (file!=null) {
FileFilter fileFilter = fileChooser.getFileFilter();
if (fileFilter instanceof FileNameExtensionFilter && ! fileFilter.accept(file)) {
// if the filter doesn't accept the filename, that must be because it doesn't have the correct extension
// so change the extension to the first extension offered by the filter.
FileNameExtensionFilter fileNameExtensionFilter = (FileNameExtensionFilter) fileFilter;
String extension = fileNameExtensionFilter.getExtensions()[0];
String newName = file.getName() + "." + extension;
file = new File(file.getParent(), newName);
}
System.out.println("The selected file is: " + file.getAbsolutePath());
}
}
For that you have to get the filefilter selected by the user after he presses the validating button of the JFileChooser and compare the filefilter description with the list of your extensions before initializing the file object with the specified extension in your code if there is a match. I don't know if you will understand me.
Modelexcel model = new Modelexcel();
JFileChooser selectFile = new JFileChooser();;
File file;
JButton btnExporterVersExcel = new JButton("Exporter vers Excel");
btnExporterVersExcel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(selectFile.showDialog(null, "Exporter")==JFileChooser.APPROVE_OPTION) {
String extension=selectFile.getFileFilter().getDescription();
if(extension.contains("(*.xlsx)")) {
file= new File(selectFile.getSelectedFile()+".xlsx");
}else if(extension.contains("(*.xls)")){
file= new File(selectFile.getSelectedFile()+".xls");
}
if(file.getName().endsWith("xls") ||
file.getName().endsWith("xlsx")) {
JOptionPane.showMessageDialog(null, model.Export(file, table));
}else {
JOptionPane.showMessageDialog(null, "Format invalid");
}
}
}
});
This is a fragment of my code to save files in ".xls" and ".xlsx" formats. Hope a look through it will help you
I am currently working on a "Notepad - type" file for my Object-Oriented Java class. I've got most of the program done - however I am stuck on the following issue:
When the program tries to save a file, it is supposed to first check for a files existence, obviously if the file exists the program will prompt the user for permission to overwrite the existing copy of the file [The overwrite prompt is not written yet, but it will go in the if(selectedFile.exists() == true) portion of code] - and if the file does not exist, the program will create it.
The issue I am having is that the program always creates the file before checking for the files existence. I have looked at probably 20-30+ answers to similar questions - mainly on stackoverflow, and have yet to come across the answer i need. I'm not sure if I am just "not getting it", or if I have really done something wrong..
Any answer - or hint as to where to find the answer - to this issue would be greatly appreciated.
Thank You.
Complete code (for the save portion of the program is shown below).
else if(source == saveFile)//-------------------------//SAVE FILE//--------------------------
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
fileChooser.setDialogTitle("JavaPad - Save File");
int result = fileChooser.showSaveDialog(fileChooser);
String myFile;
try
{
if(result == JFileChooser.APPROVE_OPTION)
{
myFile = fileChooser.getSelectedFile().getName();
File selectedFile = new File(myFile);
String[] lines = textArea.getText().split(System.getProperty("line.separator"));
readToSave = new Scanner(lines.toString()); // CANNOT use toString() on an Array - THIS WILL BE CHANGED PROPERLY?
PrintWriter savePWriter = new PrintWriter(selectedFile);
if(selectedFile.exists() == true)
{
JOptionPane.showMessageDialog(null, "This file already exists.");
statusLabel.setText("File Save Aborted...");
}
else
{
System.out.println("Creating File: " + myFile);
File newFile = new File(fileChooser.getSelectedFile().getName());
savePWriter = new PrintWriter(newFile);
int i = 0;
while(i < lines.length)
{
savePWriter.append(lines[i] + "\n");
System.out.println("Lines appended = " + i);
i++;
}
savePWriter.flush();
savePWriter.close();
statusLabel.setText("File Saved.");
}
}
else
{
JOptionPane.showMessageDialog(null, "Save has been canceled.");
}
}
catch(IOException IOSaveError)
{
System.out.println(IOSaveError);
}
}
You are calling new PrintWriter(selectedFile), which creates the file, right before you check whether selectedFile exists.
Don't create the PrintWriter before checking if the file exists. The PrintWriter is what causes the file to be written to.
You do:
myFile = fileChooser.getSelectedFile().getName();
File selectedFile = new File(myFile);
PrintWriter savePWriter = new PrintWriter(selectedFile); // Creates File! Probably unwanted.
if(selectedFile.exists() == true) // always true because of the line above
By the way, your code is far too complicated. Instead of having the variables selectedFile and newFile, which both are newly created File objects, you could simply use the File object returned by the dialog: newFile = fileChooser.getSelectedFile().
if(selectedFile.exists() == true)
can be simplified to
if (selectedFile.exists())
I recommend to do I/O using try-with-resources whenever possible:
try (final PrintWriter writer = new PrintWriter(selectedFile)) {
// Use writer
}
This helps with accidentally forgetting to close streams.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
I'm totally new in this world. I want to make a simple file move program. It works fine with only 1 file and until I add the new code for multiple files move. But I wanted more and I added multiple file selection to JFileChooser. To do the move of files I search around the web and found some users that asked for something similar to it. I tried to put it in my code but I've obtained an Error like this:
Exception in thread "main" java.lang.NullPointerException
at jfile.main(jfile.java:27)
Line 27 is: for (int i = 0; i < files.length; i++) {
This is the code, thanks you and sorry for my bad English.
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.swing.JFileChooser;
import org.apache.commons.io.FileUtils;
public class jfile {
public static void main (String[] args) throws IOException{
System.out.println("Creado por: MarcosCT7");
if (new File(System.getProperty("user.home"), "\\AppData\\Roaming\\.minecraft\\mods").exists());{
System.out.println("Seleccione el mod a instalar:");
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setMultiSelectionEnabled(true);
int returnVal = chooser.showOpenDialog(chooser);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("Se está instalando " + chooser.getSelectedFile().getName());
File fuente = new File(chooser.getSelectedFile().getAbsolutePath());
File destino = new File(System.getProperty("user.home"), "\\AppData\\Roaming\\.minecraft\\mods");
File[] files = fuente.listFiles(); //thats new added
for (int i = 0; i < files.length; i++) {
File destFile = new File(destino.getAbsolutePath()+File.separator+files[i].getName().replace(",", "")
.replace("[", "")
.replace("]", "")
.replace(" ", "")); //until here its new added
FileUtils.moveFileToDirectory(files[i], destFile, true); //changed to multiple move, before it was: FileUtils.moveFileToDirectory(fuente, destino, true);
}
}
else {
if(returnVal == JFileChooser.CANCEL_OPTION) {
System.out.println("No se ha seleccionado ningun mod. Adios.");
}
}
}
}
}
NullPointerException means that the variable doesn't hold any reference to a object. What I mean by reference is, For ex:
String path="";
File f=new File(path);
if(f.exists()) {
// do something
}
f is a variable of type File which holds a reference to a object File defined by path
and now you can use variable f just like any other variable call methods on that variable etc.
Another example
File f;
if(f.exists()) {
// do something
}
Now you will get NullPointerException in line if(f.exists()) because f doesn't hold any reference.
In JAVA new keyword is used to assign new reference. JVM will take care of all the low level details. It is similar to pointers in c and c++. In JAVA you don't have to explicitly delete the objects. JVM garbage collector will take care of these things. Java is object oriented language
Do read and understand OOP comcepts
Before you iterate through files using the loop, check using an if statement:
if(files==null){
System.out.println("Files not found");
}
else{
for (int i = 0; i < files.length; i++) {
File destFile = new File(destino.getAbsolutePath()+File.separator+files[i].getName().replace(",", "")
.replace("[", "")
.replace("]", "")
.replace(" ", "")); //until here its new added
FileUtils.moveFileToDirectory(files[i], destFile, true); //changed to multiple move, before it was: FileUtils.moveFileToDirectory(fuente, destino, true);
}
}
I'm trying to allow the user to either select an already created .ser file and save over it, or create a new .ser file by typing in a new name in the JFileChooser textfield. As you can see from the code below, I used a if/else statement to determine which of the two the user is doing. The problem I'm experiencing is that no matter how I rearrange things, or use different if conditions, the JFileChooser always chooses the latter option (create a new .ser file by typing in a new name). This wouldn't be a big problem, but it always adds ".ser" to the file.
For example: If I create a new file in JFC called mySERObject, it will be saved as "mySERObject.ser." Now when I open JFC again, and I select with my mouse mySERObject.ser, to save over, it instead creates a new file called "mySERObject.ser.ser."
I uses the System.out.println to see which statement gets exected, and it's always the "First one printed." Here's my code:
private void addSaveAsListener(JMenuItem item) {
item.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JFileChooser fc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Serialized Object Files", "ser", ".ser");
fc.setFileFilter(filter);
final JTextField textField = getTextField(fc); //gets text from JFC textfield
int returnVal = fc.showSaveDialog(null);
String fileName = textField.getText();
if (returnVal == JFileChooser.APPROVE_OPTION) {
if (!(fc.getSelectedFile().length() > 0)) {
System.out.println("first one printed");
File file = new File(fc.getCurrentDirectory(), fileName
+ ".ser");
try {
file.createNewFile();
fileSystem.saveAs(addressbook.getCopyList(), file.getAbsolutePath()); //serializes arraylist
} catch (IOException e) {
JOptionPane.showMessageDialog(null,
"File unable to be created.");
}
} else {
String path = fc.getSelectedFile().getAbsolutePath();
fileSystem.saveAs(addressbook.getCopyList(), path); //serializes arraylist
System.out.println("2nd one printed");
}
}
}
});
}
I was wondering if you could help me with what's wrong or by offering solutions, thank you.
Question 1: There is always another suffix added, what can I do?
Look at the following code from your example. You will see, that you get the textfield from the fc, then get the string from that (aka "mySERObject.ser") and then you gone save again, with ".ser" appendig. You can maybe use some String opperations on fileName to get rid of the suffix before further processing (for example with fileName.replace(".ser", "")).
final JTextField textField = getTextField(fc);
String fileName = textField.getText();
//fileName.replace(".ser", "")
File file = new File(fc.getCurrentDirectory(), fileName + ".ser");
Question 2: In my if/else block only if clause will be selected. Why?
I personally don't know much about JFileChooser, but fc.getSelectedFile().length() seems not to work like you think, since it always returns 0. But you can just use fileName.length(), can't you?
I feel like there should be a simple way to do this but I can't figure it out. I have a JFileChooser that allows the user to select directories. I want to show all the files in the directories to give the user some context, but only directories should be accepted as selections (maybe the Open button would be disabled when a file is selected). Is there an easy way of doing this?
My solution is a merge between the answers of camickr and trashgod:
final JFileChooser chooser = new JFileChooser() {
public void approveSelection() {
if (getSelectedFile().isFile()) {
return;
} else
super.approveSelection();
}
};
chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
See setFileSelectionMode() in How to Use File Choosers:
setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)
Addendum: The effect can be see by uncommenting line 73 of this FileChooserDemo, but it appears to be platform-dependent.
Addendum: If using FILES_AND_DIRECTORIES, consider changing the button text accordingly:
chooser.setApproveButtonText("Choose directory");
As the effect is L&F dependent, consider using DIRECTORIES_ONLY on platforms that already meet your UI requirements:
if (System.getProperty("os.name").startsWith("Mac OS X")) {
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
} else {
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
}
Override the approveSelection() method. Something like:
JFileChooser chooser = new JFileChooser( new File(".") )
{
public void approveSelection()
{
if (getSelectedFile().isFile())
{
// beep
return;
}
else
super.approveSelection();
}
};
The solution of overriding approveSelection can be annoying for certain users.
Sometimes, a user would just click on a file in a directory for no reason (even though she wants to select the directory and not the file). If that happens, the user would be (kind-a) stuck in the JFileChooser as the approveSelection will fail, even if she deselects the file. To avoid this annoyance, this is what I do:
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_AND_DIRECTORIES);
int option = fileChooser.showDialog(null,
"Select Directory");
if (option == JFileChooser.APPROVE_OPTION) {
File f = fileChooser.getSelectedFile();
// if the user accidently click a file, then select the parent directory.
if (!f.isDirectory()) {
f = f.getParentFile();
}
System.out.println("Selected directory for import " + f);
}
Selecting the directory, even when the user selected a file results in a better usability in my opinion.
AFAIK JFileChooser separates file filtering (what can be viewed, very configurable) from selection filtering (what can be chosen).
The configuration of selection filtering is much more limited, but AFAIK you can choose to allow only dirs or only files to be selected with setFileSelectionMode()
Keep the fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY) and use:
File[] selectedFiles = fileChooser.getSelectedFile().listFiles();
The JFileChooser supports three selection modes: files only, directories only, and files and directories. In your case what you need is :
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
source : http://www.java2s.com/Tutorial/Java/0240__Swing/TheJFileChoosersupportsthreeselectionmodesfilesonlydirectoriesonlyandfilesanddirectories.htm
Select Multiple Folders But Show All Included files
import javax.swing.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
public class MultipleFilesAndDirectoryChooserButDisplayFiles {
public static void main(String[] args) {
ArrayList<File> tempFiles = new ArrayList<>();
ArrayList<File> finalFiles = new ArrayList<>();
ArrayList<String> relativeFiles = new ArrayList<>();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Choose File To Transfer");
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.setMultiSelectionEnabled(true);
int returnVal = fileChooser.showOpenDialog(null);
fileChooser.approveSelection();
if (returnVal == JFileChooser.APPROVE_OPTION) {
fileChooser.approveSelection();
var fileAddress = fileChooser.getSelectedFiles();
for (var arrElement : fileAddress) {
tempFiles.add(arrElement);
File baseFile;
baseFile = arrElement.getParentFile();
Iterator<File> iterator = tempFiles.iterator();
while (iterator.hasNext()) {
File file = iterator.next();
if (file.isDirectory()) {
var enclosedFiles = file.listFiles();
if (enclosedFiles != null) {
if (enclosedFiles.length != 0) {
var index = tempFiles.indexOf(file);
tempFiles.remove(file);
tempFiles.addAll(index, Arrays.asList(enclosedFiles));
iterator = tempFiles.iterator();
} else {
tempFiles.remove(file);
finalFiles.add(file);
relativeFiles.add(baseFile.toURI().relativize(file.toURI()).getPath());
iterator = tempFiles.iterator();
}
}
} else if (file.isFile()) {
tempFiles.remove(file);
finalFiles.add(file);
relativeFiles.add(baseFile.toURI().relativize(file.toURI()).getPath());
iterator = tempFiles.iterator();
}
}
}
for (var relativeFile : relativeFiles) {
System.out.println(relativeFile);
}
for (var file : finalFiles) {
System.out.println(file);
}
}
}
}
Output:
Folder1/EmptyFolder/
Folder1/SubFolder1/1.1.txt
Folder1/SubFolder1/1.2.txt
Folder1/SubFolder1/1.3.txt
Folder1/SubFolder1/SubFolder 1.1/1.1.1.txt
Folder1/SubFolder1/SubFolder 1.1/1.2.1.txt
Folder1/SubFolder1/SubFolder 1.1/1.3.1.txt
Folder1/SubFolder2/2.1/2.1.1.txt
Folder1/SubFolder2/2.1/2.1.2.txt
Folder1/SubFolder2/2.1/2.1.3.txt
Folder1/SubFolder3/3.1.txt
Folder1/SubFolder3/3.2.txt
Folder1/SubFolder3/3.3.txt
Folder2/Sub Folder/2.1.txt
Folder2/Sub Folder/EmptyFolder/
file1.txt
file2.txt
E:\Folder1\EmptyFolder
E:\Folder1\SubFolder1\1.1.txt
E:\Folder1\SubFolder1\1.2.txt
E:\Folder1\SubFolder1\1.3.txt
E:\Folder1\SubFolder1\SubFolder 1.1\1.1.1.txt
E:\Folder1\SubFolder1\SubFolder 1.1\1.2.1.txt
E:\Folder1\SubFolder1\SubFolder 1.1\1.3.1.txt
E:\Folder1\SubFolder2\2.1\2.1.1.txt
E:\Folder1\SubFolder2\2.1\2.1.2.txt
E:\Folder1\SubFolder2\2.1\2.1.3.txt
E:\Folder1\SubFolder3\3.1.txt
E:\Folder1\SubFolder3\3.2.txt
E:\Folder1\SubFolder3\3.3.txt
E:\Folder2\Sub Folder\2.1.txt
E:\Folder2\Sub Folder\EmptyFolder
E:\file1.txt
E:\file2.txt
I think the best solution is just to allow the user to select either a file or a directory. And if the user select a file just use the directory where that file is located.