java里面的io流是操作文件读写的一门技术。
I:in 指的是输入流,读取文件内容到流对象
O:out 指的是输出流,写文件内容到流对象
流指的是对象。
字节流常用api:
copy文件的dome如下:
package com.mg.java.maven.day01; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * io流 * * @author admin * */ public class FileCopy { public static void main(String[] args) throws IOException { String filepath = "C:\Users\admin\Desktop\装饰器.png"; String topath = "C:\Users\admin\Desktop\装饰器2.png"; FileCopy file = new FileCopy(); file.fileCopy(filepath, topath); // fileCopy(filepath, topath); } public void fileCopy(String filepath, String topath) throws FileNotFoundException, IOException { InputStream inputStream = new FileInputStream(new File(filepath)); OutputStream outputStream = new FileOutputStream(new File(topath)); int size = 0; while ((size = inputStream.read()) != -1) { outputStream.write(size); } if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } System.out.println("success"); } }
静态方法调用不需要创建对象
package com.mg.java.maven.day01; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * io流 * * @author admin * */ public class FileCopy02 { public static void main(String[] args) throws IOException { String filepath = "C:\Users\admin\Desktop\装饰器.png"; String topath = "C:\Users\admin\Desktop\装饰器2.png"; FileCopy02.fileCopy("C:\Users\admin\Desktop\装饰器.png", "C:\Users\admin\Desktop\装饰器2.png"); } public static void fileCopy(String filepath, String topath) throws FileNotFoundException, IOException { InputStream inputStream = new FileInputStream(new File(filepath)); OutputStream outputStream = new FileOutputStream(new File(topath)); int size = 0; while ((size = inputStream.read()) != -1) { outputStream.write(size); } if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } System.out.println("success"); } }