I have a method fileUpload() which opens a FileChooser-Menu. If i select there a file then the absolute path and the filesize is written on the console. First i try this only with:
System.out.println(file.getAbsolutePath()); // Print: C:\Users\Anonym XY\Desktop\test.txt\C:\Users\Anonym XY\Desktop\test.txt
System.out.println(file.length()); // Print: "0"
But for the absolute path i get the absolute path but double printed?! Oo And for the filesize i get here 0, which is the false size.
If i wirte this:
System.out.println(fileChooser.getSelectedFile().length()); // Print: "15747840"
then i get the right filesize.
My whole method:
public void fileUpload() {
JFileChooser fileChooser = new JFileChooser();
if(fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
File file = new File(fileChooser.getSelectedFile() ,fileChooser.getSelectedFile().getAbsolutePath());
System.out.println("Path: " + file.getAbsolutePath()); // Print: "Path: C:\Users\Anonym XY\Desktop\test.txt\C:\Users\Anonym XY\Desktop\test.txt"
System.out.println("Filesize: " + fileChooser.getSelectedFile().length()); // Print: "Filesize: 15747840"
System.out.println("Filesize: " + file.length()); // Print: "Filesize: 0"
}else if(fileChooser.showOpenDialog(null)==JFileChooser.ERROR_OPTION) {
System.out.println("Error");
}
}
Error is in this line:
File file = new File(fileChooser.getSelectedFile(), fileChooser.getSelectedFile().getAbsolutePath());
what it essentially does: it combines selected-file with itself: as parent and as child, so you get the path "doubled".
the following solves the problem:
File file = fileChooser.getSelectedFile();
I think here the issue is your file creation.
File file = new File(fileChooser.getSelectedFile() ,fileChooser.getSelectedFile().getAbsolutePath());
so instead the above line if you just do
File file = fileChooser.getSelectedFile()
Just to point, I have not try this but why to create 2 objects of file? If you are doing this for checking/learning purpose then use
File file = new File(fileChooser.getSelectedFile().getAbsolutePath());
Your problem:
new File(fileChooser.getSelectedFile() ,fileChooser.getSelectedFile().getAbsolutePath());
This creates a new File instance from a parent abstract pathname and a child pathname string.
http://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.io.File , java.lang.String)
The new file has size zero and its path is obtained by appending path of 'parent' and 'child' path.
Try this: file chooser returns a file, no need to create new file.
JFileChooser chooser = new JFileChooser();
int showOpenDialog = chooser.showOpenDialog(null);
if (showOpenDialog == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
System.out.println("path: " + f.getAbsolutePath());
System.out.println("File size: " + f.length());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.