一个关于IO流的导图
IO流字节的读写,实现复制
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class TestCopy {public static void main(String[] args) throws IOException {copyThree(new File("F:\\joy\\1.mp4"), new File("F:\\1.mp4"));}// 适应小文件。public static void copyOne(File pathInput, File pathOutput) throws IOException {// 获得输入流FileInputStream fis = new FileInputStream(pathInput);// 输出流(写入流)FileOutputStream fos = new FileOutputStream(pathOutput);int a = 0;// 一个一个字节的读取,并写入。while ((a = fis.read()) != -1) {fos.write(a);}fos.close();fis.close();}// 针对文件中等大小的, 对于太大的装不完, 内存溢出。public static void copyTwo(File pathInput, File pathOutput) throws IOException {FileInputStream fis = new FileInputStream(pathInput); FileOutputStream fos = new FileOutputStream(pathOutput);//用byte数组装 字节 fis.available() 最大的字节数byte[] bb = new byte[fis.available()];fos.write(bb); fos.close();fis.close();}// 超大文件如mp4的复制, 多次装。public static void copyThree(File pathInput, File pathOutput) throws IOException {FileInputStream fis = new FileInputStream(pathInput); FileOutputStream fos = new FileOutputStream(pathOutput);//可以定义一次装的大小。缓冲区, 100MBbyte[] bb = new byte[1024 * 1024 * 100];int len;// len 实际读取的字节数。while ((len = fis.read(bb)) != -1) {fos.write(bb, 0, len);}fos.close();fis.close(); }}
JavaIO字符流的读写
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class TestCharCopy {public static void main(String[] args) {// 复制 FileWriter 和 FileReader 读取字符操作。// 把a.txt 里面的内容复制到 b.txt java 太强了不仅跨平台 还支持复制各种文件。try {FileReader fr = new FileReader("a.txt");FileWriter fw = new FileWriter("b.txt");char[] ch = new char[10];int len;while ((len = fr.read(ch)) != -1) {fw.write(ch, 0, len);}fw.close();fr.close();} catch (IOException e) {e.printStackTrace(); }}}
JavaIO流buffered 类 的 读写。
package c12_24;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class TestBuffered {public static void main(String[] args) {// 相对路径测试一下。//method1("a.txt", "b.txt");method2("a.txt", "b.txt");}// bufferedWriter bufferedReaderprivate static void method1(String path1, String path2) {try {BufferedReader br = new BufferedReader(new FileReader(path1));BufferedWriter bw = new BufferedWriter(new FileWriter(path2));//2 MB 缓冲区int len;char[] ch = new char[1024 * 1024];//最多 len 个字节长度读入数组while ((len = br.read(ch)) != -1) {bw.write(ch, 0, len);bw.newLine();//换行}bw.close();br.close();} catch(IOException e) {}}// bufferedInputStream bufferedOutputStream private static void method2(String path1, String path2) {try {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path1));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path2));int len;// 1kb 缓冲区byte[] b = new byte[1024 * 1];while ((len = bis.read(b)) != -1) {bos.write(b, 0, len);}bos.close();bis.close();} catch(IOException e) {}}}
字节流转换字符流 读取
public static void method2(String path) {FileInputStream fis = null;InputStreamReader isr = null;try {// 字节流 转换成 字符流fis = new FileInputStream(path);isr = new InputStreamReader(fis);int i;while ((i = isr.read()) != -1) {System.out.print((char)i);} } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {isr.close(); fis.close();} catch (IOException e) {System.out.println(e);}}}
字节流转换字符流 写入
// 写入文件public static void method3(String path) {FileOutputStream fos = null;OutputStreamWriter fsw = null;try {fos = new FileOutputStream(path);fsw = new OutputStreamWriter(fos);fsw.write("asshole");} catch (FileNotFoundException e) {System.out.println(e);} catch (IOException e) {System.out.println(e);} finally {try {fsw.close();fos.close();} catch (IOException e) {System.out.println(e);}}}
找出以.png … .jpg … .xml结束的文件
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;public class testFile {public static void main(String[] args) throws IOException {//get endWith .jpg files File imgs = new File("C:\\Users\\admin\\Pictures\\Saved Pictures\\cartoon");//实现过滤器接口 去掉不合规则的 再加到数组File[] lf = imgs.listFiles(new myFilter());for (File file : lf) {System.out.println(file);}}}class myFilter implements FileFilter {@Overridepublic boolean accept(File pathname) {if (pathname.isFile() && pathname.getName().endsWith(".jpg"))return true;return false;}}