IOストリーム-データストリーム、オブジェクトストリーム(シーケンス化と逆シーケンス化)、印刷ストリーム
データ・ストリーム:DataOutputStream
データ・ストリーム:DataInputStream
オブジェクトフロー:ObjectOutputStream(unserializable)、ObjectInputStream(serializable)
印刷ストリームいんさつりゅう:PrintStream PrintStream
- 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 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();
- }
- }
- }
- }