其他IO流

其他IO流

1.读写基本数据类型

任意类型都是可以转换成字节数组写入到文件,所以写入基本数据类型一般都是一些关键性数据

一般通过这个流写入的数据,外界是无法读懂,可以将一些关键性数据持久化到文件中,提高数据保存的安全性

读取文件中的信息一定要按照写入的顺序读取,更加提高了安全性

1.1数据输入流:

DataInputStream(InputStream in)

1.2数据输出流:

DataOutputStream(OutputStream out)

2.序列化流和反序列化流

关键字:transient 修饰的成员数据不被读取,可以提高安全性

2.1序列化 (ObjectInputStream)

将对象从外界还原

2.2反序列化流 (ObjectOutputStream)

将对象写入到外界 (网络 文件 控制台)

2.3异常问题

  1. java.io.NotSerializableException
  • 异常名称: 没有序列化异常

    //格式:实现Serializable插口
    public class Xxx implements Serializable {
    
    }
    
  1. InvalidClassException
  • 异常名称: 无效类异常
//当没有定义序列化id:private static final long serialVersionUID = 1749348794044443524L;

//原代码
public class Student implements Serializable {
    //系统默认给出的序列化id:stream classdesc serialVersionUID = 2512164725794751903
	private String name;
	public Student() {
		super();
	}
}

//对原代码进行任意改动(这该改动为:把私有化删除)
public class Student implements Serializable {
    //系统默认给出的序列化id:local class serialVersionUID = 799345655256203362
	String name;
	public Student() {
		super();
	}
}

//因为没有定义序列化id,所以每次改动id都会发生改变,当需要输出的是会报错,id不匹配
//local class incompatible: 
//stream classdesc serialVersionUID = 2512164725794751903, 
//local class serialVersionUID = 799345655256203362

public class Student implements Serializable {
    local class serialVersionUID = 799345655256203362
	String name;
	public Student() {
		super();
	}
}
//生成序列化id后,对类进行修改,序列化id不会发改变

3.PrintWriter(字符流) PrintStream(字节流)

概述:向文本输出流打印对象的格式化表示形式。此类实现在 PrintStream 中的所有 print 方法。

3.1特点

  • 只能写数据,不能读取数据
  • 可以操作任意类型的数据。
  • 如果启动了自动刷新,能够自动刷新。

3.2PrintStream(字节流)

1.具备写入到文件的功能
PrintStream(File file) PrintStream(String fileName)
2.具备对流进行编码的功能
PrintStream(File file, String csn)
PrintStream(OutputStream out, boolean autoFlush, String encoding)
PrintStream(String fileName, String csn)
3.具备装饰流的功能
PrintStream(OutputStream out)
4.具备自动刷新的功能
PrintStream(OutputStream out, boolean autoFlush)
5.具备字节流本身的功能
6.自动换行 println

3.3PrintWriter(字符流)

1. 具备写入到文件的功能
​ PrintWriter(File file)
​ PrintWriter(String fileName)
2. 具备对流进行编码的功能
​ PrintWriter(File file, String csn)
​ PrintWriter(String fileName, String csn)
3. 具备装饰流的功能
​ PrintWriter(Writer out)
​ PrintWriter(Writer out, boolean autoFlush)
4. 具备自动刷新的功能
​ PrintWriter(OutputStream out, boolean autoFlush)
​ PrintWriter(Writer out, boolean autoFlush)
​ 完成自动刷新的要求
​ a.启动自动刷新
​ b.调用 print println format printf
5. 具备字节流本身的功能
6. 具备转换流的功能
​ PrintWriter(OutputStream out)
​ PrintWriter(OutputStream out, boolean autoFlush)
7. 自动换行 println

写入的方式

  1. print
  2. write
  3. format
  4. append
	private static void copy(String srcFileName, String descFileName) {
		try (BufferedReader br = new BufferedReader(new FileReader(srcFileName));
				PrintWriter pw = new PrintWriter(new FileWriter(descFileName), true)){
			String line = null;
			while ((line = br.readLine()) != null) {
				pw.println(line);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void write() throws Exception {
		// PrintWriter(Writer out, boolean autoFlush) 
		PrintWriter pw = new PrintWriter(new FileWriter("a.txt"), true);
		pw.append("Hello");
		pw.write("|World");
		pw.format("|我叫做%s,我今年%d岁了", "张三", 18);
		pw.print("Student");
		pw.print(new Student("隔壁老王", 18));
		pw.println();
	}

开启自动刷新后,调用format print println printf才会自动刷新

4.System 类中的静态变量:in,out

  • "标准"输入流:static InputStream in

  • "标准"输出流:static PrintStream out

  • 它们各代表了系统标准的输入和输出设备

  • 默认输入设备是键盘,输出设备是显示器(Console)

        public class IODemo04 {
            public static void main(String[] args) throws IOException {
                // 你不需要管这个流的目的地,因为它永远输出到控制台
                // 内存 --> 控制台
                /*PrintStream ps = System.out;
                ps.write("good".getBytes());
                ps.format("大家好我叫做%s", "张三");
                ps.println();
                ps.println("123");*/
                System.out.println("abc");
                System.err.println("我变红了");
            }
        }

4.1输入流

InputStream in = System.in;

  • InputStream只能读取字节,可以转换成BufferedReader读取一行(使用转换流:InputStreamReader

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
4.1.1模拟Scanner
class MyScanner {
	
	private InputStream is;
	
	public MyScanner() {}
	
	public MyScanner(InputStream source) {
		this.is = source;
	}
	
	public String nextLine() {
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		try {
			String line = br.readLine();
			return line;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public double nextDouble() {
		String line = nextLine();
		//				java.lang.NumberFormatException
		try {
			double d = Double.parseDouble(line);
			return d;
		} catch (Exception e) {
			throw new InputMismatchException("哥们您输入的是小数吗? " + line);
		}
		
	}
	
	public int nextInt() {
		String line = nextLine();
		int i = Integer.parseInt(line);
		return i;
	}
}

5.RandomAccessFile

概述:此类的实例支持对随机访问文件的读取和写入。

RandomAccessFile类不属于流,是Object类的子类

包含了InputStream和OutputStream的功能

5.1特点:

​ 1.支持对随机访问文件的读取和写入
​ long getFilePointer()
​ void seek(long pos)
​ 2.读写基本数据类型
​ 3.读写合一,可以通过模式自由切换
​ 4.清除文件的功能
​ 5.还是通往NIO的一个流 FileChannel getChannel() 【了解】

public class IODemo06 {
	public static void main(String[] args) throws Exception {
		// write();
		// read();
		clear();//清除文件的功能
	}
    
    private static void clear() throws Exception {
		RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");
		raf.setLength(0);
	}
    
	private static void read() throws Exception {
		RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");
		byte[] bys = new byte[5];
		raf.seek(4);
		int len = raf.read(bys);
		System.out.println(new String(bys, 0, len));
		System.out.println(raf.getFilePointer());
	}

	private static void write() throws Exception {
		RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");
		raf.writeInt(10);
		raf.write("Hello".getBytes());
		raf.writeChar('a');
		raf.close();
	}
}

6.Properties

Properties 类表示了一个持久的属性集;Properties 可保存在流中或从流中加载;属性列表中每个键及其对应值都是一个字符串

Properties可以当做Map集合类使用

Properties的特殊遍历功能
public Object setProperty(String key,String value)
public String getProperty(String key)
public Set stringPropertyNames()

Properties和IO流结合使用
public void load(Reader reader)
public void store(Writer writer,String comments)

6.1配置文件

1.xxx.properties 【持久化配置】
2.xxx.xml 【持久化配置】
3.xxx.json 【持久化配置】
4.注解 【运行时配置】
防止硬编码 【写死】,满足了开闭原则

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值