将“\t”写入文本文件会创建一行?
Writing a "\t" to a text file creates a line?
我正在写 2 个文本文件,我想要一种更轻松的方式来阅读文本文件,而不需要所有的单词和数字彼此靠近,所以我选择用一个标签来分隔它们。但是当我这样做时,我的 class LineNumberReader
似乎认为该选项卡是一个新行。
我有 2 个 class。 TextWriting
和 compareTextFiles
当我 运行 我的 TextWriting
class 我可以得到这样的输出:
(1) word1
(2) word2
(3) word3
所以它正在按预期工作。 (我使用空格来格式化,但文件正确包含制表符)
在我的另一个 class compareTextFiles
中,我比较了我从第一个 class 写的 2 个文本文件。重要的代码是这个。
String word,word2;
int i;
lnr = new LineNumberReader(new FileReader(file));
while (sc.hasNext() && sc2.hasNext()) {
word = sc.next();
word2 = sc2.next();
lnr.readLine();
if (word.equalsIgnoreCase(word2)) {
i = lnr.getLineNumber();
System.out.println("Line number: " + i + " and the word: [" + word + "]" + " and the word " + "[" + word2 + "]" + " is the same!");
}
else
System.out.println("[" + word + "]" + " and " + "[" + word2 + "]" + " is not the same");
}
我收到的输出是:
Line number: 1 and the word: [(1)] and the word [(1)] is the same!
Line number: 2 and the word: [asd] and the word [asd] is the same!
Line number: 3 and the word: [(2)] and the word [(2)] is the same!
Line number: 3 and the word: [yeye] and the word [yeye] is the same!
Line number: 3 and the word: [(3)] and the word [(3)] is the same!
Line number: 3 and the word: [he] and the word [he] is the same!
为什么它多次卡在 3 上,tab 是否创建了某种新行?
您的代码为每个扫描器令牌调用 LineNumberReader.readLine
。假设 a) 每个 Scanner 使用默认分隔符(在这种情况下,每行有 2 个标记) b) LineNumberReader.readLine
递增 LineNumberReader.getLineNumber
返回的值,直到 文件已被完全读取 - 然后对于每个标记(而不是每一行),它将递增,直到读取 3 行(然后停止递增),从而得到你得到的输出。
另一个建议(有很多方法可以给这只猫换皮):考虑只使用2个Scanner读取文件,使用Scanner.readLine
方法读取文件。对于每一行,增加一个表示行号的变量,然后根据需要解析这些行。
int lineCount = 0;
while ( sc.hasNextLine() && sc2.hasNextLine() ){
lineCount++;
String line1 = sc.nextLine();
String line2 = sc2.nextLine();
//parse the lines
String[] line1tabs = line1.split("\t");
//etc...
}
我正在写 2 个文本文件,我想要一种更轻松的方式来阅读文本文件,而不需要所有的单词和数字彼此靠近,所以我选择用一个标签来分隔它们。但是当我这样做时,我的 class LineNumberReader
似乎认为该选项卡是一个新行。
我有 2 个 class。 TextWriting
和 compareTextFiles
当我 运行 我的 TextWriting
class 我可以得到这样的输出:
(1) word1
(2) word2
(3) word3
所以它正在按预期工作。 (我使用空格来格式化,但文件正确包含制表符)
在我的另一个 class compareTextFiles
中,我比较了我从第一个 class 写的 2 个文本文件。重要的代码是这个。
String word,word2;
int i;
lnr = new LineNumberReader(new FileReader(file));
while (sc.hasNext() && sc2.hasNext()) {
word = sc.next();
word2 = sc2.next();
lnr.readLine();
if (word.equalsIgnoreCase(word2)) {
i = lnr.getLineNumber();
System.out.println("Line number: " + i + " and the word: [" + word + "]" + " and the word " + "[" + word2 + "]" + " is the same!");
}
else
System.out.println("[" + word + "]" + " and " + "[" + word2 + "]" + " is not the same");
}
我收到的输出是:
Line number: 1 and the word: [(1)] and the word [(1)] is the same!
Line number: 2 and the word: [asd] and the word [asd] is the same!
Line number: 3 and the word: [(2)] and the word [(2)] is the same!
Line number: 3 and the word: [yeye] and the word [yeye] is the same!
Line number: 3 and the word: [(3)] and the word [(3)] is the same!
Line number: 3 and the word: [he] and the word [he] is the same!
为什么它多次卡在 3 上,tab 是否创建了某种新行?
您的代码为每个扫描器令牌调用 LineNumberReader.readLine
。假设 a) 每个 Scanner 使用默认分隔符(在这种情况下,每行有 2 个标记) b) LineNumberReader.readLine
递增 LineNumberReader.getLineNumber
返回的值,直到 文件已被完全读取 - 然后对于每个标记(而不是每一行),它将递增,直到读取 3 行(然后停止递增),从而得到你得到的输出。
另一个建议(有很多方法可以给这只猫换皮):考虑只使用2个Scanner读取文件,使用Scanner.readLine
方法读取文件。对于每一行,增加一个表示行号的变量,然后根据需要解析这些行。
int lineCount = 0;
while ( sc.hasNextLine() && sc2.hasNextLine() ){
lineCount++;
String line1 = sc.nextLine();
String line2 = sc2.nextLine();
//parse the lines
String[] line1tabs = line1.split("\t");
//etc...
}