我的bufferedReader读取整个文件,而不只是一行。为什么?

时间:2014-12-23 12:07:53

标签: java byte bufferedreader

我有一个bufferedReader,它应该读取文本文件的一行(已编码),对其进行解码,并检查它是否包含一组特定的数字。它检查它是否包含一组特定数字的部分很好,但是,我有一个问题,它读取整个文件,我的代码在下面,以及从文件中读取的内容的输出如下所示:

代码:

prviate static final String UTF8_BOM = "\uFEFF";
String cardNumberStr = "106382076";
String lineFromFile = null;
lineFromFile = bufferedReaderToWrite.readLine();
if (lineFromFile.startsWith(UTF8_BOM)) {
    lineFromFile = lineFromFile.substring(1);
}
lineFromFile = lineFromFile.substring(1, lineFromFile.length()-1);  //Convert bytes read back to String
for(String s: lineFromFile.split(", ")){    //Split every byte on ,
    sb.append((char) Integer.parseInt(s));  //Convert byte to char
}
String textToCheck = sb.toString(); //Convert StringBuilder to String
System.out.println(textToCheck);
System.out.println();
System.out.println(cardNumberStr);
if(textToCheck.contains(cardNumberStr)){
    System.out.println(lineFromFile);
}

从文件中读取的内容输出:

106382076
78, 97, 109, 101, 32, 116, 101, 115, 116, 32, 99, 97, 114, 100, 78, 111, 32, 52,
48, 52, 54, 48, 52, 57, 51, 57, 32, 67, 117, 114, 114, 101, 110, 116, 32, 66, 9
7, 108, 97, 110, 99, 101, 32, 51, 49, 48, 32, 111, 118, 101, 114, 100, 114, 97,
102, 116, 32, 102, 97, 108, 115, 101, 32, 111, 118, 101, 114, 68, 114, 97, 102,
116, 76, 105, 109, 105, 116, 32, 48, 32, 112, 105, 110, 32, 50, 50, 50, 50
Name test cardNo 404604939 Current Balance 310 overdraft false overDraftLimit 0
pin 2222Name account cardNo 106382076 Current Balance 132 overdraft false overDr
aftLimit 0 pin 4444Name test cardNo 404604939 Current Balance 310 overdraft fals
e overDraftLimit 0 pin 2222Name account cardNo 106382076 Current Balance 132 ove
rdraft false overDraftLimit 0 pin 4444`

应输出:

Name test cardNo 404604939 Current Balance 310 overdraft false overDraftLimit 0
pin 2222

Name account cardNo 106382076 Current Balance 132 overdraft false overDr
aftLimit 0 pin 4444

文本文件的内容如下:

[78, 97, 109, 101, 32, 116, 101, 115, 116, 32, 99, 97, 114, 100, 78, 111, 32, 49, 56, 57, 52, 57, 51, 50, 56, 52, 32, 67, 117, 114, 114, 101, 110, 116, 32, 66, 97, 108, 97, 110, 99, 101, 32, 57, 52, 32, 111, 118, 101, 114, 100, 114, 97, 102, 116, 32, 102, 97, 108, 115, 101, 32, 111, 118, 101, 114, 68, 114, 97, 102, 116, 76, 105, 109, 105, 116, 32, 48, 32, 112, 105, 110, 32, 50, 51, 50, 51]
[78, 97, 109, 101, 32, 116, 101, 100, 116, 32, 99, 97, 114, 100, 78, 111, 32, 55, 48, 49, 55, 50, 53, 50, 51, 49, 32, 67, 117, 114, 114, 101, 110, 116, 32, 66, 97, 108, 97, 110, 99, 101, 32, 50, 54, 48, 32, 111, 118, 101, 114, 100, 114, 97, 102, 116, 32, 102, 97, 108, 115, 101, 32, 111, 118, 101, 114, 68, 114, 97, 102, 116, 76, 105, 109, 105, 116, 32, 48, 32, 112, 105, 110, 32, 50, 50, 50, 50]`

3 个答案:

答案 0 :(得分:1)

我认为这里的问题是,当您到您的文件时,您忘记包含换行符...

(另外,您对BOM有什么用处?)

当您写信并使用BufferedWriter时,请在写完一行后忘记writer.newLine()

请注意,您可以使用Files.write()直接将一组行写入文件,这将为您插入换行符。最后,您可以使用Files.readAllLines()一次读取一行中的所有文件。

使用Java 8,您还有Files.lines()

答案 1 :(得分:0)

You can use BufferedReader.readLine() to get the first line.

请注意,下一次调用readLine()会得到第二行,下一行是第3行....

编辑:

如果您想指定特定的行,正如您的评论建议的那样 - 您可能希望使用Apache Commons FileUtils ,并使用: FileUtils.readLines()。它将为您提供一个List,您可以像任何列表一样处理,包括获取特定行。请注意,它有更多的开销,因为它读取整个文件,并用它的行填充List。

答案 2 :(得分:0)

BufferReader readLine阅读一行文字。一条线被认为是换行(' \ n'),回车(' \ r')或回车后紧接着换行符中的任何一条终止

当你有多行时,你应该把你的代码放在这样的循环中逐一读取每一行:

public static void main(String[] args) throws IOException {
    BufferedReader bufferedReaderToWrite = new BufferedReader(
        new FileReader(new File("D:/tmp/Z_Z_DELETE_ASAP/file.txt")));

    while ((lineFromFile = bufferedReaderToWrite.readLine()) != null) {
        StringBuilder sb = new StringBuilder();
        if (lineFromFile.startsWith(UTF8_BOM)) {
            lineFromFile = lineFromFile.substring(1);
        }
        lineFromFile = lineFromFile.substring(1, lineFromFile.length() - 1);  //Convert bytes read back to String
        for (String s : lineFromFile.split(", ")) {    //Split every byte on ,
            sb.append((char) Integer.parseInt(s));  //Convert byte to char
        }
        String textToCheck = sb.toString(); //Convert StringBuilder to String
        System.out.println(textToCheck);
        System.out.println();
        System.out.println(cardNumberStr);
        if (textToCheck.contains(cardNumberStr)) {
            System.out.println(lineFromFile);
        }
        System.out.println();
    }
}

然后输出将是:

Name test cardNo 189493284 Current Balance 94 overdraft false overDraftLimit 0 pin 2323

106382076

Name tedt cardNo 701725231 Current Balance 260 overdraft false overDraftLimit 0 pin 2222

106382076