This shows you the differences between two versions of the page.
|
java:byte_streams [2015/02/21 22:40] elkarafe |
java:byte_streams [2016/02/26 11:15] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | Τα προγράμματα χρησιμοποιούν **byte streams** για να εκτελέσουν είσοδο και έξοδο bytes των 8-bit. Όλες οι κατηγορίες byte stream είναι απόγονοι των **InputStream** και **OutputStream**. | ||
| - | |||
| - | Υπάρχουν πολλές byte stream κλάσεις. Για να δείξουμε πώς δουλεύουν τα byte streams , θα επικεντρωθούμε στα **I / O byte streams**, **FileInputStream** και **FileOutputStream**. Άλλα είδη byte streams χρησιμοποιούνται με τον ίδιο τρόπο και διαφέρουν κυρίως στον τρόπο που κατασκευάζονται. | ||
| - | |||
| - | ==== Χρησιμοποιώντας Byte Streams ==== | ||
| - | |||
| - | Θα εξερευνήσουμε τα FileInputStream και FileOutputStream εξετάζοντας ένα παράδειγμα προγράμματος που ονομάζεται [[CopyBytes]], το οποίο χρησιμοποιεί byte streams για να αντιγράψει το [[xanadu.txt]], ένα byte τη φορά. | ||
| - | |||
| - | <code java> | ||
| - | import java.io.FileInputStream; | ||
| - | import java.io.FileOutputStream; | ||
| - | import java.io.IOException; | ||
| - | |||
| - | public class CopyBytes { | ||
| - | public static void main(String[] args) throws IOException { | ||
| - | |||
| - | FileInputStream in = null; | ||
| - | FileOutputStream out = null; | ||
| - | |||
| - | try { | ||
| - | in = new FileInputStream("xanadu.txt"); | ||
| - | out = new FileOutputStream("outagain.txt"); | ||
| - | int c; | ||
| - | |||
| - | while ((c = in.read()) != -1) { | ||
| - | out.write(c); | ||
| - | } | ||
| - | } finally { | ||
| - | if (in != null) { | ||
| - | in.close(); | ||
| - | } | ||
| - | if (out != null) { | ||
| - | out.close(); | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | </code> | ||
| - | |||