Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何提取contents.txt文件_Java_Jframe - Fatal编程技术网

Java 如何提取contents.txt文件

Java 如何提取contents.txt文件,java,jframe,Java,Jframe,我正在用java浏览一个.txt文件。。。如何提取该文本文件中的内容 我浏览.txt文件的源代码是: JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); chooser.setFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { return f

我正在用java浏览一个.txt文件。。。如何提取该文本文件中的内容

我浏览.txt文件的源代码是:

JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));

chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
  public boolean accept(File f) {
    return f.getName().toLowerCase().endsWith(".txt")
        || f.isDirectory();
  }

  public String getDescription() {
    return ".txt Files";
  }
});

int r = chooser.showOpenDialog(new JFrame());
if (r == JFileChooser.APPROVE_OPTION) {
  String name = chooser.getSelectedFile().getName();
  jTextField3.setText(name);
}
else
{
    JOptionPane.showMessageDialog(null,
                    "You must select one Audieo File to be the reference.", "Aborting...",
                    JOptionPane.WARNING_MESSAGE);
}

如果要读取文本文件的内容,需要使用
BufferedReader
。然后使用
readLine()
方法读取内容。文件结尾由
null
表示

SSCCE:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {

    public static void main(String[] args) {

        try (BufferedReader br = new BufferedReader(new FileReader("C:\\testing.txt")))
        {

            String sCurrentLine;

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } 

    }
}  

取自

我正在使用此代码,但显示“将br类型更改为bufferreader”此错误。。。我能做什么?请帮帮我。。