溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何理解流中的數(shù)據(jù)流、對象流和打印流

發(fā)布時(shí)間:2021-11-26 14:24:12 來源:億速云 閱讀:140 作者:柒染 欄目:開發(fā)技術(shù)

這篇文章給大家介紹如何理解流中的數(shù)據(jù)流、對象流和打印流,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

數(shù)據(jù)流: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();          }      }

數(shù)據(jù)流: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//對象類必須實(shí)現(xiàn)可序列化的 {     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之前");          /**          * 上面部分的內(nèi)容將打印在控制臺(tái)里          * 下面部分的內(nèi)容不會(huì)打印在控制臺(tái)里,而是文件里          */                  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();             }         }     } }

關(guān)于如何理解流中的數(shù)據(jù)流、對象流和打印流就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

io
AI