【问题标题】:Get size and name of selected file in JFileChooser?在 JFileChooser 中获取所选文件的大小和名称?
【发布时间】:2014-06-27 20:56:14
【问题描述】:

大家晚上好,我想知道我将使用什么语法来获取 JFileChooser 中所选文件的大小 (mb) 和名称。

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

我会用什么来替换 cmets?感谢所有帮助。

【问题讨论】:

    标签: java swing user-interface jfilechooser


    【解决方案1】:
       JFileChooser filedlg = new JFileChooser();
        filedlg.showOpenDialog(null);
        File selected = filedlg.getSelectedFile();
        textField.setText(selected.getName());
        textField_1.setText(selected.length());
    
        String fullName = selected.getAbsolutePath();
    

    此外,.length() 以字节为单位提供大小,因此您必须将其转换为您想要的任何内容。

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

    【讨论】:

    • 如何获取文件路径?抱歉,应该在原帖中询问;)
    • 编辑了我的答案,再次检查。
    • @TheCodingTraceur 获取文件的绝对路径:selected.getCanonicalPath().
    • absolutePath 一直对我有用,但在这里阅读更多关于使用哪个:stackoverflow.com/questions/1099300/…
    • 谢谢你们两个,我会用他们中的两个来试验@YasmaniLlanes
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多