IO流
- 数据在内存与磁盘之间的读和写,将数据读到代码中(输入),将代码数据输出到(输出)
输出流Output:
输入流Input:
注意:IO读写后使用try.cathc.finally关闭close流 |
字节流
字节输入流java.io.FileOutputStream
- 如果目的地不存在File,不报错,会自动创建
- 默认进行覆盖目的地文本文件内容,需要ture开启续写
- 传输类型是byte类型,byte[ ] 类型
- 需要close关闭流
字节输出流java.io.FileInputStream
- 源文件需要读取必须存在,否则报错
- 使用缓冲思想效率更高
- 需要close关闭流
读的方法之坑逼之处
复制文件内容
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyWtrte {
private static FileInputStream fileInputStream;
private static FileOutputStream fileOutputStream;
public static void main(String[] args) {
try {
// 资源文件
fileInputStream = new FileInputStream("project/A1/A2/havaContent.txt");
// 目的地,并开启IO流续写不覆盖
fileOutputStream = new FileOutputStream("project/A1/notContent.txt",true);
// 缓冲数组
byte[] bytes = new byte[1024];
// 新元素覆盖最大索引
int len;
// 这里使用if一次就Copy读完数据,因为我们用了[1024]大字节数组,否则需要使用while多读
if ((len= fileInputStream.read(bytes))!=-1) {
fileOutputStream.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
// 使用完流后需要关闭流:1.否则需要手动调用push刷新写的数据 2.节省资源
try {
if (null!=fileInputStream){
fileInputStream.close();
}
if (null!=fileOutputStream){
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
递归计算多层目录大小
public class FileSize {
public static void main(String[] args) throws IOException {
File file = new File("F:\\面试");
System.out.println(method(file));
}
private static Integer method(File file) throws IOException {
if (!file.exists()) {
return null;
}
int o1 = 0;
int o2 = 0;
byte[] bytes = new byte[1024 * 8];
int len;
for (File listFile : file.listFiles()) {
if (!listFile.isDirectory()) {
FileInputStream fileInputStream = new FileInputStream(listFile);
while ((len = fileInputStream.read(bytes)) != -1) {
o1 += len;
}
} else {
o2 += method(listFile);
}
}
return o1 + o2;
}
}
字节缓冲流
- 读写需要循多次才可以将数据读写完,而使用缓冲流减少读写次数,默认单次缓存的是byte[1024*8] 大小的字节。然后自定义的byte数组决定读取缓存中的字节量。当缓存中的数据被读取到-1结束
字符流
String提供编码解码处理:
解码:成员方法getBytes(StandardCharsets.UTF_8)将字符串按照编码格式
编码:构造方法new String(Byte[] bytes, String decodeCharsetName)
- 虽然String提供了简单的处理方式,但是中文字符使用字节流转换会出现编码解码问题仍存在,所以IO包提供了专门服务中文字符的字符流,适合操作文本内容。
不同编码:
ASCII码表 只能操作数字和单词
GBK:Win默认 中问2字节
Unicode万国表 UTF-8编码格式 中文1字节 英文2字节
与字节流操作方式不同之处:使用的是字符数组char[]
InputStreamWriter
OutputStreamReader
字符缓冲流
FileWriter
FileReader
复制层级目录(包括文本内容)
@SuppressWarnings("All")
public class CopyFiles {
public static void main(String[] args) throws IOException {
File fileIntput = new File("project/A3/A1");
File fileOutput = new File("project/A4");
mkFile(fileIntput,fileOutput);
fileInput.close;
fileOutput.close;
}
private static void mkFile(File fileIntput, File fileOutput) throws IOException {
if (!fileIntput.exists()) {
return;
}
if (!fileOutput.exists()){
fileOutput.mkdirs();
}
for (File file : fileIntput.listFiles()) {
if (file.isFile()) {
File file1 = new File(fileOutput, file.getName());
file1.createNewFile();
copyContent(file,file1);
file1.close;
file.close
}else {
File file1 = new File(fileOutput, file.getName());
file1.mkdirs();
mkFile(file,file1);
}
}
}
private static void copyContent(File file1, File file2) throws IOException {
BufferedReader br1 = new BufferedReader(new FileReader(file1));
if (br1.readLine()==null) {
br1.close();
return;
}
BufferedReader br2 = new BufferedReader(new FileReader(file1));
String result;
BufferedWriter bw = new BufferedWriter(new FileWriter(file2,true));
while ((result = br2.readLine()) != null) {
bw.write(result);
bw.newLine();
}
bw.close();
}
}
对象操作流
注意:对于操作对象IO流读取空文件为报出EOFE异常
反序列化:读取对象集合并转型
List<Student> list = (List<Student>) objectInputStream.readObject();
序列号:将对象集合序列号