|
| 1 | +package sporadic.IO_example; |
| 2 | + |
| 3 | +import java.io.FileInputStream; |
| 4 | +import java.io.FileNotFoundException; |
| 5 | +import java.io.FileOutputStream; |
| 6 | +import java.io.FileReader; |
| 7 | +import java.io.FileWriter; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStreamReader; |
| 10 | + |
| 11 | +public class JavaIOExample { |
| 12 | + |
| 13 | + private static final String INPUT = "/Users/SteveSun/Downloads/bash.sh"; |
| 14 | + // "/tmp/input.txt"; |
| 15 | + |
| 16 | + private static final String OUTPUT = "/tmp/output.txt"; |
| 17 | + |
| 18 | + public static void main(String[] args) throws IOException { |
| 19 | + |
| 20 | + byteStreamsIO(); |
| 21 | + |
| 22 | + // characterStreamsIO(); |
| 23 | + |
| 24 | + // standardStreamsIO(); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Java byte streams are used to perform input and output of 8-bit bytes. |
| 29 | + * Though there are many classes related to byte streams but the most |
| 30 | + * frequently used classes are: FileInputStream and FileOutputStream. |
| 31 | + * Following is an example which makes use of these two classes to copy an |
| 32 | + * input file into an output file: |
| 33 | + * |
| 34 | + * @throws IOException |
| 35 | + */ |
| 36 | + static void byteStreamsIO() throws IOException {// This type is |
| 37 | + // package-level access, |
| 38 | + // without public, protected |
| 39 | + // and private keywords, |
| 40 | + // this is package-level |
| 41 | + // method |
| 42 | + FileInputStream fis = null; |
| 43 | + FileOutputStream fos = null; |
| 44 | + |
| 45 | + try { |
| 46 | + fis = new FileInputStream(INPUT); |
| 47 | + fos = new FileOutputStream(OUTPUT); |
| 48 | + |
| 49 | + int c; |
| 50 | + while ((c = fis.read()) != -1) { |
| 51 | + fos.write(c); |
| 52 | + } |
| 53 | + } catch (FileNotFoundException e) { |
| 54 | + e.printStackTrace(); |
| 55 | + } finally { |
| 56 | + if (fis != null) { |
| 57 | + fis.close(); |
| 58 | + } |
| 59 | + if (fos != null) { |
| 60 | + fos.close(); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Java Byte streams are used to perform input and output of 8-bit bytes, |
| 67 | + * where as Java Character streams are used to perform input and output for |
| 68 | + * 16-bit unicode. Though there are many classes related to character |
| 69 | + * streams but the most frequently used classes are: FileReader and |
| 70 | + * FileWriter Though internally FileReader uses FileInputStream and |
| 71 | + * FileWriter uses FileOutputStream but here major difference is that |
| 72 | + * FileReader reads two bytes at a time and FileWriter writes two bytes at a |
| 73 | + * time. |
| 74 | + * |
| 75 | + * @throws FileNotFoundException |
| 76 | + * @throws IOException |
| 77 | + */ |
| 78 | + static void characterStreamsIO() throws FileNotFoundException, IOException { |
| 79 | + FileReader in = null; |
| 80 | + FileWriter out = null; |
| 81 | + |
| 82 | + try { |
| 83 | + in = new FileReader(INPUT); |
| 84 | + out = new FileWriter(OUTPUT); |
| 85 | + |
| 86 | + int c; |
| 87 | + while ((c = in.read()) != -1) { |
| 88 | + out.write(c); |
| 89 | + } |
| 90 | + } finally { |
| 91 | + if (in != null) { |
| 92 | + in.close(); |
| 93 | + } |
| 94 | + if (out != null) { |
| 95 | + out.close(); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * All the programming languages provide support for standard I/O where |
| 102 | + * user's program can take input from a keyboard and then produce output on |
| 103 | + * the computer screen. If you are aware if C or C++ programming languages, |
| 104 | + * then you must be aware of three standard devices STDIN, STDOUT and |
| 105 | + * STDERR. Similar way Java provides following three standard streams |
| 106 | + * |
| 107 | + * Standard Input: This is used to feed the data to user's program and |
| 108 | + * usually a keyboard is used as standard input stream and represented as |
| 109 | + * System.in. |
| 110 | + * |
| 111 | + * Standard Output: This is used to output the data produced by the user's |
| 112 | + * program and usually a computer screen is used to standard output stream |
| 113 | + * and represented as System.out. |
| 114 | + * |
| 115 | + * Standard Error: This is used to output the error data produced by the |
| 116 | + * user's program and usually a computer screen is used to standard error |
| 117 | + * stream and represented as System.err. |
| 118 | + * |
| 119 | + * Following is a simple program which creates InputStreamReader to read |
| 120 | + * standard input stream until the user types a "q": |
| 121 | + * |
| 122 | + * @throws IOException |
| 123 | + */ |
| 124 | + static void standardStreamsIO() throws IOException { |
| 125 | + InputStreamReader isr = null; |
| 126 | + |
| 127 | + try { |
| 128 | + isr = new InputStreamReader(System.in); |
| 129 | + System.out.println("Enter characters, 'q' to quit."); |
| 130 | + char c; |
| 131 | + do { |
| 132 | + c = (char) isr.read(); |
| 133 | + System.out.print(c); |
| 134 | + } while (c != 'q'); |
| 135 | + } finally { |
| 136 | + if (isr != null) { |
| 137 | + isr.close(); |
| 138 | + } |
| 139 | + System.out.println("\nProgram stopped."); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments