流的分类
流的分类:
普通流分类:
流向:
读取数据单元:
转换流:InputStreamReader,OutputStreamWriter
高级流:不能直接作用于文件,要装载普通流/转换流
缓冲流:BufferedReader
二进制流:
对象流:
写对象:
1、要求对象的类型必须是可序列化类型(实现了Serializable接口)
2、先有写入的目标流
3、创建对象流,把输出流装入
4、调用对象流的writeObject()把对象装入
5、关闭资源
读对象:
1、先有输入的源文件流
2、创建对象输入流,装入文件输入流
3、调用对象流的readObject()读取到对象
4、关闭资源
字符流读写文件
TestReader类:
package cn.kgc.kb09;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* @Author Daniu_Ben
* @Date 2020/8/4
* @Description 字符流读文件
*/
public class TeatReader {
public static void main(String[] args) throws IOException {
File file = new File("a.txt");
// FileReader fr = new FileReader(file);
// char[] c = new char[(int) file.length()];
// fr.read(c);
// System.out.println(c);
// fr.close();
String s = readFile("a.txt");
System.out.println(s);
}
public static String readFile(String path) {
File file = new File(path);
FileReader fr = null;
String str = null;
try {
fr = new FileReader(path);
char[]c = new char[(int) file.length()];
fr.read(c);
str = new String(c);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return str;
}
}
TestWriter类:
package cn.kgc.kb09;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* @Author Daniu_Ben
* @Date 2020/8/4
* @Description
*/
public class TestWriter {
public static void main(String[] args) {
String s = "\nHello World!!\n你好,世界!!";
String s1 ="a.txt";
writerFile(s,s1,true);
}
public static void writerFile(String str,String path,boolean isAppen){
File file = new File(path);
// char[] c = str.toCharArray();
FileWriter fw = null;
try {
fw = new FileWriter(path,isAppen);
fw.write(str);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
转换流
TestStreamReader类:
package cn.kgc.kb09;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @Author Daniu_Ben
* @Date 2020/8/4
* @Description 转换流
*/
public class TestStreamReader {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("a.txt");
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
char[] c = new char[fis.available()];
isr.read(c);
System.out.println(c);
}
}
二进制流
TestData类:
package cn.kgc.kb09;
import java.io.*;
/**
* @Author Daniu_Ben
* @Date 2020/8/4
* @Description 二进制流
*/
public class TestData {
public static void main(String[] args) throws IOException {
// FileInputStream fis = new FileInputStream("D:/KB09/1.jpg");
// DataInputStream dis = new DataInputStream(fis);
// byte [] b = new byte[fis.available()];
// dis.read(b);
// String s = new String(b);
// System.out.println(s);
// FileOutputStream fos = new FileOutputStream("jk.jbg");
// DataOutputStream dos = new DataOutputStream(fos);
// dos.write(b);
// dos.close();
// fos.close();
// dis.close();
// fis.close();
copyData("D:/KB09/1.jpg","jk.jpg");
}
public static void copyData(String fromPath, String targetPath){
FileInputStream fis = null;
DataInputStream dis = null;
FileOutputStream fos = null;
DataOutputStream dos = null;
try {
fis = new FileInputStream(fromPath);
dis = new DataInputStream(fis);
fos = new FileOutputStream(targetPath);
dos = new DataOutputStream(fos);
int tmp ;
while ((tmp = dis.read()) != -1){
dos.write(tmp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
缓冲流
TestBuffer类:
package cn.kgc.kb09;
import java.io.*;
/**
* @Author Daniu_Ben
* @Date 2020/8/4
* @Description
*/
public class TestBuffer {
public static void main(String[] args) {
// String s = readBuffer("a.txt");
String str = "您好,我的名字是{name},我是一只{type},我的主人是{master}";
writeBuffer(str,"a.txt",false);
System.out.println("替换前:\n" + readBuffer("a.txt"));
StringBuffer sb = new StringBuffer(str);
sb.replace(sb.indexOf("{"),sb.indexOf("}") + 1,"欧欧");
sb.replace(sb.indexOf("{"),sb.indexOf("}") + 1,"狗狗");
sb.replace(sb.indexOf("{"),sb.indexOf("}") + 1,"李伟");
String s = sb.toString();
writeBuffer(s,"a.txt",false);
System.out.println("替换后:\n" + readBuffer("a.txt"));
// System.out.println(sb);
// System.out.println(s);
// writeBuffer("\r\nHello world!\r\n你好,世界!!","a.txt",true);
}
public static String readBuffer(String path){
File f = new File(path);
String str = null;
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(f);
br = new BufferedReader(fr);
String s;
StringBuffer sb = new StringBuffer();
while((s = br.readLine()) != null){
sb.append(s + "\r\n");
}
str = sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return str;
}
public static void writeBuffer(String str,String path,boolean isAppend){
File f = new File(path);
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(f,isAppend);
bw = new BufferedWriter(fw);
bw.write(str);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
对象流
Student类:
package cn.kgc.kb09.test;
import java.io.Serializable;
import java.util.Date;
/**
* @Author Daniu_Ben
* @Date 2020/8/4
* @Description
*/
public class Student implements Serializable {//此接口告诉内存,此类按照java规定排序
private int stuId;
private String stuName;
private String gender;
private Date birthday;
public Student() {
}
public Student(int stuId, String stuName, String gerder, Date birthday) {
this.stuId = stuId;
this.stuName = stuName;
this.gender = gerder;
this.birthday = birthday;
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student{" +
"stuId=" + stuId +
", stuName='" + stuName + '\'' +
", gender='" + gender + '\'' +
", birthday=" + birthday +
'}';
}
}
TestObject类:
package cn.kgc.kb09.test;
import java.io.*;
import java.util.Date;
/**
* @Author Daniu_Ben
* @Date 2020/8/4
* @Description 对象流
*/
public class TestObject {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Student s = new Student(1,"jk","女",new Date());
FileOutputStream fos = new FileOutputStream("obj.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(s);
oos.close();
fos.close();
// System.out.println(s);
FileInputStream fis = new FileInputStream("obj2.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
System.out.println(o);
ois.close();
fis.close();
}
}