|
| 1 | +packagesporadic.IO_example; |
| 2 | + |
| 3 | +importjava.io.FileInputStream; |
| 4 | +importjava.io.FileNotFoundException; |
| 5 | +importjava.io.FileOutputStream; |
| 6 | +importjava.io.FileReader; |
| 7 | +importjava.io.FileWriter; |
| 8 | +importjava.io.IOException; |
| 9 | +importjava.io.InputStreamReader; |
| 10 | + |
| 11 | +publicclassJavaIOExample { |
| 12 | + |
| 13 | +privatestaticfinalStringINPUT ="/Users/SteveSun/Downloads/bash.sh"; |
| 14 | +// "/tmp/input.txt"; |
| 15 | + |
| 16 | +privatestaticfinalStringOUTPUT ="/tmp/output.txt"; |
| 17 | + |
| 18 | +publicstaticvoidmain(String[]args)throwsIOException { |
| 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 | +staticvoidbyteStreamsIO()throwsIOException {// This type is |
| 37 | +// package-level access, |
| 38 | +// without public, protected |
| 39 | +// and private keywords, |
| 40 | +// this is package-level |
| 41 | +// method |
| 42 | +FileInputStreamfis =null; |
| 43 | +FileOutputStreamfos =null; |
| 44 | + |
| 45 | +try { |
| 46 | +fis =newFileInputStream(INPUT); |
| 47 | +fos =newFileOutputStream(OUTPUT); |
| 48 | + |
| 49 | +intc; |
| 50 | +while ((c =fis.read()) != -1) { |
| 51 | +fos.write(c); |
| 52 | +} |
| 53 | +}catch (FileNotFoundExceptione) { |
| 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 | +staticvoidcharacterStreamsIO()throwsFileNotFoundException,IOException { |
| 79 | +FileReaderin =null; |
| 80 | +FileWriterout =null; |
| 81 | + |
| 82 | +try { |
| 83 | +in =newFileReader(INPUT); |
| 84 | +out =newFileWriter(OUTPUT); |
| 85 | + |
| 86 | +intc; |
| 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 | +staticvoidstandardStreamsIO()throwsIOException { |
| 125 | +InputStreamReaderisr =null; |
| 126 | + |
| 127 | +try { |
| 128 | +isr =newInputStreamReader(System.in); |
| 129 | +System.out.println("Enter characters, 'q' to quit."); |
| 130 | +charc; |
| 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 | +} |