一、文件
1、基础操作
File file1 = new File("d:/test/test.txt");
if(!file1.exists()){
file1.createNewFile();
}
System.out.println(file1.getName());
System.out.println(file1.length());
System.out.println(file1.getAbsolutePath());
//文件夹
File file2 = new File("d:/test/test/t");
if(!file2.exists()){
file2.mkdirs();
}
File f1 = new File("D:/test");
File []fs = f1.listFiles();
for (File f:fs
) {
System.out.println(f.getName()+" "+f.isFile());
}
二、IO流
分类
1、根据流动方向分为“输入流、输出流”(内存为参考,读取文档为输入、写入文档为输出)
2、根据传输格式分为“字节流、字符流”
字节:InputStream输入、OutputStream输出
字符:Read读取、Writer写入
1、字节流操作
读取写入
File file = new File("D:/test/test/3w.txt");
FileInputStream fi = new FileInputStream(file);
//单个字节读取
for(int i=0;i<file.length();i++){
System.out.println(fi.read());
}
System.out.println("-----------------");
//一次读取一个字节数组的内容
fi = new FileInputStream(file);
byte []b = new byte[(int)file.length()];
fi.read(b);
System.out.println(new String(b));
File file = new File("d:/test/123.txt");
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fo = new FileOutputStream(file);//加参数true,追加
String str = "how are you";
//单个字节写入
byte []b = str.getBytes();
// for (int i=0;i<b.length;i++){
// fo.write(b[i]);
// }
//一次写入一个字节数组的数据
fo.write(b);
复制
File f1 = new File("D:/file/Video_2019-08-21_151318.wmv");
File f2 = new File("D:/test/高清.mp4");
if(!f2.exists()){
f2.createNewFile();
}
FileInputStream fi = new FileInputStream(f1);
FileOutputStream fo = new FileOutputStream(f2);
byte []b = new byte[1024*1024*6];
while(fi.read(b)!=-1){
fo.write(b);
}
fo.close();
fi.close();
System.out.println("复制完成!!");
2、字符流的操作
读取
File file = new File("d:/test/test.txt");
FileReader fr = new FileReader(file);
//读取一个或者一个数组效率低,而通过包装缓存对象读取效率高
BufferedReader br = new BufferedReader(fr);
String str = null;
while((str = br.readLine())!=null){
System.out.println(str);
}
写入
File file = new File("d:/test/test2.txt");
// if(!file.exists()){
// file.createNewFile();
// }
FileWriter fw = new FileWriter(file,true);//第二个参数为true表示追究,默认不写为覆盖
BufferedWriter bw = new BufferedWriter(fw);
String str = "你好,世界";
bw.write(str);
bw.newLine();//换行
bw.close();
复制
File f1 = new File("D:/test/test2.txt");
File f2 = new File("D:/gg.txt");
if(!f2.exists()){
f2.createNewFile();
}
BufferedReader bf = new BufferedReader(new FileReader(f1));
BufferedWriter bw = new BufferedWriter(new FileWriter(f2));
String str = null;
while ((str=bf.readLine())!=null){
bw.write(str);
bw.newLine();
}
bw.close();
bf.close();
三、序列化和反序列化
描述
序列化(ObjectOutputStream) 对象以字节的形式存储到硬盘(被序列化的对象需要实现Serializable接口)
反序列化(ObjectInputStream)
用途
1、将对象以文件的形式保存
2、网络编程数据传输
1、序列化
Students s1 = new Students("李四",18,"男");
Students s2 = new Students("张丽",14,"女");
File file = new File("d:/students.obj");
FileOutputStream fos = new FileOutputStream(file,true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
if(!file.exists()){
file.createNewFile();
}
oos.writeObject(s1);
oos.writeObject(s2);
oos.close();
fos.close();
2、反序列化
File file = new File("d:/students.obj");
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Students students = null;
//在反序列化中如果包含多个对象,读取到最后时系统会自动抛出异常需要进行捕获
try {
while ((students=(Students) ois.readObject())!=null){
System.out.println(students.toString());
}
}catch (Exception e){
System.out.println("反序列化结束");
}
ois.close();
fis.close();
四、字符转字节
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine();
System.out.println(s);