读取.text文件

时间:2013-07-11 20:34:28

标签: java

所以我有一个类,它的作用是读取文件的内容,然后根据内容做一些事情。

package maple;

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

import javax.swing.JOptionPane;

public class SetSettings {

public SetSettings() {

    BufferedReader reader;

    File f = new File (Shared.saveLocation + "Settings.txt");

    String line = "";

    if (f.exists()) {
        while (line != null) {
            try {
                //System.out.println("COMES HERE");
                reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));
                line = reader.readLine();
                System.out.println(line);

            } catch (IOException e2) {
                // TODO Auto-generated catch block
                //e2.printStackTrace();
                //JOptionPane.showMessageDialog(null, "Error Code: Bx001", "Error", 0);

            }
        }
    }
}
 }

出于测试目的,我打印出.txt的每一行。而不是打印出.txt文件中的内容,它将打印出第一行,然后打开。无论.txt文件中有一行还是100行,它都会不断读取一行。如何让它读到下一个并在没有更多时停止?

6 个答案:

答案 0 :(得分:6)

您正在针对while循环内的文件创建一个新的BufferedReader,因此每次循环执行时读取器基本上都会重置。将创建阅读器的行移到循环外部,然后开始使用。

答案 1 :(得分:1)

将“new BufferedReader”行移到while循环之外。

if (f.exists()) {
        reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));
        while (line != null) {
            try {
                //System.out.println("COMES HERE");

                line = reader.readLine();
                System.out.println(line);

            } catch (IOException e2) {
                // TODO Auto-generated catch block
                //e2.printStackTrace();
                //JOptionPane.showMessageDialog(null, "Error Code: Bx001", "Error", 0);

            }
        }
    }

答案 2 :(得分:1)

你继续在while循环中重建你的bufferedReader。把它带到外面。

if (f.exists()) {
        reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));
        while (line != null) {
            try {
                //System.out.println("COMES HERE");
                line = reader.readLine();
                System.out.println(line);

            } catch (IOException e2) {
                // TODO Auto-generated catch block
                //e2.printStackTrace();
                //JOptionPane.showMessageDialog(null, "Error Code: Bx001", "Error", 0);

            }
        }
    }

享受:)我们都会犯错误。

这个例子也很糟糕,你可能从那里拿走了它。 http://www.cafeaulait.org/slides/sd2000east/javaio/56.html

答案 3 :(得分:1)

问题在于你的while循环中的这一行:

reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));

您每次都在创建一个新的BufferedReader,因此您一遍又一遍地阅读第一行。将此行移到循环外,然后重试。

我认为您还必须扩展try-catch块以捕获IO异常。

try {
reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));
line = reader.readLine();
while (line != null) {

            //System.out.println("COMES HERE");
            System.out.println(line);
            line = reader.readLine();
           }

} catch (IOException e2) {
            // TODO Auto-generated catch block
            //e2.printStackTrace();
            //JOptionPane.showMessageDialog(null, "Error Code: Bx001", "Error", 0);

             }

答案 4 :(得分:1)

你应该有这个:

if (f.exists()) {
    reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));
    while (line != null) {
        try {
            //System.out.println("COMES HERE");

            line = reader.readLine();
            System.out.println(line);

        } catch (IOException e2) {
            // TODO Auto-generated catch block
            //e2.printStackTrace();
            //JOptionPane.showMessageDialog(null, "Error Code: Bx001", "Error", 0);

        }
    }
}

答案 5 :(得分:0)

reader循环之前放置while您正在创建多个读者,然后您只读取第一行。

reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));
while (line != null) {
    try {
        line = reader.readLine();
        System.out.println(line);