好记性不如有网,有网也得知道去哪里寻找,小编作为一只(条)猴子(鱼),对自己的记忆不敢恭维,默默写起笔记。并且画了一个流族谱图,看得更清晰些:
一、你知道以前的文件写读,
使用Write和Read的子类子类FileWriter和FileReader可实现文件的读写操作,一般用于字符的转换
使用FileWrite()写入,写入字符流
//可以是读取某目录下的文件,也可以是创建一个文件夹
File file = new File("D:/io.txt");
System.out.println(file.length());
try (FileWriter fileWriter = new FileWriter(file)) {
//上面的try会自动释放资源,写到新创建的文件里
//如果需要追加数据,而不是覆盖,则使用FileWriter(path,true)构造方法
fileWriter.write("Hello PLUS");
fileWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
使用FileRead()读取,读取文本
//从最初的读一个字符然后向下一个字符移动,到发现可以使用while循环读取每一个字符,
//再到使用字节数组将需要的字符收集起来,到如下指定字符数组的长度,演变如此艰难
int len = 0 ;
char[] buff = new char[1024];
while((len = fileReader.read(buff))!=-1)
{System.out.println(new String(buff, 0, len).toString());}
FileOutputStream是用于写入原始字节流比如图片流数据,FileInputSteam用来读取字节流。
FileOutputStream out = new FileOutputStream(new File("D:/out.txt"));
out.write("开会了".getBytes());
out.flush();
out.close();
FileInputStream in = new FileInputStream(new File("D:/out.txt"));
FileOutputStream ou = new FileOutputStream(new File("D:/copy.txt"));
int len = -1 ;
byte [] buff = new byte[1024];
while ((len = in.read(bytes,0,bytes.length)) != -1){}
while ((len = in.read(buff))!=-1){
ou.write(buff,0,len);
}
in.close();
ou.close();
//聊表意思,不做深究
二、应该用效率高一点的io流
BufferedReader、BufferedWriter(缓存流,提供readLine方法读取一行文本)
//写入相应的文件
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:/out3.txt"),"GBK"));
out.write("开会了,真的有点紧张");
out.flush();
out.close();
//读取文件(字符流)
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("D:/out3.txt"),"GBK"));
//写入相应的文件
BufferedWriter ou = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:/copy3.txt"),"GBK"));
//读取数据
//循环取出数据
String str = null;
while ((str = in.readLine()) != null) {
System.out.println(str);
//写入相关文件
ou.write(str);
ou.newLine();
}
//清楚缓存
ou.flush();
//关闭流
in.close();
ou.close();
BufferedInputStream、BufferedOutputStream(缓存字节流)
BufferedOutputStream out = new BufferedOutputStream(new File("D:/out.txt"));
out.write("开会了".getBytes());
out.flush();
out.close();
//读取文件(缓存字节流)
BufferedInputStream in = new BufferedInputStream(new FileInputStream("D:/out.txt"));
//写入相应的文件
BufferedOutputStream ou = new BufferedOutputStream(new FileOutputStream("D:/copy2.txt"));
//读取数据
//一次性取多少字节
byte[] bytes = new byte[2048];
//接受读取的内容(n就代表的相关数据,只不过是数字的形式)
int n = -1;
//循环取出数据
while ((n = in.read(bytes)) != -1) {
//转换成字符串
String str = new String(bytes,0,n,"utf-8");
System.out.println(str);
//写入相关文件
ou.write(bytes, 0, n);
}
//清楚缓存
ou.flush();
//关闭流
in.close();
ou.close();
三、来点大家喜欢的,IOUtils
write:可以把数据写入到输出流中
public static void ioUtilsWrite(){
try (OutputStream outputStream = new FileOutputStream("D:/outUtils.txt")) {
IOUtils.write("把数据写入到输出流中",outputStream);
} catch (IOException e2) {
e2.printStackTrace();
}
}
copy可以拷贝流,copyLarge(reader,writer)2G上的数据,真的很爽的。
public static void ioUtilsCopy() throws IOException {
FileInputStream in = new FileInputStream("D:/outUtils.txt");
FileOutputStream ou = new FileOutputStream("D:/copyUtils.txt");
IOUtils.copy(in,ou);
}
public static void ioUtilsReadLine(){
List<String> lines;
try (InputStream is = new FileInputStream("D://out.txt")) {
lines = IOUtils.readLines(is);
for(String line : lines){
System.out.println(line);//会读取到每一行的
}
} catch (IOException e) {
e.printStackTrace();
}
public static void ioUtilsRead(){
byte [] bytes = new byte[1024];
try (FileInputStream fileInputStream = new FileInputStream("D:/out.txt")) {
IOUtils.read(fileInputStream, bytes);
System.out.println(new java.lang.String(bytes));
FileOutputStream ou = new FileOutputStream("D:/outC.txt");
IOUtils.write(bytes,ou);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}