IO流基础

 

目录

 1.FileOutPutStream字节输入流

1.1FileOutPutStream使用

1.1.1创建对象      FileOutPutStream  fos = new  FileOutPutStream("路径或者File对象");

1.1.2.写数据         调用write方法,参数是int类型,但传入文件中是ascii值对应的字符   fos.write(97); 

1.1.3.释放资源             fos.close();     

清空文件演示

​编辑

1.2.FileOutPutStream写数据的三种方式

1.3.FileOutPutStream写数据的续写(不清空)

2.FileInPutStream字节输出流

2.1FileInPutStream使用

几乎和FileOutPutStream一样,只是第二步调用read()方法即可,并且该方法只能读取单个数据

 2.2FileInPutStream循环读入多个数据

2.3文件拷贝

2.4获取当前时间

3.字符流

3.文件拷贝代码

序列化流和反序列流

打印流

字节打印流

字符打印流


 

 

 1.FileOutPutStream字节输入流

字节流读取中文会乱码,但是拷贝中文文件不会乱码

1.1FileOutPutStream使用

使用:

1.1.1创建对象      FileOutPutStream  fos = new  FileOutPutStream("路径或者File对象");

不存在就创建,如果存在就会清空文件

1.1.2.写数据         调用write方法,参数是int类型,但传入文件中是ascii值对应的字符   fos.write(97); 

1.1.3.释放资源             fos.close();     

清空文件演示

       


public class FOPdemo {
    public static void main(String[] args) throws IOException {
        FileOutputStream  fos = new FileOutputStream("D:\\io\\src\\a.txt");
        fos.write(97);
        fos.close();



    }

}

1.2.FileOutPutStream写数据的三种方式

write也有对应的三种形式使用哪种参数形式,就要调用相应的write方法

其中off是起始索引,len参数指的是个数

1.3.FileOutPutStream写数据的续写(不清空)

在路径后还有一个参数,写上true即可


2.FileInPutStream字节输出流

2.1FileInPutStream使用

几乎和FileOutPutStream一样,只是第二步调用read()方法即可,并且该方法只能读取单个数据

 2.2FileInPutStream循环读入多个数据

sout(new string(str))        该语句将str转换成字符 

sout(new string(str,0,len))  该语句将str数组中0~len的元素都转换成字符 

2.3文件拷贝

FIS  fis = new FIS( "文件A的路径 " );

Fos  fos = new FOS( "文件B的路径 " );

byte   [ ]   bytes = new   byte[1024*1024*5] ;

while((len = fis.read())!=-1)

{

        fos.write(bytes,0,len);//这个0,len很重要,能避免出现不想看到的结果
}

fos.close();

fis.close();

2.4获取当前时间

long  start  = System.CurrentTimeMillis();

2.5解决乱码

不要用字节流读取文本文件

编码解码时使用同一个码表,同一个编码方式

3.字符流

 

 注意这里的read形参是char数组型


如果要输入中文到文件中,只能用字符输入流,因为字节流只能操作一个字节,中文的值肯定超过一个字节,所以需要用FileWriter,

3.文件拷贝代码

普通字节流,将文件夹里的文件和文件夹全部拷贝

package bao;

import java.io.*;

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

//拷贝文件夹,D\io\src    ,拷贝到D\io\dest

      //1.创建对象表示数据源
        File src = new File("D:\\io\\src");


        //2.创建对象表示目的地
        File dest = new File("D:\\io\\dest");

        //3。调用方法开始拷贝
        copydir(src,dest);



    }
//方法作用:拷贝文件夹
    private static void copydir(File src, File dest) throws IOException {

        dest.mkdir();
        //如果文件里有其他文件夹,要递归
        //1.进入数据源
        File [] file = src.listFiles();
        //2.遍历数组
        for (File  a : file) {
            if(a.isFile())//如果是文件,就拷贝
            {
                FileInputStream fis = new FileInputStream(a);//要拷贝的文件
                FileOutputStream fos = new FileOutputStream(new File(dest,a.getName())); //拷贝的目的地
                //定义一个byte类型数组储存文件
                byte[] bytes = new byte[1024];
                int len ;
                while((len = fis.read(bytes))!=-1){
                    fos.write(bytes,0,len);
                }
                fos.close();
                fis.close();


            }else{
                copydir(a,new File(dest,a.getName()));

            }




        }
        //3.如果是文件,拷贝
        //4.如果是文件夹,递归







    }

}

 缓冲字节流


public class FOPdemo {
    public static void main(String[] args) throws IOException {
//        FileOutputStream  fos = new FileOutputStream("D:\\io\\src\\a.txt");
//        fos.write(97);
//        fos.close();

        //字节缓冲流
       // 1.创建字节缓冲流对象,在参数里创建普通字节流
        BufferedInputStream  bis = new BufferedInputStream(new FileInputStream("D:\\io\\src\\a.txt"));
        BufferedOutputStream  bos = new BufferedOutputStream(new FileOutputStream("D:\\io\\src\\copya.txt"));

        int len;
        byte [] b = new byte [1024*1024*5];
        while((len = bis.read(b))!=-1)
        {
            bos.write(b,0,len);
        }
        bos.close();
        bis.close();

序列化流和反序列流

可以把java中的对象写到文件中,而且看不懂,需要用反序列流读出

 //把一个对象写入文件

        //1.创建对象
        Student  stu = new Student("zhangsan",23);


        //2.船舰序列化对象
        ObjectOutputStream  oos = new ObjectOutputStream(new FileOutputStream("D:\\io\\src\\aaa.txt"));


        //3.写出数据
        oos.writeObject(stu);


        //4.释放资源
        oos.close();
        
        

在文件中查看 

反序列流将其读出

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\io\\src\\aaa.txt"));

        Object o = ois.readObject();

        System.out.println(o);
          
        ois.close();

打印流

只能将数据传入文件

字节打印流

PrintStream ps = new PrintStream(new FileOutputStream("D:\\io\\src\\a.txt"),true);

字符打印流

PrintStream ps = new PrintStream(new FileWriter("D:\\io\\src\\a.txt"),true);

只是在括号里new  的时候一个是FileOutputStream,一个是 FileWriter

同样的,会清空文件原有的数据,相当于不管之前有没有这个文件,都新开一个文件。

               

  PrintStream ps = new PrintStream(new FileOutputStream("D:\\io\\src\\a.txt"));
        ps.println("s");
        ps.println("s");
        ps.printf("%s是一个%s","小妹","女的");
        ps.close();

     结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值