黑马程序员_IO流-字符流的学习(二)

来源:互联网 发布:centos 五笔 编辑:程序博客网 时间:2024/06/05 14:29

------------------------------android培训java培训期待与您交流! ----------------------------------------------------------------


 

读取文件Reader

java.lang.Object
  java.io.Reader
      java.io.InputStreamReader
          java.io.FileReader

 

 int

read()
          读取单个字符。

 int

read(char[] cbuf)
          将字符读入数组。

abstract  int

read(char[] cbuf, int off, int len)
          将字符读入数组的某一部分。

 int

read(CharBuffer target)
          试图将字符读入指定的字符缓冲区。

 

需求:用一次一个字符来读取硬盘中的文本文件数据

 

importjava.io.*;

 

class  FileReaderDemo

{

       public static void main(String[] args)throws IOException

       {

              //创建一个文件读取流对象,和指定名称的文件相关联。

              //要保证该文件是已经存在的,如果不存在,会发生异常FileNotFoundException

      FileReaderfr = new FileReader("demo.txt");


              //调用读取流对象的read方法。

              //read():一次读一个字符。而且会自动往下读。

             

                            while(true)

              {

                     int ch = fr.read();

                     if(ch==-1)

                            break;

                     System.out.println("ch="+(char)ch);

              }

      

              fr.close();

 

 

       }

}

问题: 为什么是-1,循环条件是:read()中有:

 

public int read()

         throws IOException

读取单个字符。在有可用字符、发生I/O 错误或者已到达流的末尾前,此方法一直阻塞。

用于支持高效的单字符输入的子类应重写此方法。

返回:

作为整数读取的字符,范围在 0 到 65535 之间 (0x00-0xffff),如果已到达流的末尾,则返回 -1

抛出:

IOException - 如果发生 I/O 错误

 

 

优化代码有:

mportjava.io.*;

 

class  FileReaderDemo

{

       public static void main(String[] args)throws IOException

       {

 

              FileReader fr = newFileReader("demo.txt");

                     int ch = 0;

 

              while((ch=fr.read())!=-1)

              {

                     System.out.println((char)ch);

                     fr.close();

              }

 

}

}

 

需求: 通过字符数组进行读取文本文件。

 

public int read(char[] cbuf)

         throws IOException

将字符读入数组。在某个输入可用、发生 I/O 错误或者已到达流的末尾前,此方法一直阻塞。

参数:

cbuf - 目标缓冲区

返回:

读取的字符数,如果已到达流的末尾,则返回 -1

抛出:

IOException - 如果发生 I/O 错误

 

 

importjava.io.*;

 

classFileReaderDemo2

{

       public static void main(String[] args)throws IOException

       {

              FileReader fr = newFileReader("demo.txt");

             

 

              //定义一个字符数组。用于存储读到字符。

              char[] buf = new char[1024];//此数组用作缓冲区.,1个字符有2个字节,1024个字符就是2048个字节,2048个字节就是2KB

             

              int num = 0;//作为标记.

              //该read(char[])返回的是读到字符个数。

              while((num=fr.read(buf))!=-1)//读到的数据放在数组里面.

              {

                     System.out.println(new String(buf,0,num));

              }

             

              fr.close();

       }

}

 

需求: C盘一个文本文件复制到D盘。

 

复制的原理:

其实就是将C盘下的文件数据先读取后写入到D盘的一个文件中。

 

步骤:

1,在D盘创建一个文件。用于存储C盘文件中的数据。

2,定义读取流和C盘文件关联。

3,通过不断的读写完成数据存储。

4,关闭资源。

 

 

import java.io.*;

 

class CopyText

{

       publicstatic void main(String[] args) throws IOException

       {

              copy_1();

       }

       //从C盘读一个字符,就往D盘写一个字符。

       publicstatic void copy_1()throws IOException

       {

              //创建目的地。

              FileWriterfw = new FileWriter("RuntimeDemo_copy.txt");

 

              //与已有文件关联。

              FileReaderfr = new FileReader("RuntimeDemo.java");

 

              intch = 0;

 

              while((ch=fr.read())!=-1)

              {

                     fw.write(ch);

              }

             

              fw.close();

              fr.close();

 

       }

}

 

缺点:速度慢,占用资源太多,

 

第二种方式:

 

public static void main(String[] args)throws IOException

       {

              copy_2();

       }

 

 

       publicstatic void copy_2()

       {

              FileWriter fw = null;

              FileReader fr = null;//在外面定义,try里引用,

              try

              {

                     fw= new FileWriter("SystemDemo_copy.txt");

                     fr = newFileReader("SystemDemo.java");

  char [] buf = new char [1024];

 

                     intlen = 0;//定义一个标记

                     while((len=fr.read(buf))!=-1)

                     {

                            fw.write(buf,0,len);

                     }

              }

              catch(IOException e)

              {

                     thrownew RuntimeException("读写失败");

 

              }

              finally

              {

                     if(fr!=null)

                            try

                            {

                                   fr.close();

                            }

                            catch(IOException e)

                            {

                            }

                     if(fw!=null)

                            try

                            {

                                   fw.close();

                            }

                            catch(IOException e)

                            {

                            }

              }

       }

}

 

分析: 1在代码中用到写入字符串的某一部分,char[1024];没有写满的时侯,写入一部分就可以了.

 2 关流时必须分开写.

void

write(char[] cbuf)
          写入字符数组。

abstract  void

write(char[] cbuf, int off, int len)
          写入字符数组的某一部分。

 void

write(int c)
          写入单个字符。

 void

write(String str)
          写入字符串。

 void

write(String str, int off, int len)
          
写入字符串的某一部分。

 

 

 

 

 

 









------------------------------android培训java培训期待与您交流! ----------------------------------------------------------------


原创粉丝点击