黑马程序员-IO流

IO流

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

IO流常用类

字节流的抽象基类:

         InputStreamOutputStream

字符流的抽象基类:

ReaderWrite

注:由这四个类派生出来的子类名称都是以其父类名作为类名的后缀,如:InputStream的子类FileInputStreamReader的子类FileReader

 

Writer 字符的写入

Reader 字符的读取

第一种方式单个字符读取

publicclass ReaderTest {

      publicstaticvoid main(String[] args) {

       //Reader类读取文件,利用他的子类FileReader

       FileReader fr= null;

       try

       {

           fr=new FileReader("D:\\test.txt");

        int ch=0;

           while((ch=fr.read())!=-1)//当读取文件到末尾是将返回-1

           {

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

           }

       }

       catch(IOException e)

       {

           System.out.println(e.toString());

       }

       finally

       {

           try

           {

              fr.close();

           } catch (IOException e)

           {

              e.printStackTrace();

           }

       }

    }

}


 

读取的第二种方式

是将字符读入到以定义的数组中

publicclass ArrayReader {

    publicstaticvoid main(String[] args) {  

       FileReader  fr =null;

       try

       {

           fr=new FileReader("D:\\demo.txt");

            char[] ch=newchar[4]; //创建一个数组,用于存储读取出的字符

            int i=0;

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

            {

               System.out.print(new String(ch,0,i));

            } 

       }

       catch(FileNotFoundException e)

       {

           e.printStackTrace();

       } catch (IOException e)

{

           e.printStackTrace();

       }

       finally

       {

           try

           {

              fr.close();

              System.out.println("\nover");

           }

           catch(IOException e)

           {

              e.printStackTrace();

           }

       }

    }

}


 

 

拷贝文件

publicclassCopyFile {

 

    publicstaticvoid main(String[] args) {    

       String str1="D:\\test.txt";

       String str2="D:\\test1.txt";  

           copy(str1,str2);                      //参数需要两个String类型的完整地址 

    }

    publicstaticvoid copy(String reader,String writer)

    {

       BufferedReader br=null;                     //创建一个BufferedReader引用,应用缓冲提高效率

       BufferedWriter bw=null;

       try

       {

           br=new BufferedReader(new FileReader(reader));//创建BufferedReader对象

           bw=new BufferedWriter(new FileWriter(writer));

           String str=null;

           while((str=br.readLine())!=null)         //判断是否读到文本末尾

           {

              bw.write(str);                        //写入

              bw.newLine();

              bw.flush();

           }

           System.out.println("copy已完成,文件在"+writer);

       }

       catch(IOException e)

       {      

           thrownew RuntimeException("copy失败!!");

       }

       finally

       {

           if(br!=null)

           try

           {

              br.close();                           //关闭字符流

           }

           catch(IOException e)

           {

              e.printStackTrace();

           }

           if(bw!=null)

           try

           {

              bw.close();

           }

           catch(IOException e)

           {

              e.printStackTrace();

           }

        }

    }

}


 

 

字符流的缓冲区

对应类:

         BufferedWriter   BufferReader

缓冲区要结合流才可使用,

在流的基础上对流的功能进行了增强。

 

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值