One. INPUT and OUTPUT1. Stream represents any data source that has the ability to output data, or any capacity to receive data receiving source. In Java's IO, all streams include two types: 1.1 Byte-oriented stream in byte-oriented Stream, indicating bytes, read or read from Stream in bytes or Write information into the stream. Byte oriented stream comprises the following types: 1) input stream: 1) ByteArrayInputStream: the use of a buffer memory 2) StringBufferInputStream as InputStream: a String object to a InputStream3) FileInputStream: to a file as InputStream Realize the reading operation of the file 4) PipedinputStream: implements the concept of PIPE, mainly in the thread 5) SEQUENCEINPUTSTREAM: Multiple InputStream is merged into an inputStream2) BYTEARRAYOTPUTSTREAM: put information into a buffer in memory District 2) FileOutputStream: PipedputStream: PipedputStream: Implement the concept of PIPE, mainly in the thread 4) SEQUENCEOTPUTSTREAM: Multiple OutStream merge into an outstream1.2 in Unicode Character-oriented Stream in Unicode Character-oriented Stream indicates that the information is read from the stream in units in Unicode characters to the stream. In Unicode character oriented stream comprises the following types: 1) Input Stream1) CharArrayReader: corresponds with ByteArrayInputStream 2) StringReader: corresponds with StringBufferInputStream 3) FileReader: corresponds with FileInputStream 4) PipedReader: corresponds with PipedInputStream 2) Out Stream1) CharArrayWrite : Corresponding to byterrayoutputstream 2) StringWrite: Corresponding to byte-oriented Stream3) FileWrite: with FileoutPutStream Corresponds 4) PiPedWrite: The word corresponding to the PiPedoutputStream corresponds to the word. Section-oriented stream. The two corresponding classes implementation are the same, the word is different during operation. For example, CharaRrayReader: and ByTearRayinputStream uses a buffer in memory as an inputStream, which is different that the former reads a byte information from memory, while the latter reads a character from memory. 1.3 Conversion between two unsampicated streams InputStreamReader and OutputStreamReader: Convert a byte-oriented Stream into a character-oriented Stream. 2. Stream Add Properties 2.1 "Add Property for Stream Add Properties" to operate the IO's API in the Java described above, we can complete any operation we want to complete. But through the subclasses of FilterInputStream and FilterOutstream, we can add properties to Stream. The following examples illustrate the function of this function.
If we want to write data into a file, we can do this: FileoutStream Fs = New FileoutStream ("Test.txt"); then you can call the Write () function to call the west.txt file by generating FS objects. Data. However, if we want to implement "first to write the data to the file first caching into memory, then the API above the data is written in the file", the above API does not have a meeting to meet our needs. But add our needs to FileoutStream through FilterInputStream and FilteroutStream. 2.2 FilterInputStream's various types 2.2.1 for encapsulation of bytes-oriented InputStream1) DataInputStream: Read basic types (int, char, etc.) data from Stream. 2) BufferedInputStream: Using the buffer 3) LINENUMBERINPUTSTREAM: The number of rows within the input stream can then call getLineNumber () and setLinenumber (int) 4) PushbackInputStream: Little use, generally used for compiler development 2.2.2 There is no class corresponding to the DataInputStream in the package with character-oriented InputStream1). Unless the switch is to be used in BufferedReader readLine (), otherwise the DataInputStream2) BufferedReader: correspondence with BufferedInputStream 3) LineNumberReader: corresponds with LineNumberInputStream 4) PushBackReader: 2.3 FilterOutStream corresponding to the various types PushbackInputStream bytes for packaging 2.2.3 Oriented OutputStream1) DataiOutstream: Outputs Basic Types (INT, CHAR, etc.) data into Stream.
2) BufferedOutstream: Using Buffer 3) PrintStream: Generates Format Output 2.2.4 Used to Pack the Character-oriented OutputStream1) BUFFEREDWRITE: and Correspondence 2) PrintWrite: with the corresponding 3. RandomaccessFile1) You can complete the file via the RandomaccessFile object. Read and write operation 2) When an object is generated, indicate the nature of the file to be opened: R, read-only; W, write only; RW Readable Writing 3) You can directly jump to the location specified in the file 4. I / O An example of application iMport java.io. *; Public class testio {public static void main (string [] args) throws ioException {// 1. Read data from a file from a file BufferedReader in = New BufferedReader (New FileReader "F: //nepalon/testio.java"); string s, s2 = new string (); while ((s = in.readline ())! = Null) S2 = s "/ n"; In.close (); // 1b. Enter the input bufferedreader stdin = new buffrereader ("Enter a line:"); System.Out.println ("Enter a line:"); system.out.println (stdin) .readline ()); // 2. Read data from a string object StringReader in2 = new stringReader (S2); int C; while ((c = in2.read ())! = -1) System.out. Println ((char) c); in2.close (); // 3. Remove the format input from the memory Try {DataInputStream in3 = New DataNputStream (New ByTearrayInputStream (s2.getbytes ()); while (true) system.out .println ((char) in3.Readbyte ());} catch (eofexception e) {SYST Em.out.println ("End of Stream");} // 4. Output to Document Try {BufferedReader In4 = New BufferedReader (New StringReader (S2)); PrintWriter Out1 = New PrintWriter (New FileWriter ("F : // Nepalon // Testio.Out ")); int linecount = 1; while ((s = in4.readline ())! = null) Out1.println (LineCount ": s); Out1.close (); in4.close ();} catch (eofexception ex) {system.out.println ("end of stream");} // 5. Data storage and recovery try2 = new dataputstream (New Bufferedputstream (New Bufferedputstream (New Bufferedputstream New FileOutputStream ("f: // nepalon // data.txt"))))))
Out2.writedouble (3.1415926); Out2.writechars / N "); Out2.WriteBytes (" Thas Was Pi: WritebyTe / N); Out2.close (); DataInputStream In5 = New DataInputStream new BufferedInputStream (new FileInputStream ( "F: // nepalon // Data.txt"))); BufferedReader in5br = new BufferedReader (new InputStreamReader (in5)); System.out.println (in5.readDouble ()); System. Out.println (in5br.readline ()); system.out.println (in5br.readline ());} catch (eofexception e) {system.out.println ("end of stream");} // 6. Pass RandomaccessFile Operation File RandomaccessFile RF = New RandomaccessFile ("f: // Nepalon // RTest.dat", "RW"); for (int i = 0; i <10; i ) RF.WRITEDOUBLE (i * 1.414); RF .close (); rf = new randomaccessfile ("f: // Nepalon // RTest.dat", "R"); for (int i = 0; i <10; i ) system.out.println ("Value" i ":" rf.close ()); rf.close (); rf = new randomaccessfile ("f: // nepalon // rtest.dat", "rw"); rf.seek (5 * 8 ); rf.writedouble (47.0001); rf.close (); rf = new randomaccessfile ("f: // Nepalon // RTest.dat", "R"); for (int i = 0; i <10; i ) ) System.out.println (" Value " i ": " rf.close ()); rf.close ();}} About code interpretation (in the area): 1 area, when reading the file, first read the file content first To the cache, when IN.READLINE () is called, the data is read from the cache in characters in characters (hereinafter referred to as "cache byte reading mode"). In the 1b area, since the data is read from the standard IO (keyboard) from the standard IO (keyboard) by cache byte reading, the standard IO (system.in) is first converted to the Character-oriented Stream, and then the BUFFEREDReader package is performed. In the zone, you should read data from a String object in a form of characters, so a StringReader type Stream is created.
In the zone, when the data STRING object S2 reads the data, first put the data in the object, and then read from the buffer; when the Testio.out file is operated, the formatted information is output to In the cache, the information in the cache is output to the file. In the 5th district, when the data.txt file is output, it is first to output the basic type of data output housing cache, and then output the data in the cache to the file; when the file is read, the data in the file first Read to the cache, then read from the cache in the basic type of form. Note in5.Readdouble (). Because write the first WriteDouble (), in order to be displayed correctly. It is also necessary to read in the form of a basic type. The 6 district is operated by the RandomaccessFile class. Reply: --- Must read: Thoroughly understand the Java IO System (Digest) --- Java's essence IO stream! Servers: pdfjn5189 short message Reply Theme: --- must read: Thoroughly understand the Java IO System (Abstract) --- Java's Entivation IO Flow! 2004-4-10 10:06 am
Reply 1 of 19
Reply
---- Important Tip: LinenumberinputStream, StringBufferInputStream has been abolished! Don't use it again! StringBufferInputStream, this class does not profrily convers! StringBufferInputStream, Deprecated. This class incorrectly assuments That Bytes adequately represent character! ---------------- Care IO, just care about your Java future! --------------------------------- So I put this article alone into a post! hope its good for U.S! ----------------
Reply: --- Must read: Thoroughly understand the Java IO System (Digest) --- Java's essence IO stream! Author: PDFJN5189 short message Reply Theme: Reply: --- Must read: Thoroughly understand the Java IO System (Abstract) --- Java's Entivation IO Flow! 2004-4-10 12:09 pm
Reply 2 of 19
Reply
There is no readline () throughout the DataInputStream stream! Replace with: BufferedReader D = New BufferedReader (in)); -------- turn the word throne to a character stream access cache read the character stream, then process it! This method is very advantageous! --------------------------------------- Why use character streams? ------ ------------ The primary advantage of character streams is that they make it easy to write programs that are not dependent upon a specific character encoding, and are therefore easy to internationalize. Java stores strings in Unicode, an international standard character encoding that is capable of representing most of the world's written languages. Typical user-readable text files, however, use encodings that are not necessarily related to Unicode, or even to ASCII, and there are many such encodings. Character streams hide the complexity of dealing with these encodings by providing two classes that serve as bridges between byte streams and character streams. The InputStreamReader class implements a character-input stream that reads bytes from a byte-input stream and converts them to characters according to a specified Encoding. Similarly, The OutputStreamwriter Class Implements a Character-Output Stream That Converts characters into bytes according a specified encoding and writes them to a byte-output stream. A second advantage of character streams is that they are potentially much more efficient than byte streams. The implementations of many of Java '
s original byte streams are oriented around byte-at-a-time read and write operations. The character-stream classes, in contrast, are oriented around buffer-at-a-time read and write operations. This difference, in combination with a More efficient locking scheme, allows the character stream classes to make Up for the added Overhead of encoding conversion in many Cases. Reply: --- Must-read: Thoroughly understand the Java IO System (Abstract) --- The essence of Java IO stream! Author: PDFJN5189 short message Reply Theme: Reply: --- Must read: Thoroughly understand the Java IO System (Abstract) --- Java's Entivation IO Flow! 2004-4-10 3:50 pm
Reply 3 of 19
Reply
---------- Standard Equipment System.IN Read Data ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------------- Read the byte: BufferedInputStream Read characters: BUFFEREDReader InputStreamReader -------------------------------------------- Import Java .io. *; public class systemin {public static void main (string args []) {TRY {// stream conversion! BufferedReader is = new BufferedReader (new InputStreamReader (System.in)) String inputline = null; while (! (Inputline = is.readLine ()) = null) System.out.println (inputline); is.close ();} Catch (IOEXCEPTION E) {System, Out.println ("IOXE:" E);}}} --------------------------- -------------------------------------------------- -------------------- Standard Output System.out is a PrintStream -------------------- -import java.io. *; public class printstandardoutput {public static void main (String "args) {string myanswer =" no, and what's final, "; system.out.println (" Hello World of Java "); System .out.println ("The Answer IS" Myanswer "At this time."); PrintWriter PW = New PrintWriter (System.out); PW.Println ("The Answer IS" Myanswer "at this time." INT i = 42; PW.Println (i '=' "THE ANSWER."); PW.Println ("NOTE:" i '=' "the answer."); PW.Println i "=" "The answer."); PW.Println (i ('=' "The answer.")); PW.Close ();}} ---------- -------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------- ---------------------- To read (output to -) a text file ----------------- ------------ BufferedReader is = New BufferedReader ("xxxx.text"); read bufferedoutputstream byteout =
New BufferedOutputStream (New FileoutPutStream ("xx.dat")); // Write to text! -----------------------------------------------Reply: - - must read: Thoroughly understand the Java IO system (abstract) --- Java's essence IO stream! Author: PDFJN5189 short message Reply Theme: Reply: --- Must read: Thoroughly understand the Java IO System (Abstract) --- Java's Entivation IO Flow! 2004-4-10 3:55 pm
Reply 4 of 19
Reply
import java.io *;. public class OpenFileByName {public static void main (String [] args) throws IOException {BufferedReader is = new BufferedReader (new FileReader ( "myFile.txt")); BufferedOutputStream bytesOut = new BufferedOutputStream (new FileOutputStream ( "Bytes.dat"); // code here to read from is, write to bytesoutBytesout.close ();}}
Reply: --- Must read: Thoroughly understand the Java IO System (Digest) --- Java's essence IO stream! Author: PDFJN5189 short message Reply Theme: Reply: --- Must read: Thoroughly understand the Java IO System (Abstract) --- Java's Entivation IO Flow! 2004-4-10 3:58 pm
Reply 5 of 19
Reply
import java.io *;. public class FileIO {private FileIO () {} public static void copyFile (String inName, String outName) throws FileNotFoundException, IOException {BufferedInputStream is = new BufferedInputStream (new FileInputStream (inName)); BufferedOutputStream os = new BufferedOutputStream (new FileOutputStream (outName)); copyFile (is, os, true);} / ** Copy a file from an opened InputStream to opened OutputStream * / public static void copyFile (InputStream is, OutputStream os, boolean close) throws IOException {INT B; while ((b = is.read ())! = -1) {os.write (b);} is.close (); if (close) os.close ();} public static void CopyFile (Reader IS, Boolean Close) throws ioException {Int b; while ((b = is.read ())! = -1) {os.write (b);} is.close (); if (Close ) os.close ();} public static void copyFile (String inName, PrintWriter pw, boolean close) throws FileNotFoundException, IOException {BufferedReader is = new BufferedReader (new FileReader (inName)); copyFile (is, pw, close);} Public Static String Readline (STRIN g inName) throws FileNotFoundException, IOException {BufferedReader is = new BufferedReader (new FileReader (inName)); String line = null; line = is.readLine (); is.close (); return line;} protected static final int BLKSIZ = 8192; public void copyFileBuffered (String inName, String outName) throwsFileNotFoundException, IOException {InputStream is = new FileInputStream (inName); OutputStream os = new FileOutputStream (outName); int count = 0; byte b [] = new byte [BLKSIZ]; While (count = is.read (b)))! = -1) {os.write (b, 0, count);} is.close (); os.close ();} public static string readertostring (Reader IS ) throws oException {stringbuffer sb = new stringbuffer (); char [] b = new char [blks];
INT N; WHILE ((n = is.read (b))> 0) {sb.append (b, 0, n);} return sb.tostring ();} public static string infutstreamTring (InputStream IS) throws oews oException { return readerToString (new InputStreamReader (is));} public static void stringToFile (String text, String fileName) throws IOException {BufferedWriter os = new BufferedWriter (new FileWriter (fileName)); os.write (text); os.flush () os.close ();} public static bufferedreader OpenFile (String filename) throws ooException {return new bufferedreader (new filereader);}} reply: --- must read: Thoroughly understand the Java IO System (Digest) - - Java's essence IO stream! Author: PDFJN5189 short message Reply Theme: Reply: --- Must read: Thoroughly understand the Java IO System (Abstract) --- Java's Entivation IO Flow! 2004-4-10 4:11 pm
Reply 6 of 19
Reply
Import Java, IO. *; Class FileProcess {Public Static Void Main (String Args []) {INT B; BYTE BUFFER [] = New Byte [1000]; Try {b = system.out.read (buffer); // Access data for writing! FileOutputStream out = new fileootstream ("line.text"); out.write (buffer, 0, b); // Write out! Pay attention to the relationship between the byte buffer file stream] Catch (IOException E) {System.out.println ("Error");}]}
Reply: --- Must read: Thoroughly understand the Java IO System (Digest) --- Java's essence IO stream! Author: PDFJN5189 short message Reply Theme: Reply: --- Must read: Thoroughly understand the Java IO System (Abstract) --- Java's Entivation IO Flow! 4:18 pm 2004-4-10
Reply 7 of 19
Reply
Import Java, IO. *; Public Class Read {Public Static Void Main (String Args [] 0 {INT B; BYTE BUFFER = New Byte [2000]; Try {FileInputStream Readfile = New FileInputStream ("xxxx.java"); b = ReadFile (Buffer, 0,25000); try {string str = NRE STRING (buffer, 0, b, "default"); // Construct String object! System.out.println (STR);} catch (unsuportEncodeIndexception E) {E.PrintStackTrace ();} catch (ooexception e) {system.out.println ("error");}}}
Reply: --- Must read: Thoroughly understand the Java IO System (Digest) --- Java's essence IO stream! Author: PDFJN5189 short message Reply Theme: Reply: --- Must read: Thoroughly understand the Java IO System (Abstract) --- Java's Entivation IO Flow! 2004-4-10 5:17 pm Reposted 8 of 19
Reply
--------------- Use BufferedReader - FileReader to read text files! -------------------------- Always pay attention to byte flow or character stream ----------- -------------------------------------------------- ------------------------------- Import java.io. *; import java.awt. *; import javax.swing . *; import java.awt.event *;. class filewindow extends JFrame implements ActionListener {JTextArea text; BufferedReader in; JButton button; FileReader file; filewindow () {super ( "character stream file"); Container con = getContentPane () Text = New Jtextarea (50, 50); text.setBackground; try {file f = new file ("e: //a.txt"); file = new fileReader (f); in = new BufferedReader (file); / ** BufferedReader (Reader in) constructor, * file self-character read flow FileReader access to the bufferedReader * stream in order to readline () efficiently in the BufferedReader object method readline () efficiently! * /} catch (filenotfoundexception e) {} catch (ooException e) {} button = new jbutton ("Read"); button.addactionListener (this); con.setLayout (New borderLayout ()); setsize (300, 200); Setvisible (TRUE); Con. AD (TEXT, "CENTER"); Con.com (Button, "South"); addwindowlistener (new windowadapter () {public void windowClosing (WindowEvent E) {setvisible (false); system.exit (0);}});} public void actionperformed (ActionEvent E) {string s; if (E.Getsource () == Button) Try {while ((s = in.readline ())! = Null) Text. Append (s '/ n'); // You can also use bufferstring to temporarily save the read character data! } catch (ioException E1) {}} // --------- main () ---------- Public static void main (string args []) {filewindow win = new filewindow Win.PACK ();}}
Reply: --- Must read: Thoroughly understand the Java IO System (Digest) --- Java's essence IO stream! Author: PDFJN5189 short message Reply Theme: Reply: --- Must read: Thoroughly understand the Java IO System (Abstract) --- Java's Entivation IO Flow! 2004-4-10 5:49 pm Reposted 9 of 19
Reply
------------------------------------- Import java.io. *; Public class randomread { final static String FILENAME = "E: //a.txt"; protected String fileName; protected RandomAccessFile seeker; public static void main (String [] argv) throws IOException {RandomRead r = new RandomRead (FILENAME); System.out.println ("OFFSET IS" R.Readoffset ()); System.out.Println ("Message IS /" R.ReadMessage () "/"" :/** constructor: save filename, construct randomaccessfile * / public RandomRead (String fname) throws IOException {fileName = fname; seeker = new RandomAccessFile (fname, "rw");}. / ** Read the Offset field, defined to be at location 0 in the file * / public int readOffset () throws IOException {seeker.seek (0); seeker.writeChars (FILENAME); // move to very beginningreturn seeker.readInt (); // and read the offset} / ** Read the message at the given offset * / public string readMessage () throws oException {seeker.seek (120); // move to wherereturn seeker.readline (); // and read the string}}