简体   繁体   English

获取JFileChooser中所选文件的大小和名称?

[英]Get size and name of selected file in JFileChooser?

Good evening all, I was wondering what syntax I would use to get the size (mb) and the name of the selected file in my JFileChooser. 晚上好,我想知道我将使用什么语法在JFileChooser中获取大小(mb)和所选文件的名称。

JFileChooser filedlg = new JFileChooser();
    filedlg.showOpenDialog(null);
    textField.setText(//File's name);
    textField_1.setText(//File's size);

What would I use to replace the comments? 我将用什么来替换评论? All help is appreciated. 感谢所有帮助。

   JFileChooser filedlg = new JFileChooser();
    filedlg.showOpenDialog(null);
    File selected = filedlg.getSelectedFile();
    textField.setText(selected.getName());
    textField_1.setText(selected.length());

    String fullName = selected.getAbsolutePath();

Also, the .length() gives size in bytes, so you will have to transform it to whatever you want after. 另外,.length()给出了以字节为单位的大小,因此您必须将其转换为所需的任何内容。

http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html http://docs.oracle.com/javase/6/docs/api/java/io/File.html http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html http://docs.oracle.com/javase/6/docs/api/java/io/File.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM