Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand data flow, object flow, and print stream in a stream

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to understand the data flow, object flow and print flow in the stream. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Data flow: 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 ("before using printStream") / * the contents of the above part will be printed in the console * the contents of the lower part will not be printed in the console, but in the file * / 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 ("this will be printed in the file"); System.out.println ("after using printStream");} catch (FileNotFoundException e) {e.printStackTrace () } finally {if (ps! = null) {ps.close ();} on how to understand the data flow, object flow and print flow in the flow is shared here. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report