1、文件复制
1.1单级文件夹复制
1.获取要复制的源文件夹和目标文件夹
2.遍历源文件夹,获取每一个文件
3.将当前文件复制到目标文件夹下
package itfenghua01;
import java.io.*;
public class copyDemo01 {
public static void main(String[] args) throws IOException{
File srcfile = new File("D:\\COPY");
String srcfileName = srcfile.getName();
File destfile = new File("day10",srcfileName);
if (!destfile.exists()){
destfile.mkdir();
}
File[] listFiles = srcfile.listFiles();
for (File file:listFiles){
String fileName = file.getName();
File file1 = new File(destfile,fileName);
if (file.isFile()){
copy(file,file1);}
}
}
private static void copy(File file, File file1) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));
byte[] by = new byte[1024];
int len;
while ((len=bis.read(by))!=-1){
bos.write(by,0,len);
}
bos.close();
bis.close();
}
}
1.2多级文件夹复制
1.获取要复制的源文件夹和目标文件夹
2.遍历源文件夹,获取每一个文件对象
3.判断当前文件对象是文件还是文件夹,是文件直接复制,是文件夹,递归调用自己
package com.itfenghua03;
import java.io.*;
public class fileDemo02 {
public static void main(String[] args) throws IOException{
//获取源文件和目标文件
File srcfile = new File("D:\\IO");
File destfile = new File("day10\\");
//调用方法
copyFile(srcfile,destfile);
}
private static void copyFile(File srcfile, File destfile) throws IOException {
//判断源文件夹是否是文件夹
if (srcfile.isDirectory()){
String srcfileName = srcfile.getName();
File destFile = new File(destfile,srcfileName);
//获取文件列表
File[] listFiles = srcfile.listFiles();
//目的文件夹是否存在
if (!destFile.exists()){
destFile.mkdir();
}
//调用方法
for (File file:listFiles){
copyFile(file, destFile);
}
//是文件则调用复制文本方法
}else {
//新的目标文件名称
String fileName = srcfile.getName();
File newfile = new File(destfile,fileName);
copy(srcfile,newfile);
}
}
private static void copy(File file, File newfile) throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newfile));
byte[] by = new byte[1024];
int len;
while ((len = bis.read(by)) != -1) {
bos.write(by, 0, len);
}
bos.close();
bis.close();
}
}
1.3IO流异常处理
声明流对象;
try{
创建流对象;
使用流对象;
}catch(IOException e){
//处理异常
}finally{
//关流,释放资源
}
package itfenghua01;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class exceptionDemo {
public static void main(String[] args) {
method1();
}
public static void method2() {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("test.txt");
fw = new FileWriter("my.txt");
char[] cs = new char[1024];
int len;
while ((len = fr.read(cs)) != -1) {
fw.write(cs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void method1() {
try (FileReader fr = new FileReader("test.txt");
FileWriter fw = new FileWriter("my.txt");) {
char[] ch = new char[1024];
int len;
while ((len = fr.read(ch)) != -1) {
fw.write(ch, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、打印流
2.1特点
只负责数据的输出,不负责数据的读取
2.2特有方法
void print(Object o)
打印后不换行
void println(Object o)
打印后换行
2.3字节打印流 printStream
2.3.1继承关系
父类是FileOutputStream,继承了父类的成员方法
2.3.2构造方法
PrintStream(String fileName)
通过字符串文件路径创建字节打印流对象
2.3.3注意事项
使用父类的write方法写入数据时,会进行转码,97 -> a
使用自己特有的print/println方法写入数据时,原样写入,97 -> 97
2.4字符打印流 printWriter
2.4.1继承关系
父类是Writer,继承了父类的成员方法
2.4.2构造方法
PrintWriter(String fileName)
通过字符串文件路径创建字符打印流对象
PrintWriter(Writer out, boolean autoFlush)
通过字符输出流对象创建字符打印流对象,可以进行自动刷新
3、序列化流
3.1序列化和反序列化
1、序列化
将内存中的对象写入到本地文件中(持久化)
2、反序列化
将本地文件中的对象读取到内存中
3.2序列化流 ObjectOutputStream
3.2.1继承关系
父类是OutputStream,继承了父类的成员方法
3.2.2构造方法
ObjectOutputStream(OutputStream out)
通过字节输出流对象创建序列化流对象
3.2.3特有方法
void writeObject(Object obj)
将对象写入到文件中
3.3反序列化流 ObjectInputStream
3.3.1继承关系
父类是InputStream,继承了父类的成员方法
3.3.2构造方法
ObjectInputStream(InputStream in)
通过字节输入流对象创建反序列化流对象
3.3.3特有方法
Object readObject()
将文件中的对象读取出来
3.4注意事项
1.序列化与反序列化的类必须实现Serializable接口
如果是一个自定义的类,要求自定义的类必须实现Serializable接口
如果是一个容器类,要求容器类和容器中存储的元素类都要实现Serializable接口
2.修改序列化后的类的class文件,反序列化时会出现异常,通过定义serialVersionUID解决
3.如果类中的某一个字段不想被序列化,可以使用transient关键字修饰
4、Properties集合
4.1介绍
是HashTable的子类,与IO流相关的集合
4.2特有方法
Object setProperty(String key,String value)
向集合中添加指定的键和值
String getProperty(String key)
通过指定的键获取对应的值
Set stringPropertyNames()
获取所有的键封装到Set集合中返回
4.3将集合中的数据持久化到本地文件中
void store(Writer writer,String commons)
writer:字符输出流
commons:注释,对本次操作进行解释说明的
注意事项
commons注释建议使用英文或空格,防止中文乱码
4.4将本地文件中的数据加载到集合中
4.4.1void load(Reader reader)
reader:字符输入流
4.4.2注意事项
1.文件中的键值对数据使用=号进行连接
2.文件中的#号后边的是注释,不会被加载到集合中
package com.itfenghua04;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class propertiesDemo {
public static void main(String[] args) throws IOException{
myload();
mystore();
}
```
private static void mystore() throws IOException{
Properties prop = new Properties();
FileWriter fw = new FileWriter("day10\\test.txt");
prop.setProperty("楚子航","1110");
prop.store(fw,"a");
fw.close();
}
```
```
private static void myload() throws IOException{
Properties prop = new Properties();
FileReader fr = new FileReader("day10\\Demo01.txt");
prop.load(fr);
fr.close();
System.out.println(prop);
}
```
}
5、使用场景
5.1图片,音乐,视频
字节流
高效字节流
5.2文本
字符流
高效字符流
5.3对文件转码,读取指定编码的文件
转换流
5.4将对象写入到文件中
序列化流
5.5直接打印数据
打印流