繁体   English   中英

请解释以下代码

[英]Please explain the following code

public void open()
{
    int returnVal = jfilechooser.showOpenDialog(jf); //jf is JFrame's reference
    if(returnVal == JFileChooser.APPROVE_OPTION)
    {
        //to erase any text in the text area before adding new text
        jtextarea.setText(null);
        try
        {
            String fileName = jfilechooser.getSelectedFile().getPath();
            String show=jfilechooser.getSelectedFile().getName();
            Reader in = new FileReader(jfilechooser.getSelectedFile());
            char[] buff = new char[100000];
            int nch;
            while((nch = in.read(buff, 0, buff.length)) != -1)
            jtextarea.append(new String(buff, 0, nch));
            String fileContent = jtextarea.getText();
            jf.setTitle(show+" : S-Textpad");
        }
        catch(FileNotFoundException x)
        {}
        catch(IOException ioe)
        {
            System.err.println("I/O Error on Open");
        }
    }
}

它看起来像:

  • 打开“文件选择器”对话框,以便用户可以选择文件
  • 如果用户没有单击取消,则读取文件内容
  • 然后它将该内容附加到文本区域
  • 最后将帧标题设置为选定的文件名加上一个静态字符串

但是我不知道代码是否编译并且至少有一条无用的行:

String fileContent = jtextarea.getText();

暂无
暂无

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

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