找一个文本文件,把最后一行文字的回车换行删掉,然后测试我的代码
- package com.xiaozhi.io;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class Test {
- public static void main(String[] args) throws IOException {
- MybufferReader mybufferReader=new MybufferReader(new FileReader("c:\\1.txt"));
- String line=null;
- while((line=mybufferReader.myRead())!=null)
- {
- System.out.println(line);
- }
- }
- }
- class MybufferReader{
- FileReader fileReader;
-
- public MybufferReader(FileReader fileReader) {
- super();
- this.fileReader = fileReader;
- }
- public String myRead() throws IOException
- {
- int c=0;
- StringBuilder builder=new StringBuilder();
- while((c=fileReader.read())!=-1 && c!='\r'){
- if(c == '\n')
- continue;
- builder.append((char)c);
- }
- if(c== -1)
- {
- System.out.println("-------------------"+builder.toString());
- return builder.toString().length()==0 ? null : builder.toString();//最后一行没有回程换行
-
- }
- return builder.toString();
- }
- }
复制代码 |