06 IO流 02FileOutputStream的使用 + 拷贝文件 + FileReader的使用 + FileWriter的使用 + 拷贝普通文件
1、FileOutputStream的使用
FileOutputStream fos = null; try { //myfile文件不在的时候,会自动新建 //这种构造方法方式谨慎使用,会先将源文件清空,然后写入 fos = new FileOutputStream("myfile"); //以追加的方式,在文件末尾加入,不会清空原文件内容 fos = new FileOutputStream("myfile",true); byte[] bytes = {97,98,99,100}; fos.write(bytes); //文件没有回创建新文件,写进去“abcd" fos.write(bytes,0,2); //将byte数组的一部分写出,写进去的是"ab" //字符串 String s = "我是一个中国人,我骄傲"; //将字符串转换成byte数组 byte[] bs = s.getBytes(); //写 fos.write(bs); //写完之后,最后一定要刷新 fos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } }
2、拷贝文件
/* * 使用FileInputStream + FileOutputStream 完成文件的拷贝 * 拷贝的过程是一边读,一边写 * 使用 以上的字节流拷贝文件的时候,文件类型随意,万能的,什么样的文件都能拷贝 * * * */ FileInputStream fis = null; FileOutputStream fos = null; try { //创建一个输入流对象 fis = new FileInputStream("D:\\java_base\\day19课堂笔记.txt"); //创建一个输出流对象 fos = new FileOutputStream("D:\\java_base\\newfile.txt"); //最核心的是一边读,一边写 byte[] bytes = new byte[1024*1024]; //1MB(一次最多拷贝1MB) int readCount = 0; while((readCount = fis.read(bytes)) != -1){ fos.write(bytes,0,readCount); } //刷新,输出流最后要刷新 fos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ //分开try,不要一起try //一起try,可能会影响另一个流的关闭 if(fos != null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if(fis != null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } }
3、FileReader的使用
/* * FileReader * 文件字符输入流,只能读取普通文本 * 读取文本内容,比较方便,快捷 * * * */ FileReader reader= null; try { //创建文件字符输入流 reader = new FileReader("tempfile"); //开始读 char[] chars = new char[4]; //一次读取四个字符 int readCount = 0; while((readCount = reader.read(chars)) != -1){ System.out.println(new String(chars,0,readCount)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(reader != null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
4、FileWriter的使用
/* * FileWriter * 文件字符输入流,只能写入普通文本 * 写入文本内容,比较方便,快捷 * * * */ FileWriter out = null; try { //创建文件字符输出流对象 true是每次在文件后面添加元素 没加true需要清空原文件 out = new FileWriter("file",true); //开始写 char[] chars = {'我','是','中','国','人'}; out.write(chars); out.write(chars,0,2); out.write("我是一名java软件工程师"); out.write("\n"); out.write("hello world"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(out != null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }
5、拷贝普通文件
/* * 使用FileReader 和 FileWriter 拷贝文件 * * * */ FileReader in = null; FileWriter out = null; try { //读 in = new FileReader("D:\\java_base\\day19课堂笔记.txt"); //写 out = new FileWriter("copy02.txt"); //一边读一边写 char[] chars = new char[1024 * 512]; //1MB int readCount = 0; while((readCount = in.read(chars)) != -1){ out.write(chars,0,readCount); } //flush out.flush(); } catch (IOException e) { e.printStackTrace(); }finally { if(in != null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if(out != null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }