1.写文件
File + FileOutputStream 按字节写入
package indi.huishi;
import org.junit.Test;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.stream.Stream;
public class IOTest {
/**
* 写文件1 File + FileOutputStream
* @throws IOException
*/
@Test
public void testWriteByBytes() throws IOException {
String path = "src//write.txt";
File file = new File(path);
String content = "I am AsajuHuishi";
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(content.getBytes(StandardCharsets.UTF_8));
fileOutputStream.close();
}
FileWriter + BufferedWriter 按字符写入
/**
* 写文件2 FileWriter + BufferedWriter
* @throws IOException
*/
@Test
public void testWriteByWriter() throws IOException {
String path = "src//write2.txt";
String content = "I am AsajuHuishi";
FileWriter fileWriter = new FileWriter(path);
BufferedWriter writer = new BufferedWriter(fileWriter);
writer.write(content);
writer.close();
}
2.读文件
File + FileInputStream 按字节读取
/**
* https://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html
*/
/**
* 读文件 按字节读取 File + FileInputStream
* @throws IOException
*/
@Test
public void testReadByBytes() throws IOException {
String path = "src//content.txt";
File file = new File(path);
FileInputStream fileInputStream = new FileInputStream(file);
int tempByteLength;
byte[] bytes = new byte[30];
while ((tempByteLength = fileInputStream.read(bytes))!=-1){
System.out.write(bytes,0,tempByteLength);
}
fileInputStream.close();
}
File + FileInputStream + InputStreamReader 按字符读取
InputStreamReader类是从字节流到字符流的桥接器:它使用指定的字符集读取字节并将它们解码为字符。
它使用的字符集可以通过名称指定,也可以明确指定,或者可以接受平台的默认字符集。每次调用一个InputStreamReader的read()方法都可能导致从底层字节输入流中读取一个或多个字节。
原文链接:https://blog.csdn.net/ai_bao_zi/article/details/81133476
/**
* 读文件 按字符读取 File + FileInputStream + InputStreamReader
* @throws IOException
*/
@Test
public void testReadByCharacter() throws IOException {
String path = "src//content.txt";
File file = new File(path);
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader streamReader = new InputStreamReader(fileInputStream);
int tempCharLength;
char[] chars = new char[30];
while ((tempCharLength = streamReader.read(chars))!=-1){
for (char c : chars) {
System.out.print(c);
}
}
streamReader.close();
fileInputStream.close();
}
File + FileInputStream + InputStreamReader+BufferedReader 按字符读取
为了获得最高效率,请考虑在BufferedReader中包装InputStreamReader.
@Test
public void testReadByCharacter2() throws IOException {
String path = "src//content.txt";
File file = new File(path);
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader streamReader = new InputStreamReader(fileInputStream);
BufferedReader reader = new BufferedReader(streamReader);
int tempCharLength;
char[] chars = new char[30];
while ((tempCharLength = streamReader.read(chars))!=-1){
for (char c : chars) {
System.out.print(c);
}
}
streamReader.close();
fileInputStream.close();
reader.close();
}
为什么使用BufferedReader
从read()方法理解,若使用InputStreamReader的read()方法,可以发现存在每2次就会调用一次解码器解码,但若是使用
BufferedReader包装InputStreamReader后调用read()方法,可以发现只会调用一次解码器解码,其余时候都是直接从BufferedReader的缓冲区中取字符即可
从read(char cbuf[], int offset, int length)方法理解,若使用InputStreamReader的方法则只会读取leng个字符,但是使用BufferedReader类则会读取8192个字符,会尽量提取比当前操作所需的更多字节;
例如文件中有20个字符,我们先通过read(cbuf,0,5)要读取5个字符到数组cbuf中,然后再通过read()方法读取1个字符。那么使用InputStreamReader类的话,则会调用一次解码器解码然后存储5个字符到数组中,然后又调用read()方法调用一次解码器读取2个字符,然后返回1个字符;等于是调用了2次解码器,若使用BufferedReader类的话则是先调用一次解码器读取20个字符到字符缓冲区中,然后复制5个到数组中,在调用read()方法时,则直接从缓冲区中读取字符,等于是调用了一次解码器
因此可以看出BufferedReader类会尽量提取比当前操作所需的更多字节,以应该更多情况下的效率提升,
因此在设计到文件字符输入流的时候,我们使用BufferedReader中包装InputStreamReader类即可
原文链接:https://blog.csdn.net/ai_bao_zi/article/details/81134801
FileReader + BufferedReader 按行读取(使用stream)
/**
* 读文件 按行读取 FileReader + BufferedReader
* @throws IOException
*/
@Test
public void testRead2() throws IOException {
String path = "src//content.txt";
FileReader fileReader = new FileReader(path);
BufferedReader reader = new BufferedReader(fileReader);
// String line = null;
// while((line=reader.readLine())!=null){
// System.out.println(line);
// }
// reader.close();
// 或者直接用stream
Stream<String> stream = reader.lines();
stream.forEach(System.out::println);
reader.close();
}
边读边写二进制图片 File + FileInputStream + FileOutputStream
/**
* 边读边写二进制图片 File + FileInputStream + FileOutputStream
*/
@Test
public void testReadAndWrite() throws IOException {
// (按字节)读取图片
String inputPath = "src//1.png";
File file = new File(inputPath);
FileInputStream fileInputStream = new FileInputStream(file);
// 输出图片
String outputPath = "src//2.png";
File file1 = new File(outputPath);
FileOutputStream fileOutputStream = new FileOutputStream(file1);
// 边读边写
int tempByteLength;
byte[] bytes = new byte[30];
while((tempByteLength = fileInputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,tempByteLength);
}
fileOutputStream.close();
fileInputStream.close();
}
}
参考链接:
https://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html