如何理解流中的数据流、对象流和打印流
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章给大家介绍如何理解流中的数据流、对象流和打印流,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。数据流:DataOutputStreampublic class TestDa
千家信息网最后更新 2026年11月03日如何理解流中的数据流、对象流和打印流
这篇文章给大家介绍如何理解流中的数据流、对象流和打印流,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
数据流:DataOutputStream
public class TestDataStream { public void writeData() { double[] arrays = new double[1000]; Arrays.fill(arrays, Math.PI); String fileName = "F:/java/test2.txt"; FileOutputStream out; DataOutputStream dos = null; try { out = new FileOutputStream(fileName); dos = new DataOutputStream(out); for (int i = 0; i < arrays.length; i++) { dos.writeDouble(arrays[i]); } // dos.write(123); // dos.writeBoolean(true); // dos.writeChar('Z'); // dos.writeDouble(Math.PI); dos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (dos != null) { try { dos.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { TestDataStream tds = new TestDataStream(); tds.writeData(); } }数据流:DataInputStream
public class TestDataInputStream { public static void main(String[] args) { String fileName = "F:/java/test2.txt"; FileInputStream in = null; DataInputStream dis = null; try { in = new FileInputStream(fileName); dis = new DataInputStream(in); for (int i = 0; i < 1000; i++) { System.out.println(dis.readDouble() + "i: " + i); } // System.out.println(dis.read()); // System.out.println(dis.readBoolean()); // System.out.println(dis.readChar()); // System.out.println(dis.readDouble()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (dis != null) { try { dis.close(); } catch (IOException e) { e.printStackTrace(); } } } } }对象流:ObjectOutputStream(unserializable)、ObjectInputStream(serializable)
public class TestSerializable { public static void main(String[] args) { TestSerializable ts = new TestSerializable(); ts.serializable(); ts.unserializable(); } public void serializable()//序列化,写入 { String filename = "F:/java/stu.txt"; Student s1 = new Student("haoyouduo",1987); FileOutputStream fis =null; ObjectOutputStream ois = null; try { fis = new FileOutputStream(filename); ois = new ObjectOutputStream(fis); ois.writeObject(s1); ois.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(null != ois) { try { ois.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void unserializable()//反序列化,读取 { String filename = "F:/java/stu.txt"; FileInputStream fis = null; ObjectInputStream ois = null; try { fis = new FileInputStream(filename); ois = new ObjectInputStream(fis); Student s = (Student)ois.readObject(); System.out.println(s); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if(ois != null) { try { ois.close(); } catch (IOException e) { e.printStackTrace(); } } } } } class Student implements Serializable//对象类必须实现可序列化的 { String name; int age; public Student(String name,int age) { this.name = name; this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } }打印流:PrintStream
public class Test { public static void main(String[] args) { boolean flag = 2 > 1; if (flag) { System.out.println("sss"); } else { System.out.println("aaa"); } System.out.println("使用printStream之前"); /** * 上面部分的内容将打印在控制台里 * 下面部分的内容不会打印在控制台里,而是文件里 */ String filename = "f:/java/log.txt"; FileOutputStream fos; PrintStream ps = null; try { fos = new FileOutputStream(filename); ps = new PrintStream(fos); System.setOut(ps); System.out.println("这将打印在文件里"); System.out.println("使用printStream之后"); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (ps != null) { ps.close(); } } } }关于如何理解流中的数据流、对象流和打印流就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
对象
数据
数据流
内容
序列
控制台
文件
更多
部分
帮助
控制
不错
兴趣
小伙
小伙伴
文章
知识
篇文章
而是
参考
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技会
互联网核心科技是什么
互联网巨头反垄断金融科技
齐家互联网科技
睿民互联网科技
互联网 新科技
互联网科技素材视频竖屏
哈尔滨新华互联网科技
互联网科技博主是什么意思
恒银科技与互联网金融
成都互联网科技公司100强
互联网5g黑科技有哪些
成都互联网科技公司工作好不好
互联网金融科技名词解释
小米互联网科技园入驻
智帮互联网科技公司最近动向
35岁以上互联网科技行业
互联网高科技行业有哪些企业
广州三三互联网科技有限公司
重庆科技产业互联网有哪些
耶加雪菲
南京欣网互联网络科技有限公司
A股互联网金融科技公司
科技互联网公司装修
朗新科技与工业互联网
金融科技 互联网金融
九亚互联网科技
互联网科技有限公司 翻译
乌鲁木齐新华智原互联网科技学校
同城互联网络科技