Java 各种IO流的使用

//1.复制文件
package io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class IO1 {
	public static void main(String[] args) {
		String a = "C:\\java视频\\传智播客_历经5年锤炼(史上最适合初学者入门的Java基础视频)\\JavaSE基础视频01";
		String b = "C:\\Users\\Administrator\\Desktop\\xi";
		File aa = new File(a);
		File bb = new File(b);
		if (aa.isDirectory() && bb.isDirectory()) {
			copy(aa, bb);
		} else {
			System.out.println("文件名字有误");
		}
	}

	// 递归法复制文件夹
	private static void copy(File from, File to) {
		InputStream is = null;
		OutputStream os = null;
		try {
			is = new FileInputStream(from);
			os = new FileOutputStream(to);
			int len = 0;
			byte bytes[] = new byte[1024 * 4];
			while ((len = is.read(bytes)) != -1) {
				os.write(bytes, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					os.close();
					if (os != null) {
						is.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		System.out.println("复制成功");

	}
}
//2.复制文件夹
package io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class IO2 {
	public static void main(String[] args) {
		// 源文件夹
		File theFile = new File("C:\\copy");
		// 复制到哪里去
		File newFile = new File("E:/sdfadf/t");
		// 原字符串中要删除的部分的长度
		int cutLength = theFile.getPath().substring(0, theFile.getPath().lastIndexOf("\\")).length();
		// 开始复制
		copyDir(theFile, newFile, cutLength);
	}

	// 先测试遍历输出文件夹以及文件的名字
	private static void copyDir(File file, File elif, int h) {
		String hi = file.getPath();// 原文件路径
		String now = hi.substring(h, hi.length());// 文件相对路径
		// System.out.println(hi);//原来的文件路径
		String one = elif.getPath() + now;// 新文件的绝对路径
		// System.out.println(one);//现在的文件路径
		File two = new File(one);// 封装新路径,准备复制该文件
		if (file.isDirectory()) {
			two.mkdirs();
		}
		if (file.isFile()) {
			copyFile(file, two);
		}
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (File afile : files) {
				copyDir(afile, elif, h);
			}
		}
	}

	private static void copyFile(File from, File to) {
		InputStream is = null;
		OutputStream os = null;
		try {
			is = new FileInputStream(from);
			os = new FileOutputStream(to);
			int len = 0;
			byte bytes[] = new byte[1024 * 4];
			while ((len = is.read(bytes)) != -1) {
				os.write(bytes, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					os.close();
					if (os != null) {
						is.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println("复制成功");
	}
}

//3.输出文件夹
package io;

import java.io.File;

public class IO3 {
	public static void main(String[] args) {
		File dir = new File("E:/copy");
		if (dir.isDirectory()) {
			delDir(dir);
			System.out.println("OK");
		} else {
			System.out.println("您要删除的东西不是文件夹或不存在");
			return;
		}
	}

	// 调用此函数的必然是文件夹
	private static void delDir(File dir) {
		File[] files = dir.listFiles();
		for (File f : files) {
			// 如果是文件夹先删除内容再说
			if (f.isDirectory()) {
				delDir(f);
			}
			// 文件和文件夹都得删除,这里的文件夹已经为空
			f.delete();
		}
		// 注意:把顶层空文件夹删除
		dir.delete();
	}
}

//4.字符流复制文件
package io;
注意:如果两个流中有一个没有关闭,复制都会失败
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class IO4 {
	public static void main(String[] args) {
		copyFileOfChar("C:\\xixi.m\\a.txt", "C:\\xixi.m\\aAgain.txt");
	}

	private static void copyFileOfChar(String from, String to) {
		Reader a = null;
		Writer b = null;
		try {
			a = new FileReader(from);
			b = new FileWriter(to, true);
			char[] s = new char[10];
			int len = 0;
			while ((len = a.read(s)) != -1) {
				b.write(s, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				b.close();
				a.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

//5.缓冲字符输入输出流 拷贝文件
package io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class IO5 {

	public static void main(String[] args) {
		copy("C:\\xixi.m\\a.txt", "C:\\xixi.m\\aAgain.txt");
	}

	private static void copy(String from, String to) {
		BufferedReader a = null;
		BufferedWriter b = null;
		String mid = "";
		try {
			a = new BufferedReader(new FileReader(from));
			b = new BufferedWriter(new FileWriter(to));
			while ((mid = a.readLine()) != null) {
				// 此处应该用notePade或着sublime打开
				b.write(mid + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				b.close();
				a.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

//6.缓冲字节输入输出流 拷贝文件
package io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class IO6 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		copy("C:\\xixi.m\\a.txt", "C:\\xixi.m\\aAgain.txt");
	}

	private static void copy(String from, String to) {
		BufferedInputStream a = null;
		BufferedOutputStream b = null;
		byte[] bytes = new byte[1024];
		int len = 0;
		try {
			a = new BufferedInputStream(new FileInputStream(from));
			b = new BufferedOutputStream(new FileOutputStream(to));
			while ((len = a.read(bytes)) != -1) {
				b.write(bytes, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				b.close();
				a.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

//7.字符流转字节流(不使用缓冲)
package io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class IO7 {

	public static void main(String[] args) {
		copy("C:\\xixi.m\\a.txt", "C:\\xixi.m\\aAgain.txt");

	}

	private static void copy(String from, String to) {
		InputStreamReader a = null;
		OutputStreamWriter b = null;
		char[] chars = new char[10];
		int len = 0;
		try {
			a = new InputStreamReader(new FileInputStream(from));
			b = new OutputStreamWriter(new FileOutputStream(to));
			while ((len = a.read(chars)) != -1) {
				b.write(chars, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				b.close();
				a.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

//8.字符流转字节流(使用缓冲)
package io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class IO8 {

	public static void main(String[] args) {
		copy("C:\\xixi.m\\a.txt", "C:\\xixi.m\\aAgain.txt");
	}

	private static void copy(String from, String to) {
		InputStreamReader a = null;
		OutputStreamWriter b = null;
		char[] chars = new char[1024];
		int len = 0;
		try {
			a = new InputStreamReader(new BufferedInputStream(new FileInputStream(from)));
			b = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(to)));
			while ((len = a.read(chars)) != -1) {
				b.write(chars, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				b.close();
				a.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
// 注意: 1.如果顶层包装是缓冲才能使用readLine方法,如果缓冲流又被其他流包装则不能使用该方法。
//        2.不关闭流拷贝会失败。
改进:
拷贝文件夹判断目录的地方有重复,可以合并

猜你喜欢

转载自blog.csdn.net/qq_32182637/article/details/79174532