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

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

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 (garbled characters may appear) * @date 2015-08-14 11:21:11 * @author SGL */Pub
		Lic class Fileinputstreamtest {public static void main (String[]args) {FileInputStream fileinputstream=null;
			try {//create byte input stream, construct parameter is not existing stream, FileInputStream is node stream fileinputstream=new fileinputstream ("test.txt");
			Create a buffer of length 1024 byte[]buffer=new byte[32];
			The user saves the number of bytes actually read int hasread=0; The process of repeating the contents of a byte using a loop//because each Chinese character occupies 2 bytes, if the Read method reads only half of the Chinese characters, this causes the Chinese characters to 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 resources, garbage collection cannot reclaim the resource, so should show off 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 Test.txt file contents as characters and output to console
 * @date 2015-08-14 11:35:25
 * @author SGL *
 * Public
class Filereadertest {public
	static void Main (string[] args) throws IOException {
		FileReader filere Ader=null;
		try {
			//create character input stream, construct parameter is not existing stream, FileReader is node stream
			filereader=new filereader ("test.txt");
			Create a buffer of length 32
			char[] Buffer=new char[32];
			The number of characters used to hold the actual read
			int hasread=0;
			Use a loop to repeat the character process while
			((hasread=filereader.read (buffer)) >0) {
				//Remove bytes in buffer, convert character array to 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) * Reads Test.txt file contents and writes 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 fileinputstream=null;
		FileOutputStream Fileoutputstream=null;
			try {//create byte input stream, construct parameter is not existing stream, FileInputStream is node stream fileinputstream=new fileinputstream ("test.txt");
			To create a byte output stream, the construction parameter is not an existing stream, indicating that FileOutputStream is the node stream fileoutputstream=new fileoutputstream ("NewTest.txt");
			Byte[] Bbuf=new byte[32];
			int hasread=0;
				The loop pulls the data out of the input stream while ((Hasread=fileinputstream.read (BBUF)) >0) {//reads each time, writes the file output stream, reads as much, writes as 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)
 * Write string to 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 {
			//Create output stream, construct parameter is not existing stream, note fileWriter is 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.
			//\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ r \ n is the line break of Windows platform, if it is Linux and other platforms, use \ n as line break
			filewriter.write ("Jin-li shangyin \ r \ n");
			Filewriter.write ("The brocade is unprovoked 50 chord, one string one pillar thinks Chinese year." \ r \ n ");
			Filewriter.write ("Butterfly Dream fan butterfly, hope Emperor hath to the cuckoo." \ r \ n ");
			Filewriter.write ("The Sea Moon Pearl has tears, Lantian day warm jade smoke." \ r \ n ");
			Filewriter.write ("The feeling can be recalled, but at that time has been disconsolate.") \ r \ n ");
		} catch (IOException e) {
			e.printstacktrace ();
		} finally{
			//Close output stream
			if (filewriter!=null) {
				try {
					filewriter.close ();
				} catch (IOException e) {
					e.printstacktrace ();}}}}









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.