简体   繁体   English

BufferedReader无法读取完整的.txt文件

[英]BufferedReader not reading complete .txt file

I'm trying to read a moderately sized txt file (65,00 words) into a String, then a string array. 我正在尝试将中等大小的txt文件(65,00个单词)读入字符串,然后是字符串数组。 The bufferedreader throws the "could not read file" catch. bufferedreader引发“无法读取文件”捕获。 When I clear it, a small part of the contents of the text file is shown in the system.out. 当我清除它时,system.out中将显示文本文件的一小部分内容。 I do not get the error with smaller text files. 较小的文本文件没有出现错误。 I'm a beginner and I'm having a lot of trouble trying to narrow down the issue. 我是初学者,尝试缩小问题范围时遇到很多麻烦。

Why isn't the BufferedReader ingesting the entire file? 为什么BufferedReader不提取整个文件? And why is the "could not read file" error being thrown? 为什么会引发“无法读取文件”错误?

public class Main {

public static void main(String[] args) {

    final Guid Main = new Guid();  //creates instance of Guid

    Main.mergeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) 
        {
        merge();

        }
    });

}
    static void merge()
    {

        //read file and turn into string
        String pathone = open(Guid.PathOne);
        System.out.print(pathone);

        //parse and format 
        //String[] oneArray = pathone.replace("\n"," ").split(" "); 


        //get pathtwo text
        //String pathtwo = open(Guid.PathTwo);

        //parse and format
        //load into array
        //compare array entries
        //add new array entry
        //sort array
        //write array to paththree file


        //for(int i=0; i<oneArray.length;i++)
        //{
            //System.out.println(oneArray[i]);
        //}

    }


    public static String open(JTextArea Path)
    {
        String record = null;

        FileReader frFile = null;
        try {
            frFile = new FileReader(Path.getText());//gets file from Path

            BufferedReader brFile = new BufferedReader(frFile);//creates buffered reader

            record = brFile.readLine() + "\n"; //gets contents of file and puts it into a string
            brFile.mark(0);

            while (brFile.read() != -1) //loop to read the rest of the text file
                {
                    brFile.reset();
                    record = record + brFile.readLine() + "\n";
                    brFile.mark(0);
                }

        } 
        catch (FileNotFoundException e) //catch path is in error
            {
                JFrame frame = null;

                JOptionPane.showMessageDialog(frame, "Could not find file.");
            } 
        catch (IOException e) //catch if file cannot be read
            {
                JFrame frame = null;

                JOptionPane.showMessageDialog(frame, "Could not read file.");
            }

        try {  //closes file
            frFile.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return record;
    }


}

Do in this way. 用这种方式做。 Remove reset() and mark() methods if you want to read the entire file. 如果要读取整个文件,请删除reset()mark()方法。

StringBuilder record = new StringBuilder();
BufferedReader brFile = new BufferedReader(frFile);
String line = null;
while ((line = brFile.readLine()) != null) {
    record.append(line).append("\n");
}

Note: 注意:

  • Don't forget to close the stream. 不要忘记关闭流。
  • Use finally block to close the stream 使用finally块关闭流
  • Use StringBuilder or StringBuffer to append the string. 使用StringBuilder或StringBuffer附加字符串。
  • Use System.getProperty("line.separator") to get the system specific line separator 使用System.getProperty("line.separator")获取系统特定的行分隔符

Have a look at Java7 try-with-resources Statement advantage 看看Java7 try-with-resources语句的优势

readLine will read the first line of your document. readLine将读取文档的第一行。 Try with it (not tested): 尝试一下(未测试):

String lineReaded;

while ((lineReaded=brFile.readLine())!=null) 
{
record +=linereaded+"\n";
}

Niko 尼可

您可能想使用Files.readAllLines

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

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