입출력 흐름 - 데이터 흐름, 객체 흐름(시리얼화 및 역시리얼화), 인쇄 흐름
- 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
- {
- 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();
- }
- }
- }
- }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java 정렬화 및 반정렬화 인스턴스 상세 정보Java 정렬화 및 반정렬화 인스턴스 상세 정보 분포식 응용에서 대상은 서열화를 거쳐야만 각 분포식 구성 요소 사이에서 전송할 수 있다. 이것은 두 가지 측면의 기술인 송신자가 대상을 서열화하고 수용자가 대상을 반서...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.