一、FileInputStream的使用详解如下:
public class FileInputStream_ { /** * 演示读取文件... * 单个字节的读取,效率比较低 * ->使用read(byte[] b) */ @Test public void readFile01() { String filePath = "D:\\hello.txt"; int readData = 0; FileInputStream fileInputStream = null; try { //创建FileInputStream对象,用于读取文件 fileInputStream = new FileInputStream(filePath); //fileInputStream.read()方法 //从该输入流读取一个字节的数据。如果没有输入可用,此方法将阻止 //如果返回-1,表示读取完毕。 while ((readData = fileInputStream.read()) != -1) { System.out.print((char) readData); //转成char进行显示 } } catch (IOException e) { e.printStackTrace(); } finally { //关闭文件流释放资源 try { if (fileInputStream != null) { fileInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 使用read(byte[] b)读取文件,提高效率 */ @Test public void readFile02() { String filePath = "D:\\hello.txt"; int readData = 0; //字节数组 byte[] buf = new byte[8]; //一次读取八个字节 int readLen = 0; FileInputStream fileInputStream = null; try { //创建FileInputStream对象,用于读取文件 fileInputStream = new FileInputStream(filePath); //fileInputStream.read(byte[] b)方法 //从该输入流读取最多b.length字节的数据到字节数组中。此方法将阻塞,直到某些输入可用。 //如果返回-1,表示读取完毕 //如果读取正常,返回实际读取的字节数。 //如果最后一次读取的字节数,不够八个,则返回实际的字节数。如果够的话,每次最多只能读取八个 while ((readLen = fileInputStream.read(buf)) != -1) { //把每次读取的字节数,返回给readLen,用字节数组构建一个字符串,显示出来。 System.out.print(new String(buf, 0, readLen)); } } catch (IOException e) { e.printStackTrace(); } finally { //关闭文件流释放资源 try { if (fileInputStream != null) { fileInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
二、FileOutputStream的使用详解如下:
public class FileOutputStream01 { /** * 演示使用FileOutputStream将数据写到文件中 * 如果该文件不存在,则创建该文件 */ @Test public void writeFile() { //创建FileOutputStream对象 String filePath = "D:\\a.txt"; FileOutputStream fileOutputStream = null; try { //1.new FileOutputStream(filePath);创建方式,当写入内容时,会覆盖原来的内容 //1.new FileOutputStream(filePath,true);创建方式,当写入内容时,是追加到文件后面 fileOutputStream = new FileOutputStream(filePath,true); //写入一个字节 //fileOutputStream.write('L'); //写入字符串 String str = "ly,world"; //str.getBytes()可以把字符串->字节数组 //write(byte[] b) 将b.length个字节从指定的字节数组写入此文件输出流。 //fileOutputStream.write(str.getBytes()); //write(byte[] b,int off,int len)将len字节从位于偏移量off的指定字节数组写入此文件的输出流 fileOutputStream.write(str.getBytes(),0,2); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
具体案例如下
一、复制图片
@Test public void test2(){ FileInputStream fis = null; FileOutputStream fos = null; try { //1.提供File类的对象 File file1 = new File("工作二维码.png"); File file2 = new File("工作二维码3.png"); //2.造流 fis = new FileInputStream(file1); fos = new FileOutputStream(file2); //3.复制图片 byte[] buffer = new byte[5]; int len; while ((len=fis.read(buffer))!=-1){ //读取到后,就写入到文件,通过FileOutputStream //即:是一边读,一边写 fos.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { //4.关闭资源 if (fis!=null){ try { fis.close(); }catch (IOException e) { e.printStackTrace(); } } if (fos!=null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
二、复制文件
//指定路径下文件的复制 public void copyFile(String srcPath,String descPath){ FileInputStream fis = null; FileOutputStream fos = null; try { //1.提供File类的对象 File file1 = new File(srcPath); File file2 = new File(descPath); //2.造流 fis = new FileInputStream(file1); fos = new FileOutputStream(file2); //3.复制图片 byte[] buffer = new byte[1024]; int len; while ((len=fis.read(buffer))!=-1){ //void write(byte[] b ,int off, int len) //将byte[]数组下标off开始的len长度的数据写入当前输出流 fos.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { //4.关闭资源 if (fis!=null){ try { fis.close(); }catch (IOException e) { e.printStackTrace(); } } if (fos!=null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void testCopyFile(){ long start = System.currentTimeMillis(); String srcPath="C:\\Users\\DELL\\Desktop\\1.回顾类-封装和继承.mp4"; String descPath= "C:\\Users\\DELL\\Desktop\\1.回顾类-封装和继承3.mp4"; copyFile(srcPath,descPath); long end = System.currentTimeMillis(); System.out.println("复制消耗的时间:"+(end-start)); }