【问题标题】:Using JFileChooser to read a file name使用 JFileChooser 读取文件名
【发布时间】:2012-09-21 11:27:11
【问题描述】:

我正在使用文件输入流来输入文件


String filename="D:\\abc.txt";
File file = new File(filename);

到目前为止它工作正常。现在我想使用JFileChooser 输入文件名,然后做同样的事情。但是JFileChooser 返回一个类似D:\abc.txt 的字符串。我的代码如下


public static String fileChose()
{
    JFileChooser fc= new JFileChooser();
    int ret = fc.showOpenDialog(null);

    if (ret== JFileChooser.APPROVE_OPTION) 
    {
        File file = fc.getSelectedFile();
        String filename= file.getAbsolutePath();
        return filename;
    }
    else
        return null;
}

问题在于需要用\\ 分隔文件名而不是\。我不能以这样的方式直接从 JFileChooser 输入文件名,以便直接将其用作

中的参数吗
File file = new File(Classname.fileChose());

【问题讨论】:

  • 你声明的字符串“D:\\abc.txt”和方法返回的“D:\abc.txt”字符串是同一个字符串,因为代码中的` is an escape character so \`实际上是``。话虽如此,我很难辨别你的问题到底是什么。
  • 我知道这是一个转义字符。但是 File 中的参数需要使用 \\ 格式而不是 \。所以我想知道是否可以直接访问该格式的文件路径?
  • 我同意 Vulcan,但更重要的是,您为什么不更改方法以返回 File 本身而不是文件名?比如File file = fc.getSelectedFile(); return file;你反正就是把它做成一个文件
  • @prajeeshkumar:好的.. 我认为您提供了更好的解决方案。谢谢你..
  • @CSSS 文件中的参数根本不需要 需要“\\ 格式”。在 Windows 中,\ 是文件分隔符,因此您只需要一个,而不是两个。在字符串声明端使用 \\ 是必要的,因为“\\”实际上不是两个反斜杠,它被编译为单个反斜杠。

标签: java swing relative-path jfilechooser absolute-path


【解决方案1】:

String s = fileChooser.getSelectedFile()

public String removeExtension(String s) {

    String separator = System.getProperty("file.separator");
    String filename;

    int lastSeparatorIndex = s.lastIndexOf(separator);
    if (lastSeparatorIndex == -1) {
        filename = s;
    } else {
        filename = s.substring(lastSeparatorIndex + 1);
    }

    int extensionIndex = filename.lastIndexOf(".");
    if (extensionIndex == -1)
        return filename;

    return filename.substring(0, extensionIndex);
}

【讨论】:

    【解决方案2】:

    试试

    if (code == JFileChooser.APPROVE_OPTION) {
                   File selectedFile = chooser.getSelectedFile();
                   fileName = selectedFile.getName();
                   FileInputStream fis = 
                      new FileInputStream(selectedFile);
                   InputStreamReader in = 
                      new InputStreamReader(fis, Charset.forName("UTF-8")); 
                   char[] buffer = new char[1024];
                   int n = in.read(buffer);
                   String text = new String(buffer, 0, n);
                   myPane.setText(text);
                   in.close();
                }
    

    演示:

    public class JEditorPaneFileChooser implements ActionListener {
       JFrame myFrame = null;
       JEditorPane myPane = null;
       JMenuItem cmdOpen = null;
       JMenuItem cmdSave = null;
       String dirName = "\\herong\\swing\\";
       String fileName = "JEditorPane.txt";
    
       public static void main(String[] a) {
          (new JEditorPaneFileChooser()).test();
       }
       private void test() {
          myFrame = new JFrame("JEditorPane JFileChooser Test");
          myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          myFrame.setSize(300,200);
    
          myPane = new JEditorPane();
          myPane.setContentType("text/plain");
          myPane.setText(
             "Hello computer! - \u7535\u8111\u4F60\u597D\uFF01\n"
             + "Welcome to Herong's Website!\n"
             + "\u6B22\u8FCE\u4F60\u8BBF\u95EE\u548C\u8363\u7F51\u7AD9"
             + "\uFF01\nwww.herongyang.com");
          myFrame.setContentPane(myPane);
    
          JMenuBar myBar = new JMenuBar();
          JMenu myMenu = getFileMenu();
          myBar.add(myMenu); 
          myFrame.setJMenuBar(myBar);
    
          myFrame.setVisible(true);
       }
       private JMenu getFileMenu() {
          JMenu myMenu = new JMenu("File");
          cmdOpen = new JMenuItem("Open");
          cmdOpen.addActionListener(this);
          myMenu.add(cmdOpen);
    
          cmdSave = new JMenuItem("Save");
          cmdSave.addActionListener(this);
          myMenu.add(cmdSave);
          return myMenu;
       }
       public void actionPerformed(ActionEvent e) {
          JFileChooser chooser = new JFileChooser();
          chooser.setCurrentDirectory(new File(dirName));
          chooser.setSelectedFile(new File(fileName));
          chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    
          FileNameExtensionFilter filter = new FileNameExtensionFilter(
            ".txt and .java files", "txt", "java");
          chooser.setFileFilter(filter);
    
          Object cmd = e.getSource();
          try {
             if (cmd == cmdOpen) {
                int code = chooser.showOpenDialog(myPane);
                if (code == JFileChooser.APPROVE_OPTION) {
                   File selectedFile = chooser.getSelectedFile();
                   fileName = selectedFile.getName();
                   FileInputStream fis = 
                      new FileInputStream(selectedFile);
                   InputStreamReader in = 
                      new InputStreamReader(fis, Charset.forName("UTF-8")); 
                   char[] buffer = new char[1024];
                   int n = in.read(buffer);
                   String text = new String(buffer, 0, n);
                   myPane.setText(text);
                   in.close();
                }
             } else if (cmd == cmdSave) {
                int code = chooser.showOpenDialog(myPane);
                if (code == JFileChooser.APPROVE_OPTION) {
                   File selectedFile = chooser.getSelectedFile();
                   fileName = selectedFile.getName();
                   FileOutputStream fos = 
                      new FileOutputStream(selectedFile);
                   OutputStreamWriter out = 
                      new OutputStreamWriter(fos, Charset.forName("UTF-8")); 
                   out.write(myPane.getText());
                   out.close();
                }
             }
          } catch (Exception f) {
             f.printStackTrace();
          }
       }
    }
    

    参考:http://www.herongyang.com/Swing/JEditorPane-File-Chooser-Dialog-Box.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多