【java基础】IO-Part4-序列化/管道/NIO等

1.序列化:

个人的理解就是:把对象转成二进制,实现在磁盘中的存储和网络传输.

2.ObjectOutputStream和ObjectInputStream(序列化和反序列化)用法,这里贴个Demo:

/**
 * 演示序列化和反序列化
 */
public class SeriliazbleDemo {

	public static void main(String[] args) throws Exception {
		File file = new File("file/objFile");
//		writeObj(file);
		readObj(file);
	}
	//反序列化
	private static void readObj(File file) throws Exception {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
		User user = (User) ois.readObject();
		System.out.println(user);
	}
	//序列化
	private static void writeObj(File file) throws Exception {
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
		User user = new User();//User对象请自行创建,实现Serializable接口,建议添加SerialVersionUID,否则如果对对象改动后,UID不同无法序列化.
		user.setAge(10);
		user.setSex("女");
		user.setUsername("凤姐");
		oos.writeObject(user);
		oos.close();
	}
}

3.关于SerialVersionUID 和transient 的一些细节.

4.打印流PrintStream,PrintWriter(用的不多,简单了解即可)

5.标准流(了解即可)

6.扫描器类 Scanner

用的不多,简单看一下就好:

/**
  *扫描器
 */
public class ScannerDemo {

	public static void main(String[] args) throws Exception {
		/**扫描从键盘输入的数据,效果类似于echo*/
		Scanner sc = new Scanner(System.in);
		/**下面均为从指定文件中扫描,效果等价*/
//		Scanner sc = new Scanner(new File("file/writerFile")); 
//		Scanner sc = new Scanner(new FileInputStream(new File("file/writerFile")));
		while(sc.hasNext()) {
			System.out.println(sc.next());
		}
		sc.close();
	}
}

7.Properties加载配置文件类(重点,常用,必须掌握,另外文末追加yml配置文件的IO操作,因为现在yml才是主流)

/**
 * 演示读取配置文件
 */
public class PropDemo {

	public static void main(String[] args) throws Exception {
		Properties prop = new Properties();
		InputStream in = new FileInputStream("resource/db.properties");//路径一般为classpath
		prop.load(in);
		System.out.println(prop);
		String username = prop.getProperty("username");
		String password = prop.getProperty("password");
		System.out.println("username = "+username+" "+"password = "+password);
	}
}

8.数据流DataInputStream-DataOutputStream(数据流可以读写任意类型的数据),DEMO就不贴了,比较简单,参阅JDK的API即可.

9.RandomAccessFile 随机访问文件,可以从文件的任意位置进行读写,不常用,在多线程断点下载时会用到.

10.PipedOutputStream-PipedInputStream(管道输出/输入流) 用的不多,下面简单演示:

package com.xpc.io.piped;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

/**
 * 线程A,创建管道,并将A~Z写入管道中
 */
class AThread extends Thread{
	private PipedOutputStream pos = new PipedOutputStream();
	public PipedOutputStream getPos() {
		return pos;
	}
	@Override
	public void run() {
		try {
		for(int i=65;i<65+26;i++) {
				pos.write(i);
			} 
		pos.close();
		}catch (IOException e) {
			e.printStackTrace();
		}
	}
}
/**
 * 线程B,将A创建的管道联通,读取管道中的数据
 */
class BThread extends Thread{
	PipedInputStream in = null;
	public BThread(AThread a) throws Exception {
		in = new PipedInputStream(a.getPos());
	}
	@Override
	public void run() {
		int len = -1;
		try {
			while((len = in.read()) != -1) {
				System.out.println((char)len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
/**
 * 演示两个线程之间通过管道读写数据
 */
public class PipedDemo {

	public static void main(String[] args) throws Exception {
		AThread A = new AThread();
		BThread B = new BThread(A);
		A.start();
		B.start();
	}
}

11.NIO(new IO,从JDK1.4出现,到1.7又再次增强,名曰NIO2.0,接下来要重点深入去了解和学习,请继续关注本博更新),现在先介绍一个简单又常用的Copy方法,只需要一行代码完成之前超长的Copy操作,看似很屌,其实是语法糖.

/**
 * 演示JDK7的NIO中的COPY操作
 */
public class NioDemo {

	public static void main(String[] args) throws Exception {
		Files.copy(Paths.get("file/writerFile"), new FileOutputStream("file/xx.txt"));
	}

}

12.从yaml文件中读取数据(重点)

步骤:

(1).在springboot的resource文件夹下的application.yml文件中添加需要配置的内容(key和value用空格隔开),比如:

(2).创建对应的Model对象来接收配置内容:

@Component
@ConfigurationProperties(prefix = "Person")//注意,这里的前缀就是在.yml中的Person,加了此注解后会把.yml中以Person打头的字段自动映射过来,注意必须是同名字段才能对应的映射.
public class Person {
//省略了get set 自己补上.
    private String name;
    private String sex;
    private Integer age;
}

(3).在需要的地方@Autowired注入即可

@RunWith(SpringRunner.class)
@SpringBootTest
public class T {
    @Autowired
    private Person person;
    @Test
    public void test(){
        System.out.print(person);//拿到结果:Person{name='老王', sex='男', age=18}
    }
}

猜你喜欢

转载自blog.csdn.net/lovexiaotaozi/article/details/81068072