字节流:FileInputStream FileoutputStream
过滤流/处理流:
- BufferedInputStream BufferedOutputStream
- DataInputStream DataOutputStream
转换流:
- 对象流:ObjectInputStream ObjectOutputStream
- 字符流:Reader,Writer
用字节流(管子)FileInputStream,FileoutputStream拷贝文件:
void copyfile(File src, File target) throws IOException {
if (!src.exists()) {// 源文件不存在,程序结束
System.out.println("source file is not exist");
return;
}
if (!target.exists()) {// 目标文件不存在,则创建
target.createNewFile();
}
FileInputStream fis = new FileInputStream(src);// 文件输入流/读,从文件输入/读到内存
FileOutputStream fos = new FileOutputStream(target);// 文件输出流/写,从内存输出/写到文件
byte[] buf = new byte[1024];
int lenth = -1;
// 准备拷贝
long begin = System.currentTimeMillis();
while ((lenth = fis.read(buf)) != -1) {
fos.write(buf, 0, lenth);
}
// 拷贝结束
long end = System.currentTimeMillis();
System.out.println("花费时间:");
System.out.println(end - begin);
if (fis != null) {
fis.close();
fis = null;
}
if (fos != null) {
fos.close();
fos = null;
}
}
用过滤/处理流(管子加粗)拷贝文件:不管怎么包都要用到FileInputStream,FileoutputStream
在关闭流的时候要先关包装流再关基本流
BufferedInputStream:对FileInputStream加速
void copyfilebyFilter(File src, File target) throws IOException {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(target);
BufferedInputStream bis = new BufferedInputStream(fis);
// 包装流,相当于给基本流那根管子加粗,属于高级流
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];
int len = -1;
// 准备拷贝
long begin = System.currentTimeMillis();
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
// 拷贝结束
long end = System.currentTimeMillis();
System.out.println("花费时间:");
System.out.println(end - begin);
// 先关高级流
bis.close();
bos.close();
fis.close();
fos.close();
}
DataInputStream DataOutputStream:可以对具体的数据类型作处理
ObjectInputStream ObjectOutputStream:读写对象,可 将对象写入文件,实现数据持久化
读对象,当读到文件结束时,不是返回null而是报EOFException异常
void readObject() throws IOException {
File file = new File("f://d.txt");
if (!file.exists()) {
return;
}
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = null;
// 读取对象,如果读不到,并不是返回null,而是报一个EOFException异常
try {
while ((obj = ois.readObject()) != null) {
System.out.println(obj);
}
} catch (EOFException |ClassNotFoundException e) {
// 读不到,结束
return;
}
ois.close();
fis.close();
}
首先要有一个Emp对象类,然后创建对象写入文件
void writeObject() throws IOException {
Emp emp1 = new Emp("e001", "jack", 5, (double) 2222);
Emp emp2 = new Emp("e002", "mack", 33, (double) 666);
Emp emp3 = new Emp("e003", "jass", 55, (double) 3456);
File file = new File("f://d.txt");
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(emp1);//写对象
oos.writeObject(emp2);
oos.writeObject(emp3);
oos.close();
fos.close();
}
用 File的renameTo()方法拷贝文件:
文件拷贝:找出D盘下test文件夹中所有的 .txt 文件,拷贝至 E:\test 目录下,并将所有.txt文件修改为.bat
public static void moveFile(File dir) {
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
moveFile(file);
} else {
if (file.getName().endsWith(".txt")) {
file.renameTo(new File(
"E:\\test\\" + file.getName().substring(0, file.getName().lastIndexOf('.')) + ".bat"));
}
}
}