IO流的读写
分为四大类:字节读取,字节写入,字符读取,字符写入,字节适合任意文件,字符只适合纯文本文件
原始流使用
字节读取
//字节读取
import java.io.*;
public class Inputsreamdemo {
public static void main(String[] args) throws IOException {
InputStream f = new FileInputStream("file-io-app\\src\\1234.txt") ;
// int a = f.read();//每次读取一个字节,因此无法解析中文
// System.out.println((char)a);
// byte[] buffer = new byte[1024];
// int a = f.read(buffer);//返回所有字节长度,每次截取三个字节放入buffer数组里面
// System.out.println(new String(buffer));//但无法解决中文件乱码的
// byte[] buffer1 = f.readAllBytes();//jdk9
// System.out.println(new String(buffer1)); //一次性读取所有字节
// readAllBytest等同于以下代码,定义一个与文件大小样的数组
File file = new File("file-io-app\\src\\1234.txt") ;
InputStream is = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
int b = is.read(buffer);
System.out.print(new String(buffer));
}
}
字节写入
import java.io.FileOutputStream;
import java.io.OutputStream;
public class Outstreamdemo {
public static void main(String[] args) throws Exception {
OutputStream out = new FileOutputStream("file-io-app\\src\\out.txt",true);//true表示追加数据,不清空原数据
out.write('a'); //一个字节写入,不支持中文
byte[] buffer = {'a',97,98,99};
out.write(buffer); //字节数组写入,不支持中文
out.write("\r\n".getBytes()); //换行符
String name = "我爱你中国";
out.write(name.getBytes()); //将中文转成字节数组再写入,就可以写入中文
out.flush();//刷新,将内存数据及时写入磁盘,close本身包含刷新
out.close();
}
}
文件复制
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class Filecopy {
public static void main(String[] args) {
filecopy("D:\\KwDownload\\song\\新建文本文档.txt","D:\\zhengqinjie\\新建文件夹\\1建.txt");
}
private static void filecopy(String tagetFile, String afterCopyFile) {
try {
InputStream in = new FileInputStream(tagetFile);
OutputStream out = new FileOutputStream(afterCopyFile);
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.close();
in.close();
System.out.println("复制完成");
}catch (Exception e ){
e.printStackTrace();
}
}
}
字符流读取,适合于文本
package com.ruqi;
import javax.print.DocFlavor;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
public class ReadChar {
public static void main(String[] args) {
System.out.println(readchar("file-io-app\\src\\ou1t.txt"));
}
private static String readchar(String filepath) {
// 传入文件路径
try(Reader re = new FileReader(filepath);){
char[] buffer = new char[1024];
int len;
String readed ="";
while ((len = re.read(buffer)) != -1){
readed += new String(buffer,0,len);
return readed;
}
} catch (Exception e) {
e.printStackTrace();
}
return "文件读取失败";
}
}
写入文本
import java.io.FileWriter;
import java.io.Writer;
public class CharWrite {
public static void main(String[] args) {
try(
Writer fw = new FileWriter("file-io-app/src/write.txt",true);
){
fw.write("直接写入文本内容即可");
}catch (Exception e){
e.printStackTrace();
}
}
}
缓冲流,性能较好
原始流是每个流一个个往内存加载最终输出,缓冲流就是不断将数据加载缓冲流,缓冲流不断向内存输出8kb的流,接着直接不断读取该8kb的流,因此在内存读取,性能得到了提升
字节缓冲流
import java.io.*;
public class Bufferinput {
public static void main(String[] args) {
filecopy("file-io-app\\src\\1234.jpg","file-io-app\\src\\12345.jpg");
}
private static void filecopy(String tagetFile, String afterCopyFile) {
try (InputStream in = new FileInputStream(tagetFile);
InputStream bin = new BufferedInputStream(in);//将原始流包装成缓冲流即可,其用法一致
OutputStream out = new FileOutputStream(afterCopyFile);
OutputStream bout = new BufferedOutputStream(out);//将原始流包装成缓冲流即可,其用法一致
){
byte[] buffer = new byte[1024];
int len;
while ((len = bin.read(buffer)) != -1) {
bout.write(buffer, 0, len);
}
System.out.println("复制完成");
}catch (Exception e ){
e.printStackTrace();
}
}
}
字符缓冲
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Writer;
public class BufferChar {
public static void main(String[] args) {
try(
Writer fw = new FileWriter("file-io-app/src/write.txt",true);
BufferedWriter bfw = new BufferedWriter(fw);
){
bfw.write("直接写入文本内容即可");
bfw.newLine();//换行
}catch (Exception e){
e.printStackTrace();
}
try(Reader f = new FileReader("file-io-app\\src\\write.txt");
BufferedReader bf = new BufferedReader(f);){
System.out.println(bf.readLine());
}catch (Exception e ){
e.printStackTrace();
System.out.println("缓冲字符读取");
}
}
}
编码转换
import java.io.*;
public class Transer {
public static void main(String[] args) throws Exception {
//读取
//无论是什么格式的文件,原始流是不会有编码问题
InputStream in = new FileInputStream("C:\\Users\\12281\\Desktop\\111.txt");
//将字节流转换成字符流,且可以指定编码格式
Reader transerin = new InputStreamReader(in,"GBK");//编码转化流
BufferedReader btranserin = new BufferedReader(transerin);
String line;
while ((line = btranserin.readLine()) != null)
System.out.println(line);
btranserin.close();
//写入
//按指定编码格式进行字节写入
OutputStream out = new FileOutputStream("file-io-app\\src\\write.txt");
Writer tout = new OutputStreamWriter(out,"GBK");//编码转化流
BufferedWriter btout = new BufferedWriter(tout);
btout.write("我爱你,中国");
btout.close();
}
}