本帖最后由 李厚斌 于 2014-3-31 14:43 编辑
io流。。的read机制是什么机制啊。。即便再局部内没读完。。当再次调用的时候会接着继续读。难不成是read的特有机制。。没完全读完。会接着继续读?- class MyStringBuffer
- {
- private FileReader f;
- MyStringBuffer(FileReader f)
- {
- this.f=f;
- }
- public String myLine() throws IOException
- {
- StringBuilder sb=new StringBuilder();
- int num=0;
- while((num=f.read())!=-1)
- //难不成是read的特有机制。。没完全读完。会接着继续读?
- {
- if(num=='\n')
- return sb.toString();
- String m=sb.toString();
- System.out.println("sb===="+sb.toString());
- if(num=='\r')
- continue;
- else
- sb.append((char)num);
- }
- if((sb.length())!=0)
- return sb.toString();
- System.out.println("sb=="+sb.toString());
- return null;
- }
- public void myClose() throws IOException
- {
- f.close();
- }
- }
- class Demo
- {
- public static void main(String[] args)throws IOException
- {
- FileReader fr=new FileReader("g.txt");
- MyStringBuffer mbf=new MyStringBuffer(fr);
- String line=null;
- while((line=mbf.myLine())!=null)
- {
- System.out.println(line);
- }
-
- mbf.myClose();
- }
- }
- class Demo
- {
- public static void main(String[] args)throws IOException
- {
- FileReader fr=new FileReader("g.txt");
- int ch=0;
- while((ch=fr.read())!='e')
- {
- System.out.println((char)ch);
- }
- while((ch=fr.read())!=-1)
- {
- System.out.println((char)ch);
- }
- }
- }
- read的特有机制。
- 2.
- class Demo
- {
- public static void main (String[] args)throws IOException
- {
- BufferedReader br=new BufferedReader(new FileReader("2.txt"));
- BufferedWriter bw=new BufferedWriter(new FileWriter("12.txt"));
- String line=null;
- int x=0;
- while((line=br.readLine())!=null)
- {
- if(x!=0)//多个换行。
- bw.newLine();
- x++;
- bw.write(line);
- bw.flush();
- }
- bw.close();
- br.close();
- }
- }
复制代码
|