IO Stream Learning Note input/output of 3--byte stream and character stream

Source: Internet
Author: User
Tags array to string finally block

The operation of the byte stream and the character stream is basically the same, the difference is only the data unit of the operation is different: the data unit of the byte stream operation is the data unit character of bytes, character stream manipulation.

InputStream and reader are the base classes for all input streams, all of which are two abstract classes and cannot create instances by themselves, but each have an input stream for reading the file: FileInputStream and FileReader, both of which are node streams.

OutputStream and writer are the base classes for all output streams, similar to input streams, and have corresponding output streams: FileOutputStream and FileWriter, which are also node streams.


1, the use of FileInputStream read file examples are as follows:

Package Com.songguoliang.io;import java.io.fileinputstream;import java.io.ioexception;/** * FileInputStream (node stream) * Reads the contents of the Test.txt file in bytes and outputs it to the console (may appear garbled in Chinese) * @date 2015-08-14 11:21:11 * @author SGL */public class Fileinputstreamtest {Pub Lic static void Main (String[]args) {FileInputStream Fileinputstream=null;try {///create byte input stream, construction parameter is not an existing stream, Description FileInputStream is a node stream fileinputstream=new fileinputstream ("test.txt");//Create a buffer of length 1024 byte[]buffer=new byte[32] ;//The user saves the actual number of bytes read int hasread=0;//The process of repeating the byte contents using a loop//Because each Chinese text occupies 2 bytes, if the Read method reads only half of the Chinese characters, this causes the Chinese characters to be garbled while (hasread= Fileinputstream.read (buffer)) >0) {//Remove buffer in bytes, convert byte array to string output to console System.out.println (new string (Buffer,0,hasread) );}} catch (IOException e) {e.printstacktrace ();} finally{try {///close file input stream//program Open File IO resource, not in-memory resource, garbage collection cannot reclaim the resource, So should be shown close file IO resource fileinputstream.close ();} catch (IOException e) {e.printstacktrace ();}}}}

2, use FileReader to read the file example:

Package Com.songguoliang.io;import java.io.filereader;import java.io.ioexception;/** * FileReader (node stream) * Read the contents of the Test.txt file as a character and output it to the console * @date 2015-08-14 11:35:25 * @author SGL */public class Filereadertest {public static void M Ain (string[] args) throws IOException {FileReader Filereader=null;try {//create character input stream, construct parameter is not an existing stream, Description FileReader is a node stream filereader=new filereader ("test.txt");//Create a buffer of length 32 char[] Buffer=new char[32];//to hold the number of characters actually read int hasread=0;//uses loops to repeat the character process while ((Hasread=filereader.read (buffer)) >0) {//Take out buffer bytes, Converts a character array to a string output to the console System.out.println (new string (Buffer,0,hasread));}} catch (IOException e) {e.printstacktrace ();} finally{//use the finally block to close the file input stream if (filereader!=null) {filereader.close ();}}}}



3, use FileOutputStream output content as follows:

Package Com.songguoliang.io;import Java.io.fileinputstream;import Java.io.fileoutputstream;import java.io.ioexception;/** * FileOutputStream (Node stream) * Read Test.txt file contents and write to newTest.txt file, i.e. copy test.txt file * @date 2015-08-14 11:44:34 * @author SGL */public class Fileoutputstreamtest {public static void main (string[] args) {FileInputStream fileinp Utstream=null; FileOutputStream Fileoutputstream=null;try {//create byte input stream, construction parameter is not an existing stream, the FileInputStream is a node stream fileinputstream=new FileInputStream ("test.txt");//create Byte output stream, construct parameter is not already existing stream, FileOutputStream is node stream fileoutputstream=new fileoutputstream (" NewTest.txt "); byte[] bbuf=new byte[32];int hasread=0;//Loop pulls data from the input stream while (Hasread=fileinputstream.read (BBUF)) >0 {//Read every time, that is, write to the file output stream, read how much, write how much. Fileoutputstream.write (bbuf, 0, Hasread);}} catch (IOException e) {e.printstacktrace ();} finally{//use the finally block to close the file input stream if (fileinputstream!=null) {try { Fileinputstream.close ();} catch (IOException e) {e.printstacktrace ();}} Use the finally block to close the file output stream if (fileoutputstream!=null) {try {Fileoutputstream.closE ();} catch (IOException e) {e.printstacktrace ();}}}}


4, the use of FileWriter output content examples are as follows:

Package Com.songguoliang.io;import java.io.filewriter;import java.io.ioexception;/** * FileWriter (node stream) *  Writes a string to the Text.txt file * @date 2015-08-14 11:50:42 * @author SGL */public class Filewritertest {public static void main (string[] args) {FileWriter Filewriter=null;try {//creates an output stream, the construction parameter is not an existing stream, and the FileWriter is a node stream filewriter=new FileWriter ("Text.txt"); /Because character streams are directly manipulated as characters, writer can use strings instead of character arrays, that is, using string objects as arguments. The last side \ r \ n is the Windows platform line break, if it is Linux and other platforms, as long as the use of \ n can be used as a newline character filewriter.write ("Jin-li shangyin \ r \ n"); Filewriter.write ("The 50 strings, One string, one pillar, the year of Si Hua. \ r \ n "); Filewriter.write (" Butterfly Dream fan Butterfly, Wang hath-tai cuckoo. " \ r \ n "); Filewriter.write (" The Pearl of the Sea Moon has tears, Lantian day warm jade smoke. " \ r \ n "); Filewriter.write (" The feeling can be recalled, but it was already disconsolate. ") \ r \ n ");} catch (IOException e) {e.printstacktrace ();} finally{//Close output stream if (filewriter!=null) {try {filewriter.close ();} catch ( IOException e) {e.printstacktrace ();}}}}








Copyright NOTICE: This article is the original blogger article, reproduced please indicate this article link. The contents of the article can be corrected if there is any mistake, so as not to mislead more people.

IO Stream Learning Note input/output of 3--byte stream and character stream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.