* 如何实现数据的追加写入?
*
* Windows:
* Linux:
* Mac:
import java.io.FileOutputStream; import java.io.IOException; /* * 如何实现数据的追加写入? * * Windows: * Linux: * Mac: * */ public class IntegerDemo { public static void main(String[] args) throws IOException { // 创建字节输出流对象 FileOutputStream fos = new FileOutputStream("E:\zikao\file\cs.txt", true); // 写数据 for (int i = 0; i < 10; i++) { fos.write(("hello" + i).getBytes()); fos.write((" ").getBytes()); } // 释放资源 fos.close(); } }
输出流加入异常处理
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class IntegerDemo { public static void main(String[] args) { FileOutputStream fos = null; try { // 创建字节输出流对象 fos = new FileOutputStream("E:\zikao\file\cs.txt"); // 写数据 fos.write("java".getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 释放资源 if (fos != null) { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
FileInputStream读取数据
import java.io.FileInputStream; import java.io.IOException; public class IntegerDemo { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("E:\zikao\file\cs.txt"); int by = 0; while ((by = fis.read()) != -1) { System.out.print((char) by); } } }
字节流赋值文本文件
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class IntegerDemo { public static void main(String[] args) throws IOException { // 封装数据源 FileInputStream fis = new FileInputStream("E:\zikao\file\cs.txt"); FileOutputStream fos = new FileOutputStream("E:\zikao\file\cs1.txt"); int by = 0; // 写数据 while ((by = fis.read()) != -1) { fos.write(by); } // 释放资源 fis.close(); fos.close(); } }
BufferedInputStream类
第一种方法:
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; public class IntegerDemo { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("E:\zikao\file\cs.txt"); BufferedInputStream bis = new BufferedInputStream(fis); // 读取数据 int by = 0; while ((by = fis.read()) != -1) { System.out.print((char) by); } // 关闭资源 bis.close(); } }
第二种方法:
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; public class IntegerDemo { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("E:\zikao\file\cs.txt"); BufferedInputStream bis = new BufferedInputStream(fis); // 读取数据 byte bys[] = new byte[1024]; int len = 0; while ((len = bis.read(bys)) != -1) { System.out.print(new String(bys, 0, len)); } // 关闭资源 bis.close(); } }
BufferedOutputStream类
* 通过定义的方式确实比以前一次读取一个字节的方式快很多,所以,看了有一个缓冲区还是非常好的
* 既然是这样的话,那么Java开始在设计的时候,也考虑到了这个问题,就专门提供了带缓冲区的字节类
* 这种类被称为:缓冲区类(高效类)
* 写数据:BufferedOutputStream
* 读数据:BufferedInputStream
import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; /* * 通过定义的方式确实比以前一次读取一个字节的方式快很多,所以,看了有一个缓冲区还是非常好的 * 既然是这样的话,那么Java开始在设计的时候,也考虑到了这个问题,就专门提供了带缓冲区的字节类 * 这种类被称为:缓冲区类(高效类) * 写数据:BufferedOutputStream * 读数据:BufferedInputStream * */ public class IntegerDemo { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("E:\zikao\file\cs.txt"); BufferedOutputStream bos = new BufferedOutputStream(fos); // 写数据 bos.write("hello".getBytes()); // 关闭资源 bos.close(); } }
* 字节流4种方式复制文件:
*
* 基本字节流一次读写一个字节
* 基本字节流一次读写一个字节数组
* 高效字节流一次读写一个字节
* 高效字节流一次读写一个字节数组
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* * 字节流4种方式复制文件: * * 基本字节流一次读写一个字节 * 基本字节流一次读写一个字节数组 * 高效字节流一次读写一个字节 * 高效字节流一次读写一个字节数组 * */ public class IntegerDemo { public static void main(String[] args) throws IOException { method1("E:\zikao\20151130054234629.xls", "E:\zikao\file\1.xls"); method2("E:\zikao\20151130054234629.xls", "E:\zikao\file\2.xls"); method3("E:\zikao\20151130054234629.xls", "E:\zikao\file\3.xls"); method4("E:\zikao\20151130054234629.xls", "E:\zikao\file\4.xls"); } // 基本字节流一次读写一个字节 public static void method1(String srcString, String destString) throws IOException { long start = System.currentTimeMillis(); FileInputStream fis = new FileInputStream(srcString); FileOutputStream fos = new FileOutputStream(destString); int ch; while ((ch = fis.read()) != -1) { fos.write(ch); } fos.close(); fis.close(); long end = System.currentTimeMillis(); System.out.println("共耗时:" + (end - start) + "毫秒"); } // 基本字节流一次读写一个字节数组 public static void method2(String srcString, String destString) throws IOException { long start = System.currentTimeMillis(); FileInputStream fis = new FileInputStream(srcString); FileOutputStream fos = new FileOutputStream(destString); byte bys[] = new byte[1024]; int len = 0; while ((len = fis.read(bys)) != -1) { fos.write(bys, 0, len); } fos.close(); fis.close(); long end = System.currentTimeMillis(); System.out.println("共耗时:" + (end - start) + "毫秒"); } // 高效字节流一次读写一个字节 public static void method3(String srcString, String destString) throws IOException { long start = System.currentTimeMillis(); FileInputStream fis = new FileInputStream(srcString); FileOutputStream fos = new FileOutputStream(destString); BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); int ch; while ((ch = bis.read()) != -1) { bos.write(ch); } bos.close(); bis.close(); long end = System.currentTimeMillis(); System.out.println("共耗时:" + (end - start) + "毫秒"); } // 高效字节流一次读写一个字节数组 public static void method4(String srcString, String destString) throws IOException { long start = System.currentTimeMillis(); FileInputStream fis = new FileInputStream(srcString); FileOutputStream fos = new FileOutputStream(destString); BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); byte bys[] = new byte[1024]; int len = 0; while ((len = bis.read(bys)) != -1) { bos.write(bys, 0, len); } bos.close(); bis.close(); long end = System.currentTimeMillis(); System.out.println("共耗时:" + (end - start) + "毫秒"); } }
要求:复制单极文件夹
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* * 要求:复制单极文件夹 * */ public class IntegerDemo { public static void main(String[] args) throws IOException { // 封装目录 File srcFolder = new File("E:\zikao\file"); // 封装目的地 File destFolder = new File("E:\zikao\test"); // 如果目的地文件夹不存在,就创建 if (!destFolder.exists()) { destFolder.mkdirs(); } // 获取该目录下的所有文本的File数组 File fileArray[] = srcFolder.listFiles(); // 遍历该File数组,得到每一个File对象 for (File file : fileArray) { String name = file.getName(); File newFile = new File(destFolder, name); copyFile(destFolder, newFile); } } public static void copyFile(File file, File newFile) throws IOException { FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(newFile); BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); byte bys[] = new byte[1024]; int len = 0; while ((len = bis.read(bys)) != -1) { bos.write(bys, 0, len); } bos.close(); bis.close(); } }