java IO流学习笔记

IO的分类:

分类一:输入流,输出流

分类二:字节流,字符流

分类三:节点流,处理流

IO中的核心类

Inputsream:输入流   字节流的父类    抽象类  用来读int read(byte[] int off,int len);

OutPutStream:输出流字节流的父类  抽象类    用来写 int write(byte[] int off,int len);

FileinputStream:文件输入流,inputstream的子类

FileOutputStream 文件输出流 OutPutStream的子类

如何确定IO:

Java的IO流是以java程序作为参照物的。流入java程序的叫做Inputsream,从java程序流出的叫做OutPutStream。

使用FileinputStreamFileOutputStream读写小文件.

package com.ayit.io;

 

import java.io.FileInputStream;

import java.io.FileOutputStream;

 

public class IOTest {

       publicstatic void main(String[] args) {

              //定义一个文件输入流

              FileInputStreamfis = null;

              //定义一个文件输出流

              FileOutputStreamfos = null;

              try{

                     //获取文件输入流对象

                     fis= new FileInputStream("d:/from.txt");

                     //定义一个临时存放的数组

                     byte[]buffer = new byte[100];

                     //读取文件里的内容,并获取读取的长度

                     inttemp = fis.read(buffer, 0, buffer.length);

                     //获取文件输入流对象

                     fos= new FileOutputStream("d:/to.txt");

                     //把获取到的数组写入文件中

                     fos.write(buffer,0, temp);

              }catch (Exception e) {

                     //TODO: handle exception

              }

       }

 

}

 

 

大文件的读写方法:

当一个文件特别大的时候,在内存中不足以建立那么大的空间,就需要用循环来读取该文件。定义temp = fis.read(buffer,0,buffer.length);当文件被读取完的时候,temp的值就会变成-1,否则就是读取到byte的长度。

package com.ayit.io;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

public class BigFile {

    public static void main(String[] args) {

       FileInputStream fis = null;

       // 定义一个文件输出流

       FileOutputStream fos = null;

       try {

           // 获取文件输入流对象

           fis = new FileInputStream("d:/from.txt");

           // 获取文件输入流对象

           fos = new FileOutputStream("d:/to.txt");

           // 定义一个临时存放的数组

           byte[] buffer = new byte[1024];

           //循环读写文件

           while(true){

              // 读取文件里的内容,并获取读取的长度

              int temp = fis.read(buffer, 0, buffer.length);

              //如果已经读取完了,跳出当前循环

              if(temp == -1){

                  break;

              }

              //把读取的数据写入文件中

              fos.write(buffer, 0,temp);

           }

 

       } catch (Exception e) {

           // TODO: handleexception

       }finally{

           //关闭IO

           try {

              fis.close();

              fos.close();

           } catch (IOException e) {

              // TODO Auto-generatedcatch block

              e.printStackTrace();

              throw new RuntimeException("文件读写流关闭失败");

           }

 

       }

    }

 

}

流在使用结束时,必须流进行手动关闭,一般写在finall{}代码块中,关闭流会产生异常,还必须对异常进行捕获处理。

 

字符流的使用。

字符流:在读写文件时都是以字符为基础的。

所有的字节流都是Reader的子类。           FileReader  int read(char [] c,int off,int len);

所有的字节输出流都是Writer的子类。            FileWriter  void write(char [] c,int off,int len);

package com.ayit.io;

 

import java.io.FileReader;

import java.io.FileWriter;

 

public class TestChar {

    public static void main(String[] args) {

       //声明字节读取流

       FileReader fr = null;

       //声明字节写流

       FileWriter fw = null;

       try {

           //获取字符读取对象

           fr  = new FileReader("d:/from.txt");

           //获取字符写对象

           fw = new FileWriter("d:/to.txt");

           //定义临时存放的字符区域

           char c [] = new char[100];

           //读取文件,并获取长度

           int temp = fr.read(c, 0, c.length);

           //写文件

           fw.write(c,0,temp);

       } catch (Exception e) {

           // TODO: handleexception

           System.out.println(e);

       }finally{

           try {

              //关闭流

              fw.close();

              fr.close();

           } catch (Exception e2) {

              // TODO: handleexception

           }

       }

    }

}

字符流读取大文件;

public class TestBigChar {

    public static void main(String[] args) {

       FileReader fr = null;

       FileWriter fw = null;

       try {

           fr = new FileReader("D:/from.txt");

           fw = new FileWriter("D:/to.txt");

           char [] c = new char[1024];

           while(true){

              int temp = fr.read(c, 0, c.length);

              if(temp==-1){

                  break;

              }

              fw.write(c, 0, temp);

           }

       } catch (Exception e) {

           // TODO: handleexception

       }finally{

           try {

              fr.close();

              fw.close();

           } catch (Exception e2) {

              // TODO: handleexception

           }

 

       }

    }

}

注意,在读取汉字时,如果保存的不是ansi格式编码,会出现乱码的问题。

 

 

 

装饰者设计模式:

节点流,

处理流:在节点流的数据上进行进一步的处理。

BufferedReader

readLine();  :可以一次读取一行数据。

package com.ayit.io;

import java.io.BufferedReader;

import java.io.FileReader;

 

public class Buff {

    public static void main(String[] args) {

       FileReader fr = null;

       BufferedReader in = null;

       try {

           //获取文件读取流对象

           fr = new FileReader("d:/from.txt");

           //讲读取流作为参数传给处理流

           in = new BufferedReader(fr);

           String line = null;

           while(true){

              line = in.readLine();

              if(line == null ){

                  break;

              }

              System.out.println(line);

           }

       } catch (Exception e) {

           // TODO: handleexception

       }finally{

           try {

              fr.close();

              in.close();

           } catch (Exception e2) {

              // TODO: handleexception

           }

       }

    }

}

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值