IO流结构
InputStream(字节输入流)
public static void main(String[] args) {
// byteInputStream();
// byteInputStream1();
// byteInputStream2();
byteInputStream3();
}
// 使用字节流时对于中文汉字基本都会出现乱码问题,因此对中文乱码问题通常用字符流Reader和Writer
// 字节输入流(手动关闭资源),一个一个字节读,速度较慢
public static void byteInputStream() {
InputStream inputStream = null;
try {
inputStream = new FileInputStream("C:\\Users\\21941\\Desktop\\sql.txt");
int read = 0;
while ((read = inputStream.read()) != -1) {
System.out.print((char) read);
}
}catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
// 字节输入流(自动关闭资源),一个一个字节读,速度较慢
public static void byteInputStream1() {
try (InputStream inputStream = new FileInputStream("C:\\Users\\21941\\Desktop\\sql.txt");) {
int read = 0;
while ((read = inputStream.read()) != -1) {
System.out.print((char) read);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// 字节输入流(自动关闭资源),一个一个字节读,将读取的先字节存入数组,一同取出,速度较快
public static void byteInputStream2() {
try (InputStream inputStream = new FileInputStream("C:\\Users\\21941\\Desktop\\sql.txt");) {
byte[] bytes = new byte[1024];
while ((inputStream.read(bytes)) != -1) {
System.out.print(new String(bytes));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// 字节输入流(自动关闭资源),一个一个字节读,将读取的先字节存入数组,一同取出,速度较快
public static void byteInputStream3() {
try (InputStream inputStream = new FileInputStream("C:\\Users\\21941\\Desktop\\sql.txt");) {
byte[] bytes = new byte[1024];
int temp=0;
while ((temp=inputStream.read(bytes)) != -1) {
// 防止读到最后时不能全部覆盖之前的,输出每一次读取的内容
System.out.print(new String(bytes,0,temp));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
OutputStream(字节输出流)
public static void OutputStream(){
// 没有文件则自动新建
/*OutputStream outputStream = new FileOutputStream(路径)
OutputStream outputStream = new FileOutputStream(路径,是否追加(默认false))*/
try (OutputStream outputStream = new FileOutputStream("D:\\soft\\javawork\\javawork\\one\\four\\src\\com\\tianliang\\io0\\outputstream.txt")){
outputStream.write(97);
byte[] bytes = {10,23,52,99,45,3,15};
outputStream.write(bytes);
// outputStream.write(字节数组/整形/字节数组,偏移量,长度);
// 输出流需要刷新
outputStream.flush();
}catch (IOException e){
e.printStackTrace();
}
}
Reader(字符输入流)
public static void reader(){
// 用自动关闭方式
try (Reader reader = new FileReader("C:\\Users\\21941\\Desktop\\sql.txt")){
char[] chars = new char[1024];
int temp=0;
while ((temp =reader.read(chars))!=-1){
System.out.print(new String(chars,0,temp));
// reader.read(字符数组/字符数组,偏移量,长度);
}
}catch (IOException e) {
throw new RuntimeException(e);
}
}
Writer(字符输出流)
/*FileWriter writer = new FileWriter(路径)
FileWriter writer = new FileWriter(路径,是否追加(默认false))*/
public static void writer() {
try (Writer writer = new FileWriter("D:\\soft\\javawork\\javawork\\one\\four\\src\\com\\tianliang\\io0\\writer.txt")) {
writer.write(97);
String string = "你好";
writer.write(string);
char[] chars = {'A', 'S', 'G', 'H', 'T', 'E', 'F', 'd', 'g', 'b', 'n', 'm', '中', '国'};
writer.write(chars);
writer.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
InputStreamReader(字节输入转字符输入流)
处理字节乱码问题,也可以处理编码乱码问题
public static void main(String[] args) {
// 转换流,还可以通过设置字符编码解决乱码
try {
FileInputStream fis = new FileInputStream("E:/test.txt");
// 转换的时候,还可以指定字符编码,用于解决乱码问题
// 默认UTF-8
InputStreamReader isr = new InputStreamReader(fis,"GBK");
InputStreamReader isr = new InputStreamReader(fis,编码);
int temp = 0;
while ((temp = isr.read()) != -1) {
System.out.print((char)temp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try (
// 字节输入流
FileInputStream fis = new FileInputStream(路径);
// 转换为字符输入流,为什么不是FileReader类型呢?
// 因为FileReader 继承了InputStreamReader 所以一样
InputStreamReader isr = new InputStreamReader(fis);
){
// 和字符输入流使用方式一样
// 读取char数组
// isr.read(chars);
// 读取一个字符
// isr.read();
} catch (Exception e) {
e.printStackTrace();
}
}
OutputStreamWriter(字节输出转字符输出流)
public static void main(String[] args) {
try (
// 字节输出流
FileOutputStream fos = new FileOutputStream(路径);
// 转换为字符输出流
OutputStreamWriter osw = new OutputStreamWriter(fos);
){
// 和字符输出流用法一样
osw.write("xxx");
osw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
BufferedReader(字符输入缓冲流)
提高读取速度,增加按行读的方法readLine();剩余使用方法和字符输入流一样
public static void main(String[] args) {
try (
// 字符输入流,传入文件路径
FileReader fr = new FileReader(路径);
// 字符输入缓冲流,传入字符输入流
BufferedReader br = new BufferedReader(fr);
){
// 使用方式和字符输入流一样
// br.read();
// br.read(chars)
// 新增读取一行的方法,返回读取到的这一行数据
// 到达文件末尾,返回null
// String temp = br.readLine();
String temp = null;
while ((temp=br.readLine()) != null) {
System.out.println(temp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
BufferedWriter(字符输出缓存流)
增加写出速度,增加换行写出方法newLine();其余方式使用和字符输出流一样
public static void main(String[] args) {
try (
// 字符输出流,传入文件路径,和 是否覆盖写入
FileWriter fw = new FileWriter("./src/com/test.txt");
// 字符输出缓冲流,传入字符输出流对象
BufferedWriter bw = new BufferedWriter(fw);
){
// 用法和字符输出流一样
// bw.write(chars);
// bw.write(int);
// bw.write("xx");
// 新增写出换行的方法
bw.newLine();
// 刷缓存
bw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
测试字符流与字符缓存流速度
public static void main(String[] args) {
fileInputStreamTest(路径);
bufferedFileInputStreamTest(路径);
}
public static void fileInputStreamTest(String path){
long startTime = System.currentTimeMillis();
try (
FileInputStream fis =new FileInputStream(path);
){
int temp = 0;
byte[] bytes = new byte[1024];
while ((temp = fis.read(bytes)) != -1) {
}
} catch (Exception e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("读取完成,字节输入流耗时 : "+(endTime-startTime));
}
public static void bufferedFileInputStreamTest(String path){
long startTime = System.currentTimeMillis();
try (
FileInputStream fis =new FileInputStream(path);
BufferedInputStream bis = new BufferedInputStream(fis);
){
int temp = 0;
byte[] bytes = new byte[1024];
while ((temp = bis.read(bytes)) != -1) {
//写入时需要执行的内容
}
} catch (Exception e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("读取完成,字节输入缓冲流耗时 : "+(endTime-startTime));
}