<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # JAVA之旅(三十)——打印流PrintWriter,合并流,切割文件并且合并,對象的序列化Serializable,管道流,RandomAccessFile,IO其他類,字符編碼 * * * > 三十篇了,又是一個陽光明媚的周末,一個又一個的周末,周而復始,不斷學習,前方的路你可曾看見?隨我一起走進技術的世界,流連忘返吧! ## 一.打印流PrintWriter > 打印流有PrintWriter和PrintStream,他的特點可以直接操作輸入流還有文件 * 該流提供了打印方法,可以將各種數據類型原樣打印? * file對象 File * 字符串路徑 String * 字節打印流 * 字符打印流 > 我們演示一遍大家就對此有所了解了 ~~~ package com.lgl.hellojava; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class HelloJJAVA { public static void main(String[] args) { try { BufferedReader bufr = new BufferedReader(new InputStreamReader( System.in)); PrintWriter oWriter = new PrintWriter(System.out, true); String line = null; while ((line = bufr.readLine()) != null) { if (line.equals("over")) { break; } oWriter.write(line); } oWriter.close(); bufr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ~~~ > 要想存到文件里,也是可以直接把文件給放進去的 ## 二.合并流 > 我們來看看什么是合并流,在API文檔上說,他可以串聯流 ~~~ package com.lgl.hellojava; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.SequenceInputStream; import java.util.Enumeration; import java.util.Vector; public class HelloJJAVA { public static void main(String[] args) { try { Vector<FileInputStream> v = new Vector<FileInputStream>(); v.add(new FileInputStream("1.txt")); v.add(new FileInputStream("2.txt")); Enumeration<FileInputStream> elements = v.elements(); SequenceInputStream sis = new SequenceInputStream(elements); FileOutputStream fos = new FileOutputStream("3.txt"); byte[] buf = new byte[1024]; int len = 0; while ((len = sis.read(buf)) != -1) { fos.write(buf, 0, len); } fos.close(); sis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ~~~ > 把1.txt和2.txt乃至add更多的內容合并到3.txt文件中,這就是流的合并 ## 三.切割文件 > 合并文件可以,那肯定可以切割了,我們來看下具體是怎么去制作的 ~~~ // 切割文件 public static void splitFile() { try { FileInputStream fis = new FileInputStream("1.jpg"); FileOutputStream fos = null; byte[] buf = new byte[1024 * 1024]; int len = 0; int count = 1; while ((len = fis.read(buf)) != -1) { fos = new FileOutputStream((count++) + ".patch"); fos.write(buf, 0, len); fos.close(); } fis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } ~~~ > 當運行結束之后,我們可以看到 ![這里寫圖片描述](http://img.blog.csdn.net/20160716122858482) > 已經成功切割了 > > 切割完我們可以合并了 ~~~ // 合并文件 public static void merge() { ArrayList<FileInputStream> al = new ArrayList<>(); for (int i = 1; i <= 2; i++) { try { al.add(new FileInputStream(i + ".patch")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Iterator<FileInputStream> iterator = al.iterator(); Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() { @Override public boolean hasMoreElements() { // TODO Auto-generated method stub return iterator.hasNext(); } @Override public FileInputStream nextElement() { // TODO Auto-generated method stub return iterator.next(); } }; try { SequenceInputStream seq = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("2.jpg"); byte[] buf = new byte[1024]; int len = 0; while ((len = seq.read(buf)) != -1) { fos.write(buf, 0, len); } fos.close(); seq.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } ~~~ > 這樣我們就把圖片拷貝出來了,可以看到 ![這里寫圖片描述](http://img.blog.csdn.net/20160716124824377) > 這段代碼是非常幫的,我們一定會用到的,希望能用的上 ## 四.對象的序列化Serializable > 其實就是可以直接操作對象的流,他會實現一個Serializable()接口,我們用代碼來看下他是怎么用的,我們直接寫讀取對象的類了 ~~~ package com.lgl.hellojava; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class HelloJJAVA { public static void main(String[] args) { // writeObj(); readObj(); } private static void readObj() { try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream( "obj.txt")); Person p = (Person) ois.readObject(); System.out.println(p); ois.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void writeObj() { try { ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("obj.txt")); oos.writeObject(new Person("zhangsan", 20)); oos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class Person implements Serializable { String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } } ~~~ > OK,自己去驗證一下 ## 五.管道流 > 輸入輸出可以直接進行連接,通常結合線程使用 ![這里寫圖片描述](http://img.blog.csdn.net/20160716175705938) ~~~ package com.lgl.hellojava; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; public class HelloJJAVA { public static void main(String[] args) { PipedInputStream pi = new PipedInputStream(); PipedOutputStream po = new PipedOutputStream(); try { pi.connect(po); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Read read = new Read(pi); Write write = new Write(po); new Thread(read).start(); new Thread(write).start(); } } class Read implements Runnable { private PipedInputStream in; public Read(PipedInputStream in) { this.in = in; } @Override public void run() { try { byte[] buf = new byte[1024]; int len = in.read(buf); String s = new String(buf, 0, len); System.out.println(s); in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class Write implements Runnable { private PipedOutputStream out; public Write(PipedOutputStream out) { this.out = out; } @Override public void run() { try { out.write("yes".getBytes()); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ~~~ > 現在就可以聯通了 ## 六.RandomAccessFile > 這是一個很特別的家伙,他繼承的是Object ![這里寫圖片描述](http://img.blog.csdn.net/20160716191244049) * 該類不是IO體系中的子類 * 但是他是IO包中的成員,他同時具備讀寫功能 * 內部封裝了一個數組,而且通過指針對數組中的元素進行操作 * 可以通過getFilePointer或者指針位置同時可以通過seek改變指針的位置 > 其實完成讀寫的原理就是內部封裝了字節輸入,輸出流,通過構造函數可以看出該類具備操作文件的能力,而且操作文件還有模式 ~~~ package com.lgl.hellojava; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; public class HelloJJAVA { public static void main(String[] args) { writeFile(); } public static void writeFile() { try { RandomAccessFile raf = new RandomAccessFile("1.txt", "rw"); raf.write("zhangssan".getBytes()); raf.writeInt(99); raf.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ~~~ > 這只是一個寫的過程,我們的特性還沒有體現出來,我們來看下他是怎么讀的 ~~~ private static void ReadFile() { try { RandomAccessFile raf = new RandomAccessFile("1.txt", "r"); // 調整對象指針 raf.seek(8 * 0); byte[] buf = new byte[1024]; raf.read(buf); String s = new String(buf); System.out.println(s); raf.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } ~~~ > 如果操作的文件不存在,就會自動創建,如果存在,直接覆蓋 ## 七.IO其他類 > IO其他包里,還有一些其他的使用 * 操作基本數據類型 * 字節數組 * 字符數組 ### 1.基本數據類型 > 我們先來看下基本數據類型的,我直接讀寫都寫出來 ~~~ public static void readData() { try { DataInputStream dis = new DataInputStream(new FileInputStream( "data.txt")); int num = dis.readInt(); boolean d = dis.readBoolean(); System.out.println(num + "" + d + ""); dis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 基本數據類型 public static void writeDatea() { try { DataOutputStream dos = new DataOutputStream(new FileOutputStream( "data.txt")); dos.writeInt(55); dos.writeBoolean(true); dos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } ~~~ > 讀取基本數據類型 ## 二.字節數組 > 這個偶爾用下,但是我們還是要去學習一下的 * ByteArrayInputStream: 在構造的時候,需要接收數據源,而且數據源是一個字節數組 * ByteArrayOutputStream:在構造的時候,不用定義數據的目的,因為該對象中已經內部封裝了可變程度的字節數組 > 因為這兩個流的對象都操作的數組,并且沒有使用系統資源,所以,不用進行close關閉! ~~~ package com.lgl.hellojava; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; public class HelloJJAVA { public static void main(String[] args) { // 數據源 ByteArrayInputStream bis = new ByteArrayInputStream("123".getBytes()); // 數據目的 ByteArrayOutputStream bos = new ByteArrayOutputStream(); int ch = 0; while ((ch = bis.read()) != -1) { bos.write(ch); } System.out.println(bos.size()); System.out.println(bos.toString()); } } ~~~ > 是不是比較簡單,字符流我就不說了,和字節流是類似的,但是他會衍生出一個新的內容就是字符編碼 ## 八.字符編碼 > 這些亂七八糟的編碼說真的,東西還是挺多的,但是我并不想講那么多,因為本身我也不是很清楚,其次這玩意大致的知道一點就可以了(個人覺得),什么ASCLL,GBK,UTF-8之類的 ~~~ String s = "hello java"; byte [] b1 = s.getBytes(); System.out.println(Arrays.toString(b1)); ~~~ > 他得到的是什么? ![這里寫圖片描述](http://img.blog.csdn.net/20160717160456700) > 現在我換一個編碼 ~~~ byte [] b1 = s.getBytes("GBK"); ~~~ > 我設置成GBK,他的值是一樣的,說明默認的是GBK ## 九.練習 > 寫完這個練習,我們的I/O流就GG了,感慨頗多,寫I/O寫了很多篇,但是仍然暈乎乎的,應該繼續加深印象的,我們來看下需求 * 有五個學生,每個學生有三門課程,從鍵盤輸入以下數據(包括姓名,三門課成績),輸入的格式,如zhangsan,30,60,96計算出總成績,并把學生的信息和計算出的總分數高低順序存入文件student.txt中去; > 好了,開始擼代碼了 ~~~ package com.lgl.hellojava; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.Set; import java.util.TreeSet; public class HelloJJAVA { public static void main(String[] args) { /** * 邏輯: * 1.通過獲取鍵盤錄入一行數據,然后取出數據封裝成學生對象 * 2.需要從高到低排列,需要排序,使用集合TreeSet就可以了 * 3.寫入文件 */ try { Set<Student> stus = StudentInfoTool.getStudents(); StudentInfoTool.writeFile(stus); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } // 比較數據,Comparable接口 class Student implements Comparable<Student> { // 姓名 private String name; // 三科分數 private int n1, n2, n3; // 總分 private int sum; // 構造函數 public Student(String name, int n1, int n2, int n3) { this.name = name; this.n1 = n1; this.n2 = n2; this.n3 = n3; sum = n1 + n2 + n3; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSum() { return sum; } public void setSum(int sum) { this.sum = sum; } @Override public int compareTo(Student o) { int num = new Integer(this.sum).compareTo(new Integer(o.sum)); if (num == 0) { return this.name.compareTo(o.name); } return num; } @Override public int hashCode() { // TODO Auto-generated method stub return name.hashCode() + sum * 78; } @Override public boolean equals(Object obj) { if (!(obj instanceof Student)) { throw new ClassCastException("Type Error"); } Student s = (Student) obj; return this.name.equals(s.name) && this.sum == s.sum; } } // 工具類 class StudentInfoTool { public static Set<Student> getStudents() throws IOException { BufferedReader bufr = new BufferedReader(new InputStreamReader( System.in)); String line = null; // 集合 Set<Student> stus = new TreeSet<>(); while ((line = bufr.readLine()) != null) { if (line.equals("over")) { break; } String[] info = line.split(","); Student student = new Student(info[0], Integer.parseInt(info[1]), Integer.parseInt(info[2]), Integer.parseInt(info[3])); stus.add(student); } bufr.close(); return stus; } // 寫入文件 public static void writeFile(Set<Student> stus) { try { BufferedWriter bufw = new BufferedWriter(new FileWriter( "student.txt")); for (Student stu : stus) { bufw.write(stu.getName()); bufw.write(stu.getSum() + ""); bufw.newLine(); bufw.flush(); } bufw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ~~~ > 輸出的結果,可以看到 ![這里寫圖片描述](http://img.blog.csdn.net/20160717202606787) > 總數,從小到大,如果你想從高到低的話,自己去實現下排序,看看你們學的怎么樣了 > > 好的,我們本篇到這里就結束了,這篇也是憋了挺久的才別出來的,終于把IO寫完了 ## 有興趣的可以加群:555974449 版權聲明:本文為博主原創文章,博客地址:http://blog.csdn.net/qq_26787115,未經博主允許不得轉載。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看