7.3 字节流 FileInputStream FileOutputStream

目录

字节输出流

字节输入流

使用字节流完成文件拷贝


字节输出流

字节输出流的超类:OutputStream

方法:

public void write();

public void flush();

public void close();

FileOutputStream 是其子类,是文件字节输出流;

构造函数:

public FileOutputStream(String name,append);//文件名,字符类型,append默认false

public FileOutputStream(File name,append);// 文件对象,append默认false

若append为true则是在文件内容里面追加数

在指定文件中写入一个字符,write()

FileOutputStream file = new FileOutputStream("E:\\java\\interface_test\\FileTest\\src\\File07\\file1.txt");
        file.write(97);
        file.close();

多字节写入write(byte[] b);

import java.io.*;

public class TestMun {
    public static void main(String[] args) throws IOException {
        String path = "E:\\java\\interface_test\\FileTest\\src\\File07\\file1.txt";
        FileOutputStream outStream = new FileOutputStream(new File(path));
        byte[] b = "啊热有你好haha".getBytes();
        outStream.write(b);

    }
}

write(byte[] b, int offset,int lenth)

import java.io.*;

public class TestMun {
    public static void main(String[] args) throws IOException {
        String path = "E:\\java\\interface_test\\FileTest\\src\\File07\\file1.txt";
        FileOutputStream outStream = new FileOutputStream(new File(path));
        String names= "fdhkfd";
        outStream.write(names.getBytes(),1,2);

    }
}

数据追加写

public static void main(String[] args) throws IOException {
        String path = "E:\\java\\interface_test\\FileTest\\src\\File07\\file1.txt";
        FileOutputStream outStream = new FileOutputStream(new File(path), true);
        outStream.write("你好吗?\n".getBytes());
        outStream.write("imnotgood".getBytes());

    }
//你好吗?
//imnotgood

字节输入流

FileInputStream 是InputStream类的子类,文件字节输入流,把文件中的数据读取到内存中

构造:

public FileOutputStream(String name);//文件名,字符类型,

public FileOutputStream(File name);// 文件对象,

方法:

public in read(byte[] b) 读取一个字节,返回读取长度,读到文件末尾返回-1

public int read(byte[] b, int off, int len) 读取多个字节

read()读取一个字节

import java.io.*;

public class TestMun {
    public static void main(String[] args) throws IOException {
        String path = "E:\\java\\interface_test\\FileTest\\src\\File07\\file1.txt";
        FileInputStream fis = new FileInputStream(path);
        int len;
        while( (len= fis.read())!=-1)
            System.out.println((char)len);
        fis.close();
    }
}

read(byte[] b,int off,int len) 读取的数据放入缓冲区b,返回实际读入字节数,若b长度为0,返回0,

字节数组转字符串, new String(byte[] b);

import java.io.*;

public class TestMun {
    public static void main(String[] args) throws IOException {
        String path = "E:\\java\\interface_test\\FileTest\\src\\File07\\file1.txt";
        FileInputStream fis = new FileInputStream(path);
        while(true){
            byte[] b = new byte[2];
            int len = fis.read(b);
            if(len==-1)
                break;
            else
                System.out.print(new String(b));
        }
    }
}

使用字节流完成文件拷贝

import java.io.*;

public class TestMun {
    public static void main(String[] args) throws IOException {
        String path = "E:\\java\\interface_test\\FileTest\\src\\File07\\file1.txt";
        File file1 = new File(path);
        // 创建备份文件名字
        String newPath = file1.getParent() + File.separator + file1.getName().split("\\.")[0]+"copy.text";
        // 打开需要备份的文件和新文件
        FileInputStream in = new FileInputStream(path);
        FileOutputStream out = new FileOutputStream(newPath,true);
        int len = 0;
        while((len = in.read())!=-1){
            out.write(len);
        }
        in.close();
        out.close();


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值