1. Get line number function case for custom class simulation LineNumberReader
2. Code implementation:
(1)Mybufferedreader. Java:
1 Packagecn.itcast_08;2 3 Importjava.io.IOException;4 ImportJava.io.Reader;5 6 /*7 * Simulate BufferedReader's readline () function with reader8 * 9 * ReadLine (): reads one line at a time, determines whether to end with newline characters, returns only content, and does not return newline charactersTen */ One Public classMybufferedreader { A PrivateReader R; - - PublicMybufferedreader (Reader r) { the This. R =R; - } - - /* + * Think: Write a method, the return value is a string. - */ + PublicString ReadLine ()throwsIOException { A /* at * I'm going to return a string, what do I do? We have to see what the R object can read. Two read methods, one character at a time, or one character array at a time - * So, we are going to return a string, which method is better? It's easy to think of a better character array, but the question is, how long is the array length ? - * There is no way to define the length of the array, you define how long is not appropriate. Therefore, you can only choose to read one character at a time. - * But, in this way, when we read down a character again, the last character is lost so we should define a temporary storage space to store the read characters. - * Who does this compare with? Array, collection, string buffer Three are available to choose from. - * After a simple analysis, the final choice is to use a string buffer object. And it's StringBuilder. in */ -StringBuilder SB =NewStringBuilder (); to + //The most troublesome thing to do with this reading is the end of judgment, but it should be read until the end of the 1 - the * /* $ HelloPanax Notoginseng World - Java the + 104101108108111 A 119111114108100 the 1069711897 + */ - $ intCH = 0; $ while(ch = r.read ())! =-1) {//104,101,108,108,111 - if(ch = = ' \ r ')) { - Continue; the } - Wuyi if(ch = = ' \ n ') { the returnSb.tostring ();//Hello -}Else { WuSb.append ((Char) ch);//Hello - } About } $ - //to prevent data loss, determine the length of SB cannot be greater than 0 - if(Sb.length () > 0) { - returnsb.tostring (); A } + the return NULL; - } $ the /* the * Write a closing method first the */ the Public voidClose ()throwsIOException { - This. R.close (); in } the}
(2)MyLineNumberReader2. Java:
1 Packagecn.itcast_09;2 3 Importjava.io.IOException;4 ImportJava.io.Reader;5 6 ImportCn.itcast_08.MyBufferedReader;7 8 Public classMyLineNumberReader2extendsMybufferedreader {9 PrivateReader R;Ten One Private intlinenumber = 0; A - PublicMyLineNumberReader2 (Reader r) { - Super(r); the } - - Public intGetlinenumber () { - returnlinenumber; + } - + Public voidSetlinenumber (intlinenumber) { A This. linenumber =linenumber; at } - - @Override - PublicString ReadLine ()throwsIOException { -linenumber++; - return Super. ReadLine (); in } -}
(3) test class Mylinenumberreadertest, as follows:
1 Packagecn.itcast_09;2 3 ImportJava.io.FileReader;4 Importjava.io.IOException;5 6 Public classMylinenumberreadertest {7 Public Static voidMain (string[] args)throwsIOException {8 //Mylinenumberreader MLNR = new Mylinenumberreader (New FileReader (9 //"My.txt"));Ten OneMyLineNumberReader2 MLNR =NewMyLineNumberReader2 (NewFileReader ( A"My.txt")); - - //Mlnr.setlinenumber (ten); the - //System.out.println (Mlnr.getlinenumber ()); - //System.out.println (Mlnr.getlinenumber ()); - //System.out.println (Mlnr.getlinenumber ()); + -String line =NULL; + while(line = Mlnr.readline ())! =NULL) { ASystem.out.println (Mlnr.getlinenumber () + ":" +Line ); at } - - mlnr.close (); - } -}
Run the effect as follows:
Java Fundamentals Enhanced IO Flow Note 55:io Flow exercise custom class simulation LineNumberReader get line number function case