数据操作单位不同:字节流(8 bit),字符流(16 bit)
流向不同:输入流,输出流
流的角色不同:节点流,处理流
抽象基类 | 字节流 | 字符流 |
---|---|---|
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
java中涉及到的io流基本都是从以上四个抽象基类派生出来的,其子类都是以其父类的名字做后缀。
public class TestFileReader {
public static void main(String[] args) {
//1.实例化file类对象,指明要操作的文件
File file = new File("D:\\IdeaCode\\hello.txt");
//2.数据的读入
FileReader fr = null;
try {
fr = new FileReader(file);
//3.数据读入
int data;
while ((data = fr.read()) != -1){
System.out.print((char)data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//4.关闭流
if (fr != null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 1.文件不存在则新建
* 2.可以在创建文件流的时候指定是否在源文件内容上追加或覆盖
* @author Wannengqingnian
*/
public class TestFileWriter {
public static void main(String[] args) {
File file = new File("D:\\IdeaCode\\hello.txt");
FileWriter fw = null;
try {
fw = new FileWriter(file, true);
fw.write("中文输入\n");
fw.write("asd");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class TestFileInputStream {
public static void main(String[] args) {
File file = new File("D:\\IdeaCode\\hello.txt");
FileInputStream fi = null;
try {
fi = new FileInputStream(file);
byte[] buffer = new byte[5];
int len;
while ((len = fi.read(buffer)) != -1){
String str = new String(buffer, 0, len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fi != null){
try {
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 实现文件拷贝
* 拷贝完文件大小相同
* @author Wannengqingnian
*/
public class CopyByFileStreamDemo {
public static void main(String[] args) {
File srcFile = new File("D:\\IdeaCode\\hello.txt");
File destFile = new File("D:\\IdeaCode\\hello2.txt");
FileInputStream fis = null;
FileOutputStream fio = null;
try {
//获取文输入输出流
fis = new FileInputStream(srcFile);
fio = new FileOutputStream(destFile);
//创建字节数组,保存数据
byte[] buffer = new byte[5];
//循环取数据,写数据
int len;
while ((len = fis.read(buffer)) != -1){
fio.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭连接资源
if (fio != null){
try {
fio.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
1.按角色分属于处理流
2.本质属于字符流
作用:提供字节流到字符流之间的转换功能
InputStreamReader:将一个字节输入流转换为字符输入流
OutputStreamWriter:将字符输出流转换为字节输出流
public class TestInputStreamReader {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("D:\\IdeaCode\\hello.txt");
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) != -1){
String str = new String(cbuf, 0, len);
System.out.println(str);
}
isr.close();
}
}
/**
* 标准输入流
System.in 标准输入流
System.out 标准输出流
* @author Wannengqingnian
*/
public class TestSystem {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
while (true){
System.out.println("请输入字符串:");
String data = br.readLine();
if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)){
System.out.println("程序结束");
break;
}
String upperCase = data.toUpperCase();
System.out.println(upperCase);
}
br.close();
isr.close();
}
}
PrintStream:字节打印流
PrintWriter:字符打印流
在JDK1.5之后,JAVA又对PrintStream类进行了扩充,增加了格式化的输出方式,直接使用printf()方法就可以完成操作,但是在进行格式化输出的时候需要指定其输出的数据类型。
/**
* 用于把想要保存的信息保存到文本中
可以打印任何的数据类型,例如:小数、整数、字符串等等
* @author Wannengqingnian
*/
public class TestPrintStream {
public static void main(String[] args) throws FileNotFoundException {
FileOutputStream fos = new FileOutputStream("D:\\IdeaCode\\hello3.txt");
PrintStream ps = new PrintStream(fos);
String name = "zmx";
ps.println(name);
ps.close();
}
}
/**
* 数据流
* DataInputSteam和DataOutputStream
* 作用:用于读取或写入基本数据类型的变量或字符串
* @author Wannengqingnian
*/
public class TestDataStream {
public static void main(String[] args) throws IOException {
File file = new File("D:\\IdeaCode\\hello.txt");
FileOutputStream fos = new FileOutputStream(file);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeUTF("万能青年");
//刷新操作,把内存中的数据写入文件
dos.flush();
dos.writeInt(123);
dos.flush();
dos.writeBoolean(true);
dos.flush();
dos.close();
/**
* 写入操作
读入数据时需要按照写入顺序来读取
*/
File file1 = new File("D:\\IdeaCode\\hello.txt");
FileInputStream fis = new FileInputStream(file1);
DataInputStream dis = new DataInputStream(fis);
String name = dis.readUTF();
int age = dis.readInt();
boolean flag = dis.readBoolean();
System.out.println("name = " + name);
System.out.println("age = " + age);
System.out.println("flag = " + flag);
}
}
十、对象流
对象的输入输出流的作用:用于写入对象 的信息和读取对象的信息。使得对象持久化。 ObjectInputStream : 对象输入流 ObjectOutPutStream :对象输出流
继续就是序列化:
https://juejin.im/post/5ce3cdc8e51d45777b1a3cdf