1. Input from memory
Package Penalty for com.zachary.io; Import java.io.IOException; Import java.io.StringReader; / ** * Input from memory * @author Zachary.Zheng * @version 1.0 * @date 2019 Nian 11 Yue 10 Ri * / public class MemoryInput { / ** * BufferedInputFile.read () read results are used to create a String the StringReader * @param args * @throws IOException * / public static void main (String [] args) throws IOException { the StringReader in =new new the StringReader (BufferedInputFile.read ( "the src / COM / Zachary / IO / MemoryInput.java" )); int C; the while (! (C = in.read ()) = -1 ) { // Read () is int returns the next byte, it must be transformed into char to print properly. System.out.println (( char ) C); } } }
2. formatted memory input, use caution
Package com.zachary.io; Import java.io.ByteArrayInputStream; Import java.io.DataInputStream; Import java.io.IOException; / ** * formatted input memory * used with caution * @author Zachary.Zheng * @version 1.0 * @date 2019 dated 10 years. 11 Day * / public class FormattedMemoryInput { / ** * formatted data to be read, DataInputStream may be used, which is a byte-oriented I / O class (not character-oriented - Chinese distortion) * 1. encoded as 'GBK' and 'UTF-8' have no effect * @param args * @throws IOException * / public static void main (String [] args) throws IOException { DataInputStream in = new new DataInputStream ( new new A ByteArrayInputStream ( BufferedInputFile.read ( "the src / COM / Zachary / IO / FormattedMemoryInput.java") the getBytes ( "GBK." ) ) ); the try { the while ( to true ) { System.out.println (( char ) in.readByte ()); } } the catch (exception E) { // can catch the end of the input abnormality detecting abnormal but it is wrongly used. System.out.println ( "End of Stream" ); } in = new new DataInputStream ( new new A ByteArrayInputStream ( BufferedInputFile.read ( . "The src / COM / Zachary / IO / FormattedMemoryInput.java") the getBytes ( "UTF-. 8" ) ) ); / * method of accessible see how many characters * available () "in a case where the number of bytes that can be read without blocking" literally * 1 * 2 mode of operation based on the read media types differ depending * 3. file, which means that the entire file * 4. but for different types of flow may not be the case, so be careful to use * / the while (in.available ()! = 0 ) { System.out.println ((char)in.readByte()); } } }