IO流的分类:
流的方向: IO流:输入流-读取文件内容到程序 输出流-程序写出到文件
操作文件类型: IO流分为字节流(所有类型的文件)和字符流(纯文本文件)
FileOutputStream
FileOutputStream fos = new FileOutputStream("dir\\a.txt",false);//续写改为true
fos.write(97);
fos.close();
byte[] bytes = {97,98,99,100};
fos.write(bytes);
fos.write(bytes,1,2);
String str = "asdasdadasdasdadadas";
byte[] bytes = str.getBytes();
fos.write(bytes);
fos.close();
FileinputStream
FileinputStream fis = new FileinputStream("my\\a.xtxt");
int b1 = fis.read(); //一次读一个字节 读到文件末尾 read方法返回-1
文件拷贝
FileinputStream fis = new FileinputStream("my\\a.mp4");
FileOutputStream fos = new FileOutputStream("dir\\a.mp4");
int b;
while((b = fis.read()) != -1){
fos.write(b);
}
fos.close();
fis.close();
指定读取长度
FileinputStream fis = new FileinputStream("my\\a.txt");
byte[] bytes = new byte[2];
int len1 = fis.read(bytes);
System.out.println(len1); // 2
String str1 = new String(bytes,0,len1);
System.out.println(str1 );
FileinputStream fis = new FileinputStream("my\\a.mp4");
FileOutputStream fos = new FileOutputStream("dir\\a.mp4");
int len;
byte[] bytes = new byte[1024*1024*5];
while((len = fis.read(bytes )) != -1){
fos.write(bytes,0,len);
}
fos.close();
fis.close();
ASCII GBK
计算机最小存储单元是一个字节
ASCII 字符集中 一个英文占一个字节
GBK字符集兼容ASCII字符集
一个英文占一个字节,二进制第一位是0
一个中文占两个字节,二进制高位字节的第一位是1
unicode 字符集 :UTF-8 编码方式 英文一个字节 中文三个字节
编码:
String str = "你好啊啊实打实的";
byte[] bytes1 = str.getBytes();
System.out.println(Arrays.toString(bytes1));
byte[] bytes2 = str.getBytes("GBK");
System.out.println(Arrays.toString(bytes2));
解码:
String str2 = new String(bytes1);
SSystem.out.println(str2);
String str3 = new String(bytes1,"GBK");
SSystem.out.println(str3);
字符流 = 字节流+ 字符集
FileReader
创建对象
public FileReader(File file ) 创建字符输入流关联本地文件
public FileReader(String filepath ) 创建字符输入流关联本地文件
读取数据
public int read() 读取数据,读到末尾返回-1
public int read(char[] buffer) 读取数据,读到末尾返回-1
释放资源
public void close()
FileReader fr = new FileReader("asda\\a.txt") ;
int ch;
while((ch = fr.read()) != -1){
System.out.pringln((char)ch);
}
f.close()'
int len;
char[] chars= new char[2];
while((len = fr .read(chars)) != -1){
//把数组中的数据变成字符串再进行打印
System.out.pringln(new String(chars,0,len));
}
f.close()';
FileWriter
public FileWriter(File file ,boolean append);
public FileWriter(String filepath ,boolean append) ;
读
void write(int c) 写出一个字符
void write(String str) 写出一个字符串
void write(String str,int off,int len) 写出一个字符串的一部分
void write(char[] cbuf) 写出一个字符数组
void write(char[] cbuf,int off,int len) 写出一个字符数组的一部分
public void close()
FileWriter fw = new FileWriter("asda\\ass.txt")
char[] chars = {'a','b'}
fw.write(11111);
fw.write("念你打算大苏打色企鹅");
fw.write(chars)
文件拷贝
字节缓冲流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("my\\a.xtx"));
BufferedOutputStream bis = new BufferedOutputStream (new FileOuttputStream("my\\copy.xtx"))
int b ;
while((b = bis.read()) != -1){
bos.write(b);
}
bos.close();
bis.close();
2 拷贝 一次读写多个字节
byte[] bytes = new byte[1024];
int len;
while((len = bis.read(bytes)) != -1){
bos.write(bytes,0,len);
}
字符缓冲流
读
BufferedReader br = new BufferedReader(new FileReader("my\\a.xtxt"));
String line;
while(((line = br.readline()) != null)){
System.out.print(line);
}
br.close()
写
BufferedWriter bw = new BufferedWriter (new FileWriter("my\\a.xtxt",true));
bw.write("121");
bw.newline();
bw.write("12qqqqqq1");
bw.newline();
bw.close();
转换流: 字符流和字节流之间的桥梁
字符流下 转换输入流: InputStreamReader
转换输出流: OutputStreamWriter
利用转换流按照指定字符编码
1.创建对象并指定字符编码
InputStreamReader isr = new InputStreamReader (new FileInputStream("gbkfile.xtxt"),"GBK");
2.读取数据
int ch;
while(( ch = ist.read()) != -1){
System.out.println((char)ch);
}
isr.close();
JDK 11
指定编码读
FileReader fr = new FileReader("gbkfile.xtx",Charset.forName("GBK"));
int ch;
while((ch = fr.read()) != -1){
System.out.print((char)ch);
}
fr.close();
指定编码写
FileWriter fw = new FileWriter("x.txt",Charset.forName("GBK"));
fw.write("啊大大);
fw.close();
将本地文件中的GBK文件 转为UTF-8
JDK11以前
InputStreammReader isr = new InputStreammReader(new FileInputStream("b.txt"),"GBK");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputSTream("d.txt"),"UTF-8")
int b;
while((b = isr.read()) != -1){
osw.write(b);
}
osw.close();
isr.close();
2 替代方案
FileReader fr = new FileReader("b.txt",Charsset.forName("GBK"))
FileWriter fw = new FileWriter ("b.txt",Charsset.forName("UTF-8"))
int b;
while((b = fr.read()) != -1){
fw.write(b);
}
fw.closse();
fr.closse();
利用字节流读取文件中的数据,每次读一行,而且不能出现乱码
1.字节流再读取中文的时候,是会出现乱码的,但是字符流可以搞定
2.字节流里面是没有读一整行的方法的,只有字符缓冲流才能搞定
FileInputStream fis = new FileInputStream ("a.txt");
InputStreamReader isr = InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String str = br.readdline();
System.out.println(str);
br.close();
序列化流(对象操作输出流):可以把java中的对象写到本地文件中 属于字节流子类
public ObjectOutputStream( OutputStream out) 把基本流包装成高级流
public final void writeObjecet(Object obj) 把对象序列化(写出)到文件中去
Serializable 接口里面没有抽象方法,标记型接口
一旦实现了这个接口,那么就表示当前的Student类可以被序列化
Student stu = new Student("zhangsan",23);
ObjectoutputStream oos = new objectOutputStream(new FileOutputStream("a.txt"));
oos.writeObject(stu);
oos.close();
反序列化流:将序列化到本地文件中大的对象,读取到程序中来
ObjectInputStream( InputStream out ) 把基本流变成高级流
readObject() 把序列化到本地文件中的对象,读取到程序中来
1.创建反序列化流的对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
2 读取数据
Student o = (Student)ois.readObject();
3.打印对象
System.out.pringln(o);
ois.close();
注意:1.使用序列化流将对象写到文件时,需要将javabean 类实现Serializable 接口
否则会出现 NotSerializableException异常
2.序列化流写到文件中的数据是不能修改的,一旦修改就无法再次读回来
3.序列化对象后,修改了javabean类,再次反序列化,会不会有问题?
会抛出 Invalid Class Exception异常
解决方案:给javabean类添加serialVersionUID(序列号、版本号)
4.如果一个对象中的某个成员变量的值不想被序列化
解决方案:给该成员变量加transient关键字修饰,该关键字标记的成员变量不参与序列化过程
字节打印流:字节打印流 printStream
字符打印流 PrintWriter
特点: 1.只有写 没有读、只操作文件目的地,不操作数据源
2.特有的写方法可以实现,数据原样写出
3.特有方法 可以实现自动刷新 自动换行
打印一次数据 = 写出 + 换行+ 刷新
1.创建字节打印流的对象
PrintStream ps = new PrintStrean(new FileOutputStream("a.txt"),true,Charset.forName("UTF-8"))
写出数据
ps.println(97);写出+自动刷新+自动换行
ps.close();
2.1.创建字符打印流的对象
PrintWriter pw = new PrintWriter(new FileWriter("a.txt"),true)
pw.println("啊大苏打大苏打撒旦0");
pw.close();
解压缩流 压缩流