Io, readLine () method to construct

readLine () to read a line of principle: whether a line is read, or read more characters that ultimately are read one by one on your hard disk

Therefore, end-use or read () method, a method of reading a.

 

package CoreJavaPractice;
import java.io.*;
public class Practice_1 {
    public static <K, V> void main(String[] args) throws IOException {
        FileReader fr = new FileReader("PracticeUse.java");
        MyBufferedReader mybuf = new MyBufferedReader(fr);
        String line = null;
        
        while((line=mybuf.myReadLine())!=null)
        {
            sop(line);
        }
        mybuf.myClose();
    } 
    Public  static  void SOP (Object obj) 
    { 
        System.out.println (obj); 
    } 
} 

/ * 
  Understand the class BufferedReader method readLine specific principles 
  their practice a: to simulate what readLine, actually calls the read method 
 * / 
class MyBufferedReader 
{ 
    Private the FileReader R & lt; 
    MyBufferedReader (the FileReader R & lt) 
    { 
        the this .r = R & lt; 
    } 
    public String myReadLine () throws IOException 
    { 
        // definition of a temporary container, the original packaging BufferedReader is a character array
         // to demonstrate Convenience. StringBuilder define a container. To eventually become data string.
        SB = the StringBuilder new new StringBuilder();
        int ch = 0;
        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();
    }
}

 

Guess you like

Origin www.cnblogs.com/zxl1010/p/11491414.html