java FileOutputStream输出流的使用解读
目录
- FileOutputStream的构造方法
- 场景1
- 场景2
- 场景3
- FileOutputStream的写入方法
- 例1
- 例2
- 例3
- 例4
- 总结
FileOutputStream的构造方法
FileOutputStream提供了4个常用构造方法,用于实例化FileOutputStream对象,
不同的场景使用不同的构造方法。
场景1
使用File对象打开本地文件,从文件读取数据。
public FileOutputStream(File file) throws FileNotFoundException{}
查看底层源码发现该构造方法实际是调用了另一个构造方法
public FileOutputStream(File file) throws FileNotFoundExcept开发者_C培训ion { this(file, false); }
场景2
不使用File对象,直接传入文件路径。
public FileOutputStream(String name) throws FileNotFoundException{}
FileOutputStream的构造方法允许直接传入文件路径,而无须使用File对象。查看该构造方法的源代码,其内部使用了File对象打开文件。
场景3
打开文件,在文件的尾部追加写入数据。
场景要求在文件的尾部写入数据,由于前面两个构造函数都是文件开始写入数据(覆盖原文件),因此不能使用前面2个场景的构造函数。FileOutputStream提供了另外两个构构造方法,分别是:
public FileOutputStream(File file,boolean append) throws FileNotFoundException{}
public FileOutputStream(String name,boolean append) throws FileNotFoundException{}
同前面的构造方法相比,这两个构造方法各多了一个boolean参数append。
append参数为true时,数据从文件尾部写入;append参数为false时,数据覆盖原文件。
这也是第一个方法调用的那个方法
FileOutputStream的写入方法
FileOutputStream类提供了多种文件写入方法,可以单独写一个字节到文件,
也可以写一个byte数组到文件,也可以取byte数组的部分数据写入到文件。
例1
使用write(int b)方法写入文件。
package com.demo;
import Java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo {
  public static void main(String[] args) throws IOException {
    File file = new File("d://new.txt");
    try {
      file.createNewFile();
    } catch (IOException e) {
      e.printStackTrace();
    }
    try {
      FileOutputStream fileOutputStream = new FileOutputStream(file);
      String str = "this is new file";
      for (int i = 0; i < str.length(); i++) {
        int b = (int)str.charAt(i);
        fileOutputStream.write(b);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }
}
例子程序首先调用File类的createNewFile()创建new.txt文件,然后将str内容写入到新创建的new.txt文件中。
例2
使用write(byte[] b)方法写入文件。
write(byte[] b)方法用于将b.length个字节从指定的byte数组写入到输出流。
String类的getBytes()方法可以将字符串转换为byte数组,使用FileOutputStream 类的write(byte[] b)方法,将转换的byte数组写入文件。
package com.demo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo {
  public static void main(String[] args) throws IOException {
    File file = new File("d://new.txt");
    try {
      file.createNewFile();
    } catch (IOException e) {
      e.printStackTrace();
    }
    try {
      FileOutputStream fileOutputStream = new FileOutputStream(file);
      String str = "this is new file";
      fileOutputStream.write(str.getBytes());
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }
}
例3
使用write(byte[] b,int off,int len)方法写入文件。
该方法将len个字节的数据,并从数组b的off位置开始写入到输出流。
package com.demo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo {
  public static void main(String[] args) throws IOException {
    File file = new File("d://new.txt");
    try {
      file.createNewFile();
    } catch (IOException e) {
      e.printStackTrace();
    }
    try {
      FileOutputStream fileOutputStream = new FileOutputStream(file);
      String str = "this is new file";
      fileOutputStream.write(str.getBytes(),5,11);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }
}
程序把指定的str内容写入到文件,fos.write(str.getBytes(),5,10)语句的第一个参数为byte数组,第二个参数5是从byte数组的下标5开始,第三个参数是写入的字节数。程序执行后,写入的内容为“is new file”。
使用该方法一定要注意数组越界的问题。例如,byte数组长http://www.devze.com度为20,从第下标12开始,写入15个字节到文件,编程就会造成数组越界,程序报错。
例4
使用FileOutputStream复制文件
package com.demo;
import java.io.*;
public class Demo {
  public static void main(String[] args) {
    File sourcbcBWShKtZze = new File("d://new.txt");
    File dest = new File("d://new2.txt");
    copy(source,dest);
  }
  public static void copy(File sor, File dest){
    if(!sor.exists()){
      System.out.println("源文件不存在");
      return;
    }
    try {
      FileInputStream in = new FileInputStream(sor);
      FileOutputStream out = new FileOutputStream(dest);
      byte[] buf = new byte[in.available()];
      in.read(buf);
      out.write(bubcBWShKtZzf);
      out.flush();
      out.close();
      in.close();
    } catch (FileNotFoundException e) {
javascript      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
复制文件是将源文件数据写入到新文件,在实际编程中,实现文件的复制有很多种方法,本案例使用FileInputStream和FileOutputStream实现文件的复制。
java中的IO流中的输出流一般都有flush这个操作,这个操作的作用是强制将缓存中的输出流(字节流,字符流等)强制输出。
为什么会有这么个方法啊?
因为输出流在进行输出时,比如像某个文件中写入内容,其实是先将输出流写入到缓冲区,当缓冲区写满后才将缓冲区的内容输出到文件中。但是当主机完成输出流的输出后,有可能缓冲区这个时候还没有被填满,这
样的话,就会一直等待主机发送内容,这时候,就可以使用flush将缓冲区的内容强制输出到文件中,清空缓冲区。
所以,一般在关闭输出流之前,要先调用flush方法强制缓冲区中的内容输出,并清空缓冲区。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
 
         
       
       
       
       
       
       加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论