• IO流的字符流+对象流+打印流+内存流+随机访问流(2)


    IO流的字符流+对象流+打印流+内存流+随机访问流(2)

    一、字符流

    1.利用字符输出转换流 向文件写入数据

    1.文件存在的情况
    2.文件不存在的情况

    经验:所有的输出流,当文件不存在时都会创建文件

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    
    public class Test01 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//1.创建流对象
    		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("ggc.txt"));
    		
    		//2.写入数据
    		//osw.write(97);//写入ASCII
    		//char[] cs = {'a','b','c','我','爱','你'};
    		//osw.write(cs);//写入数组
    		//osw.write(cs,2,3);//写入字符数组,偏移量,长度
    		
    		//osw.write("abc我爱你");//写入字符串
    		osw.write("abc我爱你", 3, 3);//写入字符串,偏移量,长度
    		
    		//3.关闭资源
    		osw.close();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    3.在文件末尾追加
    方案一
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    
    public class Test02 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//1.创建流对象
    		OutputStreamWriter ows = new OutputStreamWriter(new FileOutputStream("ggc.txt",true));
    		
    		//2.写入数据
    		ows.write("abx我爱你",2,3);//写入字符串,偏移量,长度
    		
    		//3.关闭资源
    		ows.close();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    方案二
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.FileNotFoundException;
    public class Test03 {
    
    	public static void main(String[] args) {
    		
    		
    		OutputStreamWriter ows = null;
    		try {
    			//1.创建流对象 + 指定编码格式
    			//osw = new OutputStreamWriter(new FileOutputStream("hhy.txt",true),Charset.forName("UTF-8"));
    			ows = new OutputStreamWriter(new FileOutputStream("ggc.txt",true),"GBK");
    			//2.写入数据
    			ows.write("abx我爱你",2,3);//写入字符串,偏移量,长度
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}finally {
    			//3.关闭资源
    			if (ows != null) {
    				try {
    					ows.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    		
    
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    2.利用字符输入转换流 读取文件里的数据

    1.文件存在的情况
    2.文件不存在的情况

    经验:所有的输入流,当文件不存在时都会报文件未找到异常 – FileNotFoundException

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Test04 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//1.创建流对象
    		InputStreamReader isr = new InputStreamReader(new FileInputStream("ggc.txt"));
    		
    		//2.读取数据
    		//isr.read(); -- 读取的是字符的ASCII,读取到文件末尾就返回-1
    		int read = isr.read();
    		System.out.println((char)read);
    	    read = isr.read();
    	    System.out.println((char)read);
    	    read = isr.read();
    		System.out.println((char)read);
    		read = isr.read();
    		System.out.println((char)read);
    		read = isr.read();
    		System.out.println((char)read);
    	    read = isr.read();
    	    System.out.println(read);
    	    
    	    //3.关闭资源
    	    isr.close();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    改进方案1
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Test05 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//1.创建数据
    		InputStreamReader isr = new InputStreamReader(new FileInputStream("ggc.txt"));
    		
    		//2.读取数据
    		int read;
    		while ((read = isr.read()) != -1) {
    			System.out.println((char)read);
    		}
    		
    		//3.关资源
    		isr.close();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    改进方案2
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Test06 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//1.创建流对象
    		InputStreamReader isr = new InputStreamReader(new FileInputStream("ggc.txt"));
    		
    		//2.读取数据
    		//len = isr.read(cs) -- 读取cs长度的数据,并把数据存入到字符数组中并返回读取到的有效字符数,如果读取到文件末尾则返回-1
    		char[] cs = new char[1024];
    		int len;
    		while ((len = isr.read(cs)) != -1) {
    			System.out.println(new String(cs, 0, len));
    		}
    		
    		//3.关闭资源
    		isr.close();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    3.积极处理异常
    4.设置指定编码格式
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.nio.charset.Charset;
    
    public class Test07 {
    
    	public static void main(String[] args){
    		
    		
    		InputStreamReader isr = null;
    		try {
    			//1.创建流对象
    			//isr = new InputStreamReader(new FileInputStream("ggc.txt"),"GBK");
    			isr = new InputStreamReader(new FileInputStream("ggc.txt"),Charset.forName("UTF-8"));
    			//2.读取数据
    			//len = isr.read(cs) -- 读取cs长度的数据,并把数据存入到字符数组中并返回读取到的有效字符数,如果读取到文件末尾则返回-1
    			char[] cs = new char[1024];
    			int len;
    			while ((len = isr.read(cs)) != -1) {
    				System.out.println(new String(cs, 0, len));
    			}
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}finally {
    			//3.关闭资源
    			if (isr != null) {
    				try {
    					isr.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    5.拷贝文本文件
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    
    public class Copy {
    
    	public static void main(String[] args) throws IOException {
    		
    		InputStreamReader isr = new InputStreamReader(new FileInputStream("ggc.txt"),"GBK");
    		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("copy.txt"),"GBK");
    		
    		char[] cs = new char[1024];
    		int len;
    		while ((len = isr.read(cs)) != -1) {
    			osw.write(cs, 0, len);
    		}
    		isr.close();
    		osw.close();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3.利用文件字符输出流 向文件写入数据

    1.文件存在的情况
    2.文件不存在的情况

    经验:所有的输出流,当文件不存在时都会创建文件

    3.在文件末尾追加
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Test01 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//1.创建流的对象
    		//FileWriter fw = new FileWriter("ggc.txt");
    		
    		//1.创建流对象 + 末尾追加
    		FileWriter fw = new FileWriter("ggc.txt",true);
    		
    		//2.写入数据
    		fw.write("123我爱你");
    		
    		//3.关闭资源
    		fw.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    4.利用文件字符输入流 读取文件里的数据

    1.文件存在的情况
    2.文件不存在的情况

    经验:所有的输入流,当文件不存在时都会报文件未找到异常 – FileNotFoundException

    import java.io.FileReader;
    import java.io.IOException;
    
    public class Test02 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//1.创建流的对象
    		FileReader fs = new FileReader("ggc.txt");
    		
    		//2.读取数据
    		char[] cs = new char[1024];
    		int len;
    		while ((len = fs.read(cs)) != -1) {
    			System.out.println(new String(cs, 0, len));
    		}
    		//3.关闭资源
    		fs.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    3.拷贝文本文件
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Copy {
    
    	public static void main(String[] args) throws IOException {
    		
    		FileReader fr = new FileReader("IO笔记.txt");
    		FileWriter fw = new FileWriter("copy.txt");
    	
    		char[] cs = new char[1024];
    		int len;
    		while((len = fr.read(cs)) != -1){
    			fw.write(cs, 0, len);
    		}
    		
    		fr.close();
    		fw.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    5.利用带有缓冲区的字符输出流 向文件写入数据

    1.文件存在的情况
    2.文件不存在的情况

    经验:所有的输出流,当文件不存在时都会创建文件

    3.在文件末尾追加
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Test01 {
    
    	public static void main(String[] args) throws IOException {
    
    		//1.创建流对象(文件字符输出流 --> 带有缓冲区的字符输出流)
    		//默认缓冲区大小:8192字符
    		//BufferedWriter bw = new BufferedWriter(new FileWriter("ggc.txt"));
    
    		//1.创建流对象(文件字节输出流 --> 字符输出转换流 --> 带有缓冲区的字符输出流)
    		//默认缓冲区大小:8192字符
    		//BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("ggc.txt"),"GBK"));
    
    		//1.创建流对象(文件字符输出流 --> 带有缓冲区的字符输出流) + 末尾追加
    		//默认缓冲区大小:8192字符
    		//BufferedWriter bw = new BufferedWriter(new FileWriter("hhy.txt",true));
    
    		//1.创建流对象(文件字节输出流 --> 字符输出转换流 --> 带有缓冲区的字符输出流) + 末尾追加
    		//默认缓冲区大小:8192字符
    		//BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("hhy.txt",true),"GBK"));
    
    		//1.创建流对象(文件字符输出流 --> 带有缓冲区的字符输出流)
    		//自定义缓冲区大小:2048字符
    		BufferedWriter bw = new BufferedWriter(new FileWriter("hhy.txt"),2048);		
    		
    		//2.写入数据
    		bw.write("123abc我爱你");
    		
    		//3.关闭资源
    		bw.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    6.利用带有缓冲去的字符输入流 读取文件里的数据

    1.文件存在的情况
    2.文件不存在的情况

    经验:所有的输入流,当文件不存在时都会报文件未找到异常 – FileNotFoundException

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class Test02 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//1.创建流对象(文件字符输入流 --> 带有缓冲区的字符输入流)
    		//默认缓冲区大小:8192字符
    		//BufferedReader br = new BufferedReader(new FileReader("hhy.txt"));
    		
    		//1.创建流对象(文件字节输入流 --> 字符输入转换流 --> 带有缓冲区的字符输入流)
    		//默认缓冲区大小:8192字符
    		//BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("hhy.txt"), "GBk"));
    		
    		//1.创建流对象(文件字符输入流 --> 带有缓冲区的字符输入流)
    		//自定义缓冲区大小:2048字符
    		BufferedReader br = new BufferedReader(new FileReader("hhy.txt"),2048);
    		
    		//2.读取数据
    		char[] cs = new char[1024];
    		int len;
    		while ((len = br.read(cs)) != -1) {
    			System.out.println(new String(cs, 0, len));
    		}
    		
    		//3.关闭资源
    		br.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    3.拷贝文本文件
    方案一
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Copy01 {
    
    	public static void main(String[] args) throws IOException {
    		
    		BufferedReader br = new BufferedReader(new FileReader("ggc.txt"));
    		BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));
    		
    		char[] cs = new char[1024];
    		int len;
    		while ((len = br.read(cs)) != -1) {
    			bw.write(cs, 0, len);
    		}
    		
    		br.close();
    		bw.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    方案二
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Copy02 {
    
    	public static void main(String[] args) throws IOException {
    		
    		BufferedReader br = new BufferedReader(new FileReader("ggc.txt"));
    		BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));
    		
    		//br.readLine() -- 读取一行数据,如果读取到文件末尾则返回null
    		String readLine;
    		while ((readLine = br.readLine()) != null) {
    			bw.write(readLine);
    			bw.newLine();//换行
    		}
    		br.close();
    		bw.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    7.字符流总结

    应用场景:处理文本文件
    abstract class Reader -- 字符输入流的基类(抽象类)
    abstract class Writer -- 字符输出流的基类(抽象类)
    
    class InputStreamReader extends Reader -- 字符输入转换流
    class OutputStreamWriter extends Writer - 字符输出转换流
    作用:将字节流转换为字符流
    
    class FileReader extends InputStreamReader -- 文件字符输入流
    class FileWriter extends OutputStreamWriter - 文件字符输出流
    
    class BufferedReader extends Reader -- 带缓冲区的字符输入流 
    class BufferedWriter extends Writer -- 带缓冲区的字符输出流
    缓冲区大小为:8192字符
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    二、对象流

    1.作用:

    可以将程序中的对象写入到文件里,也可以将文件中的对象取出到程序中

    2.概念:

    1.序列化/钝化:将程序中的对象写入到文件中的过程
    2.反序列化/活化:将文件中的对象读取到程序中的过程
    3.对象要想通过对象输入流写入到文件中,对象所属的类就必须实现序列化接口(Serializable)
    4.序列化接口中没有任何的代码,这种接口称之为标记型接口
    5.transient修饰了属性,该属性不会随着对象而写入到文件中
    6.static修饰了属性,该属性不会随着对象而写入到文件中(静态属性不属于对象的属性)

    	class ObjectInputStream --- 对象输入流
    	class ObjectOutputStream -- 对象输出流
    
    • 1
    • 2

    3.利用对象输出流 向文件写入数据

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.util.Date;
    
    public class Test01 {
    
    	public static void main(String[] args) throws FileNotFoundException, IOException {
    		
    		//1.创建流的对象
    		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ggc.txt"));
    		
    		//2.写入数据
    		oos.writeInt(100);
    		oos.writeDouble(123.123);
    		oos.writeBoolean(true);
    		oos.writeUTF("abc123我爱你");
    		oos.writeObject(new Date());
    		
    		//3.关闭资源
    		oos.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    4.利用对象输入流 读取文件里的数据

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.util.Date;
    
    public class Test02 {
    
    	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    		
    		//1.创建流的对象
    		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("ggc.txt"));
    		
    		//2.读取数据
    		int readInt = ois.readInt();
    		double readDouble = ois.readDouble();
    		boolean readBoolean = ois.readBoolean();
    		String readUTF = ois.readUTF();
    		Date date = (Date) ois.readObject();
    		
    		
    		System.out.println(readInt);
    		System.out.println(readDouble);
    		System.out.println(readBoolean);
    		System.out.println(readUTF);
    		System.out.println(date);
    		
    		//3.关闭资源
    		ois.close();
    		
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    5.利用对象输出流 向文件写入 自定义对象

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    
    public class Test03 {
    
    	public static void main(String[] args) throws FileNotFoundException, IOException {
    		
    		//1.创建流的对象
    		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ggc.txt"));
    		
    		//2.写入数据
    		oos.writeObject(new User("12345678","123456","卧龙","诸葛亮",1000,300));
    		oos.writeObject(new User("12345679","123455","大都督","周瑜",1000,350));
    		oos.writeObject(new User("12345677","123444","江东小霸王","孙策",2000,500));
    		oos.writeObject(new User("12345676","123333","常山赵子龙","赵云",1000,400));
    		oos.writeObject(null);
    		
    		//3.关闭资源
    		oos.close();
    	}
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    import java.io.Serializable;
    
    public class User implements Serializable{
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private String username;
    	private String password;
    	private String name;
    	private String role;
    	private double hp;
    	private double mp;
    	
    	public User() {
    	}
    
    	public User(String username, String password, String name, String role, double hp, double mp) {
    		this.username = username;
    		this.password = password;
    		this.name = name;
    		this.role = role;
    		this.hp = hp;
    		this.mp = mp;
    	}
    
    	public String getUsername() {
    		return username;
    	}
    
    	public void setUsername(String username) {
    		this.username = username;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getRole() {
    		return role;
    	}
    
    	public void setRole(String role) {
    		this.role = role;
    	}
    
    	public double getHp() {
    		return hp;
    	}
    
    	public void setHp(double hp) {
    		this.hp = hp;
    	}
    
    	public double getMp() {
    		return mp;
    	}
    
    	public void setMp(double mp) {
    		this.mp = mp;
    	}
    
    	@Override
    	public String toString() {
    		return "User [username=" + username + ", password=" + password + ", name=" + name + ", role=" + role + ", hp="
    				+ hp + ", mp=" + mp + "]";
    	}
    
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84

    6.利用对象输入流 读取文件里的自定义对象

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    
    public class Test04 {
    
    	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    		//1.创建流对象
    		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("ggc.txt"));
    		
    		//2.读取数据
    		User user;
    		while ((user =(User) ois.readObject()) != null) {
    			System.out.println(user);
    		}
    		
    		//3.关闭资源
    		ois.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    三、内存流

    1.含义:将数据存储在内存中

    注意:内存流是关闭不掉的(因为程序员没有权限关闭流到内存的通道)
    应用场景:频繁使用的数据不会存入文件中,因为内存与硬盘交互的越多程序性能越低,所以可以将频繁使用的数据通过内存流存入到内存中

    	class ByteArrayInputStream --- 内存输入流
    	class ByteArrayOutputStream -- 内存输出流
    
    • 1
    • 2

    2.使用内存输出流

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    
    public class Test01 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//1.创建流的对象
    		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    		
    		//2.写入数据
    		baos.write("123abc".getBytes());
    		
    		//获取对象中的数据 -- 以字节数组的形式
    		byte[] byteArray = baos.toByteArray();
    		System.out.println(new String(byteArray));
    		
    		//获取对象中的数据 -- 以字符串的形式
    		String string = baos.toString();
    		System.out.println(string);
    		
    		//内存流是关闭不掉的(因为程序员没有权限关闭流到内存的通道)
    		baos.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    3.使用内存输入流

    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    
    public class Test02 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//1.创建流对象(创建流时,将数据存储到流对象中)
    		ByteArrayInputStream bais = new ByteArrayInputStream("123abcd".getBytes());
    		
    		//2.读取数据
    		byte[] bs = new byte[1024];
    		int len;
    		while ((len = bais.read(bs)) != -1) {
    			System.out.println(new String(bs, 0, len));
    		}
    		//内存流是关闭不掉的(因为程序员没有权限关闭流到内存的通道)
    		bais.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    四、打印流

    	class PrintStream -- 字节打印流 (输出流)
    		注意:可以将字节流转换为字节打印流
    	class PrintWriter -- 字符打印流 (输出流)
    		注意:可以将字节流/字符流转换为字符打印流
    	注意:系统标准输入、输出、错误输出流的重定向
    
    • 1
    • 2
    • 3
    • 4
    • 5
    
    
    • 1

    1.使用字节打印流

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    
    public class Test01 {
    
    	public static void main(String[] args) throws FileNotFoundException {
    		
    		//1.创建流的对象
    		//PrintStream ps = new PrintStream("ggc.txt");
    		
    		//1.创建流对象(文件字节输出流 --> 字节打印流) + 末尾追加内容
    		
    		PrintStream ps = new PrintStream(new FileOutputStream("ggc.txt"));
    		
    		//2.写入数据
    		ps.println("123456adc我爱你");
    		
    		//3.关闭资源
    		ps.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    2.使用字符打印流

    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class Test02 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//1.创建流的对象
    		//PrintWriter pw = new PrintWriter("ggc.txt");
    		
    		//1.创建流对象(文件字节输出流 --> 字符打印流) + 末尾追加内容
    		//PrintWriter pw = new PrintWriter(new FileOutputStream("ggc.txt",true));
    		
    		//1.创建流对象(文件字符输出流 --> 字符打印流) + 末尾追加内容
    		PrintWriter pw = new PrintWriter(new FileWriter("ggc.txt",true));
    		
    		//2.写入数据
    		pw.println("123abc牛头人");
    		
    		//3.关闭资源
    		pw.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    五、重定向

    1.含义:

    重新定义系统标准的输入流/输出流及错误输出流的方向

    2.系统标准的输入流(方向:控制台->程序)

    重定向 – System.setIn(new FileInputStream(“hhy.txt”));

    重定向后(方向:文件->程序)

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Test03 {
    
    	public static void main(String[] args) throws IOException {
    		//重定向
    		System.setIn(new FileInputStream("ggc.txt"));
    		
    		Scanner scan = new Scanner(System.in);
    		String next = scan.next();
    		System.out.println(next);
    		scan.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    3.系统标准的输出流(方向:程序->控制台)

    重定向 – System.setOut(new PrintStream(“hhy.txt”));

    重定向后(方向:程序->文件)

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintStream;
    
    public class Test04 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//重定向
    		System.setOut(new PrintStream(new FileOutputStream("ggc.txt",true)));
    		
    		PrintStream out = System.out;
    		out.println("小赵小可爱,皇冠给你带~~~");
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    4.系统标准的错误输出流(方向:程序->控制台)

    重定向 – System.setErr(new PrintStream(“hhy.txt”));

    重定向后(方向:程序->文件)

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintStream;
    
    public class Test05 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//重定向
    		System.setErr(new PrintStream(new FileOutputStream("ggc.txt",true)));
    		
    		PrintStream err = System.err;
    		err.println("小赵小可爱,皇冠给你带~~~");
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    六、随机访问流

    1.含义:

    该类认为文件是一个大型的byte数组,底层由隐藏的指针(下标),指针默认为0(可以设置指针的位置),
    可以从指针的位置开始读取或写入(说明该流有两个方向)

    2.模式:

      	r -- 读
      				rw - 读写
    
    • 1
    • 2

    3.利用随机访问流 向文件写入数据

    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    public class Test01 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//1.创建流的对象
    		RandomAccessFile r = new RandomAccessFile("ggc.txt","rw");
    		
    		//2.写入数据
    		r.write("123abc我爱你".getBytes());
    		
    		//3.关闭资源
    		r.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    4.利用随机访问流 向文件写入数据在文件末尾追加

    import java.io.File;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    public class Test02 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//1.创建流的对象
    		File file = new File("ggc.txt");
    		RandomAccessFile raf = new RandomAccessFile(file, "rw");
    		
    		//设置指针位置
    		raf.seek(file.length());//将指针设置到文件末尾
    		
    		//2.读取数据
    		raf.write("123abcd我爱你哟".getBytes());
    		
    		//3.关闭资源
    		raf.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    5.利用随机访问流 读取文件里的数据

    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    public class Test03 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//1.创建流对象
    		RandomAccessFile raf = new RandomAccessFile("ggc.txt", "r");
    		
    		//2.读取数据
    		byte[] b = new byte[1024];
    		int len;
    		while ((len = raf.read(b)) != -1) {
    			System.out.println(new String(b, 0, len));
    		}
    		//3.关闭资源
    		raf.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    6.利用随机访问流 读取文件里的数据从英文处开始读取

    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    public class Test04 {
    
    	public static void main(String[] args) throws IOException {
    		
    		//1.创建流的对象
    		RandomAccessFile raf = new RandomAccessFile("ggc.txt", "r");
    		
    		//设置文件指针
    		raf.seek(3);
    		
    		//2.读取数据
    		byte[] b = new byte[1024];
    		int len;
    		while ((len = raf.read(b)) != -1) {
    			System.out.println(new String(b, 0, len));
    		}
    		
    		//3.关闭资源
    		raf.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    7.拷贝文本文件

    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Copy {
    
    	public static void main(String[] args) throws IOException {
    		
    		FileReader fr = new FileReader("ggc.txt");
    		FileWriter fw = new FileWriter("copy.txt");
    		
    		char[] cs = new char[1024];
    		int len;
    		while ((len = fr.read(cs)) != -1) {
    			fw.write(cs, 0, len);
    		}
    		fr.close();
    		fw.close();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    从0开始启动一个Django的docker服务
    IDEA使用 Alibaba Cloud Toolkit 插件 自动打包部署maven项目至服务器
    网络安全管理与运维服务
    【DockerCE】Docker-CE 24.0.6正式版发布
    【LINUX】统计liunx进程的内存使用情况
    使用binlog2sql工具闪回恢复被误删除的数据实战
    2022年重庆自考本科怎么报考,需要哪些条件?
    JavaFx 生成二维码工具类封装
    当前系统并无桌面环境,或无显示器,无法显示远程桌面,您需要自行安装X11桌面环境,或者使用终端文件功能
    八股文总结
  • 原文地址:https://blog.csdn.net/GL280599ZL/article/details/127625009