Java FileInputStream、FileOutputStream的使用(文件输入输出流、字节流)

FileInputStream、FileOutputStream的使用(文件输入输出流、字节流)

1.FileInputStream

FileInputStream官方api说明:

public class FileInputStream
extends InputStream

A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment.

FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

FileInputStream从文件系统中的文件获取输入字节。
FileInputStream用于读取原始字节流,例如图像数据。要读取字符流,请考虑使用FileReader。

FileInputStream提供了三个构造函数

   //以文件路径为参数
	public FileInputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null);
    }
	
	//以File对象为参数
    public FileInputStream(File file) throws FileNotFoundException {
        String name = (file != null ? file.getPath() : null);
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkRead(name);
        }
        if (name == null) {
            throw new NullPointerException();
        }
        if (file.isInvalid()) {
            throw new FileNotFoundException("Invalid file path");
        }
        fd = new FileDescriptor();
        fd.attach(this);
        path = name;
        open(name);
    }

 	public FileInputStream(FileDescriptor fdObj) {
        SecurityManager security = System.getSecurityManager();
        if (fdObj == null) {
            throw new NullPointerException();
        }
        if (security != null) {
            security.checkRead(fdObj);
        }
        fd = fdObj;
        path = null;

        /*
         * FileDescriptor is being shared by streams.
         * Register this stream with FileDescriptor tracker.
         */
        fd.attach(this);
    }

所以我们可以有三种方式创建FileInputStream实例

    
		File file = new File("D:\\javaStudy\\src\\File\\text.txt");
        if (!file.exists()){
            file.createNewFile();
        }
  		 //以File对象为参数
        FileInputStream fileInputStream1 = new FileInputStream(file);
		//以文件路径为参数
		FileInputStream fileInputStream2 = new FileInputStream("D:\\javaStudy\\src\\File\\text.txt");
		//以FileDescriptor对象为参数,可直接在控制台输入字符串作为文件内容
		FileInputStream fileInputStream2 = new FileInputStream(FileDescriptor.in);

读取文件

		File file = new File("D:\\javaStudy\\src\\File\\text.txt");
        if (!file.exists()){
       		 System.out.println("文件不存在");
            //创建新文件
             file.createNewFile();
        }
  		 //以File对象为参数
        FileInputStream in = new FileInputStream(file);	
		//将读取到的内容存到byte[]中
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = in.read(bytes))!=-1){
			 System.out.println("读取到的内容"+bytes);
        }

2.FileOutputStream

FileOutputStream官方说明:

public class FileOutputStream
extends OutputStream

A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.

FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.

文件输出流是用于将数据写入文件或FileDescriptor的输出流。文件是否可用或是否可以创建取决于底层平台。特别是某些平台,一次只允许一个FileOutputStream(或其他文件写入对象)打开一个文件进行写入。在这种情况下,如果所涉及的文件已经打开,则此类中的构造函数将失败。 FileOutputStream用于写入原始字节流,例如图像数据。要编写字符流,请考虑使用FileWriter。

FileOutputStream构造函数大体上和FileInputStream差不多 ,多出了两个,append为true的情况下指在原文件下追加内容,不覆盖。

    public FileOutputStream(File file, boolean append)
        throws FileNotFoundException
    {
        String name = (file != null ? file.getPath() : null);
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkWrite(name);
        }
        if (name == null) {
            throw new NullPointerException();
        }
        if (file.isInvalid()) {
            throw new FileNotFoundException("Invalid file path");
        }
        this.fd = new FileDescriptor();
        fd.attach(this);
        this.append = append;
        this.path = name;

        open(name, append);
    }

    public FileOutputStream(String name, boolean append)
        throws FileNotFoundException
    {
        this(name != null ? new File(name) : null, append);
    }

没有append参数的构造函数默认append为false,默认覆盖原文件内容

    public FileOutputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null, false);
    }

    public FileOutputStream(File file) throws FileNotFoundException {
        this(file, false);
    }

像文件中写入内容

    //源码
    public void write(byte b[]) throws IOException {
        writeBytes(b, 0, b.length, append);
    }

    public void write(byte b[], int off, int len) throws IOException {
        writeBytes(b, off, len, append);
    }

    public void write(int b) throws IOException {
        write(b, append);
    }

简单例子

//获取读取的文件
File file = new File("D:\\javaStudy\\src\\File\\text.txt");
//判断文件是否存在
if (!file.exists()){
    //新建文件
    file.createNewFile();
}

FileInputStream in = new FileInputStream(file);
//保存文件的流,覆盖的方式保存
FileOutputStream out = new FileOutputStream("D:\\javaStudy\\src\\File\\save.txt");
//追加的方式:FileOutputStream out = new FileOutputStream("D:\\javaStudy\\src\\File\\save.txt",true);

//用于保存读取的内容
byte[] bytes = new byte[1024];
int len = 0;
//读取文件内容
while ((len = in.read(bytes))!=-1){
    //向文件中写入数据
    out.write(bytes);
}
    in.close();
    out.close();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值