主题:[求助]为什么readLine()读不出空格?
import java.io.*;
public class TestBuffered {
public static void main(String[] args) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("C:Test.txt"));
BufferedReader br = new BufferedReader(new FileReader("C:/Test.txt"));
for(int i=0; i<10; i++) {
bw.write("java");
bw.newLine();
}
bw.flush();
bw.close();
/*int b;
while((b = br.read())!=-1) {
System.out.print((char)b);
} */ //A
String s;
while((s = br.readLine())!=null) {
System.out.print(s);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上这个小程序中,如果用readLine()方法,屏幕上显示不出来Test.txt文本中的空格。而用了注释掉的A部分的read()方法,却可以准确地显示Test.txt文本。请问,我在使用readLine()方法时哪里出了错?为什么 readLine()不能读出空格?
[img]http://hiphotos.baidu.com/anuobiss/pic/item/7bbaec82ba6d36b20df4d21e.jpg[/img]
[img]http://hiphotos.baidu.com/anuobiss/pic/item/d7678c0137e55e1e7aec2c1e.jpg[/img]
public class TestBuffered {
public static void main(String[] args) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("C:Test.txt"));
BufferedReader br = new BufferedReader(new FileReader("C:/Test.txt"));
for(int i=0; i<10; i++) {
bw.write("java");
bw.newLine();
}
bw.flush();
bw.close();
/*int b;
while((b = br.read())!=-1) {
System.out.print((char)b);
} */ //A
String s;
while((s = br.readLine())!=null) {
System.out.print(s);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上这个小程序中,如果用readLine()方法,屏幕上显示不出来Test.txt文本中的空格。而用了注释掉的A部分的read()方法,却可以准确地显示Test.txt文本。请问,我在使用readLine()方法时哪里出了错?为什么 readLine()不能读出空格?
[img]http://hiphotos.baidu.com/anuobiss/pic/item/7bbaec82ba6d36b20df4d21e.jpg[/img]
[img]http://hiphotos.baidu.com/anuobiss/pic/item/d7678c0137e55e1e7aec2c1e.jpg[/img]