Java 笔记 23:序列化,网络,TCP群聊,UDP

序列化

1、java.io.Serializable接口:

  •  类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。
    
  •  可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。
    
  • 如果实现Serializable接口,对象如何序列化,各个属性序列化的顺序是什么,都是默认的,程序员无法指定,也不用关心。
  • 如果属性前面有static和transient修饰,不参与序列化。
  • 2、java.io.Externalizable接口:
  • 若某个要完全控制某一对象及其超类型的流格式和内容,则它要实现 Externalizable 接口的 writeExternal 和 readExternal 方法。
  • 程序员在writeExternal方法中,自定定制哪些属性要序列化,顺序是什么样。
  • 程序员在readExternal方法中,自定定制哪些属性要反序列化,顺序和writeExternal的方法中一致。

打印流:输出流

  • (1)PrintStream:
  •  经典代表:System.out
    
  •  		Sysetm.err
    
  • new PrintStream(文件名)
  • new PrintStream(文件名,编码)
  • new PrintStream(另一个字节输出流)
  • (2)PrintWriter
  •  Web阶段学习时,从服务器端往客户端返回消息时用到response,response.getWriter()可以返回PrintWriter对象。
    
  •  即  Web服务器往客户端(例如:浏览器)返回html网页时,用的是PrintWriter对象的输出方法。
    
	public void test04() throws IOException{
		//所有数据类型写出去,都是按照文本处理
		PrintStream ps = new PrintStream("1.txt");
		ps.println(12);
		ps.println(true);
		ps.println(new Goods("《从入门到放弃》", 99.99, 1000));
		ps.close();
	}

System.in:默认情况下是从键盘输入的数据中扫描

  • Scanner:可以从你指定的文件、流中读取文本数据
public class TestScanner {
	@Test
	public void test05() throws FileNotFoundException{
		Scanner input = new Scanner(new File("d:/1.txt"),"GBK");//InputStream
		
		while(input.hasNextLine()){
			String line = input.nextLine();
			System.out.println(line);
		}
		
		input.close();
	}
	
	@Test
	public void test04() throws FileNotFoundException{
		Scanner input = new Scanner("1.txt");//InputStream
		
		while(input.hasNextLine()){
			String line = input.nextLine();
			System.out.println(line);
		}
		
		input.close();
	}
	
	@Test
	public void test03() throws FileNotFoundException{
		Scanner input = new Scanner(new File("1.txt"));//InputStream
		
		while(input.hasNextLine()){
			String line = input.nextLine();
			System.out.println(line);
		}
		
		input.close();
	}
	
	@Test
	public void test02() throws FileNotFoundException{
		Scanner input = new Scanner(new FileInputStream("1.txt"));//InputStream
		
		while(input.hasNextLine()){
			String line = input.nextLine();
			System.out.println(line);
		}
		
		input.close();
	}
	
	@Test
	public void test01(){
		Scanner input = new Scanner(System.in);
		System.out.print("请输入一个整数:");
		int num = input.nextInt();
		System.out.println("num = " + num);
		input.close();
	}
}

在Java层面是常量对象,但是可以同C等底层语言进行修改

  • System.in:
  • System.out
  • System.err
public class TestSystem {
	@Test
	public void test02() throws FileNotFoundException{
		System.setOut(new PrintStream("1.txt"));
		
		System.out.println("aaaa");
		System.out.println("bbb");
		System.out.println("ccc");
		System.out.println("ddd");
	}
	
	@Test
	public void test01(){
		PrintStream out = System.out;
		System.out.println(out);
	}
}

JDK1.7中新增了一种try…catch处理的方式,

  • 称为try…with…resource,它是为资源关闭专门设计的try…catch的语法
  • try(
  •  需要关闭的资源对象
    
  • ){
  •  可能发生异常的逻辑代码
    
  • }catch(异常类型 e){
  •  异常处理代码
    
  • }catch(异常类型 e){
  •  异常处理代码
    
  • }
  • 凡是在try()中声明的资源对象,都会自动关闭,无论是否发生异常。
public class TestTryWithResource {
	@Test
	public void test03() {
		//从d:/1.txt(GBK)文件中,读取内容,写到项目根目录下1.txt(UTF-8)文件中
		try(
			FileInputStream fis = new FileInputStream("d:/1.txt");
			InputStreamReader isr = new InputStreamReader(fis,"GBK");
			BufferedReader br = new BufferedReader(isr);
			
			FileOutputStream fos = new FileOutputStream("1.txt");
			OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
			BufferedWriter bw = new BufferedWriter(osw);
		){
			String str;
			while((str = br.readLine()) != null){
				bw.write(str);
				bw.newLine();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void test02() {
		//从d:/1.txt(GBK)文件中,读取内容,写到项目根目录下1.txt(UTF-8)文件中
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream("d:/1.txt"),"GBK"));
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("1.txt"),"UTF-8"));
			
			String str;
			while((str = br.readLine()) != null){
				bw.write(str);
				bw.newLine();
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(bw!=null){
					bw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(br!=null){
					br.close();
				}
			} catch (IOException e) {
				e.printS
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值