问题部分:
//请大家不要晕
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//这是一个包装类
class MyBufferedReader {
private FileReader r;
MyBufferedReader(FileReader r) {
this.r = r;
}
public String MyEeadLine() throws Exception {
StringBuilder sb = new StringBuilder();
int ch;
while ((ch = r.read()) != -1) {
if (ch == '\r')
continue;
if (ch == '\n')
return sb.toString();
else
sb.append((char) ch);
}
if (sb.length() != 0)
return sb.toString();
return null;
}
// 关闭输入流
public void MyClose() throws IOException {
r.close();
}
}
// 测试类
public class Test1 {
public static void main(String[] args) throws Exception {
MyBufferedReader mbr = new MyBufferedReader(
new FileReader("Reader.txt"));
BufferedWriter bfr = new BufferedWriter(new FileWriter("Writer.txt"));
String line;
while ((line = mbr.MyEeadLine()) != null) {
bfr.write(line);
bfr.newLine();
}
mbr.MyClose();
bfr.close();
}
}
问题部分:
当我用eclipse运行的时候,为什么写入文件中多了一行。如下图:
读取的文件:
写入的文件:
备注:怎样把写入的文件多余的空行给去掉,用代码体现出来。
|
|