IO流-数据流、对象流(序列化与反序列化)、打印流

简介:

数据流:DataOutputStream

 
 
  1. public class TestDataStream 
  2.     public void writeData() 
  3.     { 
  4.         double[] arrays = new double[1000]; 
  5.         Arrays.fill(arrays, Math.PI); 
  6.          
  7.         String fileName = "F:/java/test2.txt"
  8.         FileOutputStream out; 
  9.         DataOutputStream dos = null
  10.         try 
  11.         { 
  12.             out = new FileOutputStream(fileName); 
  13.             dos = new DataOutputStream(out); 
  14.             for (int i = 0; i < arrays.length; i++) 
  15.             { 
  16.                 dos.writeDouble(arrays[i]); 
  17.             } 
  18. //            dos.write(123); 
  19. //            dos.writeBoolean(true); 
  20. //            dos.writeChar('Z'); 
  21. //            dos.writeDouble(Math.PI); 
  22.             dos.flush(); 
  23.         } 
  24.         catch (FileNotFoundException e) 
  25.         { 
  26.             e.printStackTrace(); 
  27.         } 
  28.         catch (IOException e) 
  29.         { 
  30.             e.printStackTrace(); 
  31.         } 
  32.         finally 
  33.         { 
  34.             if (dos != null
  35.             { 
  36.                 try 
  37.                 { 
  38.                     dos.close(); 
  39.                 } 
  40.                 catch (IOException e) 
  41.                 { 
  42.                     e.printStackTrace(); 
  43.                 } 
  44.             } 
  45.         } 
  46.     } 
  47.      
  48.     public static void main(String[] args) 
  49.     { 
  50.         TestDataStream tds = new TestDataStream(); 
  51.         tds.writeData();      
  52.     } 
  53.      

 

数据流:DataInputStream

 
 
  1. public class TestDataInputStream 
  2.     public static void main(String[] args) 
  3.     { 
  4.         String fileName = "F:/java/test2.txt"
  5.         FileInputStream in = null
  6.         DataInputStream dis = null
  7.         try 
  8.         { 
  9.             in = new FileInputStream(fileName); 
  10.             dis = new DataInputStream(in); 
  11.             for (int i = 0; i < 1000; i++) 
  12.             { 
  13.                 System.out.println(dis.readDouble() + "i: " + i); 
  14.             } 
  15. //            System.out.println(dis.read()); 
  16. //            System.out.println(dis.readBoolean()); 
  17. //            System.out.println(dis.readChar()); 
  18. //            System.out.println(dis.readDouble()); 
  19.         } 
  20.         catch (FileNotFoundException e) 
  21.         { 
  22.             e.printStackTrace(); 
  23.         } 
  24.         catch (IOException e) 
  25.         { 
  26.             e.printStackTrace(); 
  27.         } 
  28.         finally 
  29.         { 
  30.             if (dis != null
  31.             { 
  32.                 try 
  33.                 { 
  34.                     dis.close(); 
  35.                 } 
  36.                 catch (IOException e) 
  37.                 { 
  38.                     e.printStackTrace(); 
  39.                 } 
  40.             } 
  41.         } 
  42.     } 

 

对象流:ObjectOutputStream(unserializable)、ObjectInputStream(serializable)

 
 
  1. public class TestSerializable 
  2.     public static void main(String[] args) 
  3.     { 
  4.         TestSerializable ts = new TestSerializable(); 
  5.         ts.serializable(); 
  6.         ts.unserializable(); 
  7.     } 
  8.     public void serializable()//序列化,写入 
  9.     { 
  10.         String filename = "F:/java/stu.txt"
  11.         Student s1 = new Student("haoyouduo",1987); 
  12.          
  13.         FileOutputStream fis =null
  14.         ObjectOutputStream ois = null
  15.         try 
  16.         { 
  17.             fis = new FileOutputStream(filename); 
  18.             ois = new ObjectOutputStream(fis); 
  19.             ois.writeObject(s1); 
  20.             ois.flush(); 
  21.         } 
  22.         catch (FileNotFoundException e) 
  23.         { 
  24.             e.printStackTrace(); 
  25.         } 
  26.         catch (IOException e) 
  27.         { 
  28.             e.printStackTrace(); 
  29.         } 
  30.         finally 
  31.         { 
  32.             if(null != ois) 
  33.             { 
  34.                 try 
  35.                 { 
  36.                     ois.close(); 
  37.                 } 
  38.                 catch (IOException e) 
  39.                 { 
  40.                     e.printStackTrace(); 
  41.                 } 
  42.             } 
  43.         } 
  44.          
  45.     } 
  46.     public void unserializable()//反序列化,读取 
  47.     { 
  48.         String filename = "F:/java/stu.txt"
  49.         FileInputStream fis = null
  50.         ObjectInputStream ois = null
  51.         try 
  52.         { 
  53.             fis = new FileInputStream(filename); 
  54.             ois = new ObjectInputStream(fis); 
  55.             Student s = (Student)ois.readObject(); 
  56.             System.out.println(s); 
  57.              
  58.         } 
  59.         catch (FileNotFoundException e) 
  60.         { 
  61.             e.printStackTrace(); 
  62.         } 
  63.         catch (IOException e) 
  64.         { 
  65.             e.printStackTrace(); 
  66.         } 
  67.         catch (ClassNotFoundException e) 
  68.         { 
  69.             e.printStackTrace(); 
  70.         } 
  71.         finally 
  72.         { 
  73.             if(ois != null
  74.             { 
  75.                 try 
  76.                 { 
  77.                     ois.close(); 
  78.                 } 
  79.                 catch (IOException e) 
  80.                 { 
  81.                     e.printStackTrace(); 
  82.                 } 
  83.             } 
  84.         } 
  85.          
  86.     } 
  87.  
  88. class Student implements Serializable//对象类必须实现可序列化的 
  89.     String name; 
  90.     int age; 
  91.     public Student(String name,int age) 
  92.     { 
  93.         this.name = name; 
  94.         this.age = age; 
  95.     } 
  96.     @Override 
  97.     public String toString() 
  98.     { 
  99.         return "Student [name=" + name + ", age=" + age + "]"
  100.     } 
  101.  
  102.      

 

打印流:PrintStream

 
 
  1. public class Test 
  2.     public static void main(String[] args) 
  3.     { 
  4.         boolean flag = 2 > 1
  5.         if (flag) 
  6.         { 
  7.             System.out.println("sss"); 
  8.         } 
  9.         else 
  10.         { 
  11.             System.out.println("aaa"); 
  12.         } 
  13.          
  14.         System.out.println("使用printStream之前"); 
  15.  
  16.         /** 
  17.          * 上面部分的内容将打印在控制台里 
  18.          * 下面部分的内容不会打印在控制台里,而是文件里 
  19.          */ 
  20.          
  21.         String filename = "f:/java/log.txt"
  22.         FileOutputStream fos; 
  23.         PrintStream ps = null
  24.         try 
  25.         { 
  26.             fos = new FileOutputStream(filename); 
  27.             ps = new PrintStream(fos); 
  28.             System.setOut(ps); 
  29.             System.out.println("这将打印在文件里"); 
  30.             System.out.println("使用printStream之后"); 
  31.         } 
  32.         catch (FileNotFoundException e) 
  33.         { 
  34.             e.printStackTrace(); 
  35.         } 
  36.         finally 
  37.         { 
  38.             if (ps != null
  39.             { 
  40.                 ps.close(); 
  41.             } 
  42.         } 
  43.     } 

 





本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1191874,如需转载请自行联系原作者
目录
相关文章
|
10天前
|
存储 Java
【IO面试题 四】、介绍一下Java的序列化与反序列化
Java的序列化与反序列化允许对象通过实现Serializable接口转换成字节序列并存储或传输,之后可以通过ObjectInputStream和ObjectOutputStream的方法将这些字节序列恢复成对象。
|
28天前
|
存储 开发框架 .NET
解锁SqlSugar新境界:利用Serialize.Linq实现Lambda表达式灵活序列化与反序列化,赋能动态数据查询新高度!
【8月更文挑战第3天】随着软件开发复杂度提升,数据查询的灵活性变得至关重要。SqlSugar作为一款轻量级、高性能的.NET ORM框架,简化了数据库操作。但在需要跨服务共享查询逻辑时,直接传递Lambda表达式不可行。这时,Serialize.Linq库大显身手,能将Linq表达式序列化为字符串,实现在不同服务间传输查询逻辑。结合使用SqlSugar和Serialize.Linq,不仅能够保持代码清晰,还能实现复杂的动态查询逻辑,极大地增强了应用程序的灵活性和可扩展性。
58 2
|
3天前
|
存储 Java
Java编程中的对象序列化与反序列化
【8月更文挑战第28天】在Java世界中,对象序列化与反序列化是数据持久化和网络传输的关键技术。本文将深入浅出地探讨这一过程,带你领略其背后的原理及应用,让你的程序在数据的海洋中自由航行。
|
10天前
|
XML 存储 JSON
【IO面试题 六】、 除了Java自带的序列化之外,你还了解哪些序列化工具?
除了Java自带的序列化,常见的序列化工具还包括JSON(如jackson、gson、fastjson)、Protobuf、Thrift和Avro,各具特点,适用于不同的应用场景和性能需求。
|
10天前
|
JSON Java 数据格式
【IO面试题 七】、 如果不用JSON工具,该如何实现对实体类的序列化?
除了JSON工具,实现实体类序列化可以采用Java原生序列化机制或第三方库如Protobuf、Thrift、Avro等。
|
25天前
|
存储 算法 Python
【Leetcode刷题Python】297. 二叉树的序列化与反序列化
LeetCode第297题"二叉树的序列化与反序列化"的Python语言解决方案,包括序列化二叉树为字符串和反序列化字符串为二叉树的算法实现。
18 5
|
30天前
|
开发框架 缓存 前端开发
基于SqlSugar的开发框架循序渐进介绍(24)-- 使用Serialize.Linq对Lambda表达式进行序列化和反序列化
基于SqlSugar的开发框架循序渐进介绍(24)-- 使用Serialize.Linq对Lambda表达式进行序列化和反序列化
|
15天前
|
JSON 缓存 安全
Python pickle 二进制序列化和反序列化 - 数据持久化
Python pickle 二进制序列化和反序列化 - 数据持久化
29 0
|
2月前
|
存储 安全 Java
day24:Java零基础 - 序列化与反序列化
【7月更文挑战第24天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
30 1
|
2月前
|
存储 Java
JaveSE—IO流详解:对象输入输出流(序列化及反序列化)
JaveSE—IO流详解:对象输入输出流(序列化及反序列化)

热门文章

最新文章

下一篇
云函数