FileInputStream, FileReader, FileInputStream, FileWriter use summary

Source: Internet
Author: User

This article is based on Linux environment operation, readers need to have a certain knowledge of Linux before reading

The InputStream consists of three methods as follows:

    • int read (): reads a single byte from the input stream, returning the byte data read (byte data can be converted directly to int type)
    • int read (byte[] b): reads up to b.length bytes of data from the input stream and stores it in byte array B, returning the actual number of bytes read
    • int read (byte[] b, int off, int len): reads up to len bytes of data from the input stream, stores it in array B, and in array B, not starting from the beginning of the array, but starting from the off position, returning the actual number of bytes read

FileInputStream inherit InputStream, use FileInputStream to read file contents

Code 1-1

Import Java.io.fileinputstream;import Java.io.ioexception;public class Fileinputstreamtest {public static void main ( String[] args) {if (args = = NULL | | args.length = = 0) {throw new RuntimeException ("Please enter Path");} FileInputStream fis = null;try {byte[] bbuf = new Byte[1024];int Hasread = 0;fis = new FileInputStream (Args[0]); while ((Ha Sread = Fis.read (bbuf)) > 0) {System.out.print (new String (bbuf, 0, Hasread));}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} finally {try {if (FIS! = null) {Fis.close () ;}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

Code 1-1 Run Result:

[Email protected]:/home/software/.io# cat text Hello springhello Hibernatehello mybatis[email protected]:/home/ software/.io# java fileinputstreamtest text Hello Springhello Hibernatehello MyBatis

Reader consists of the following three methods:

    • int read (): reads a single character from the input stream, returning character data from read (character data can be converted directly to int type)
    • int read (char cbuf[]): reads up to cbuf.length characters of data from the input stream and stores it in Cbuf of the character array, returning the actual number of characters read
    • int read (char cbuf[], int off, int len): reads up to Len characters of data from the input stream and stores it in array B; When placed in an array cbuf, it does not start from the beginning of the array, but starts from the off position, returning the actual number of bytes read

FileReader's parent inherits from reader and reads files using FileReader

Code 1-2

Import Java.io.filereader;import Java.io.ioexception;public class Filereadertest {public static void main (string[] args {if (args = = NULL | | args.length = = 0) {throw new RuntimeException ("Please enter Path");} FileReader fr = Null;try {fr = new FileReader (args[0]); char[] Cbuf = new Char[32];int Hasread = 0;while ((Hasread = Fr.rea D (cbuf)) > 0) {System.out.print (new String (cbuf, 0, Hasread));}} catch (IOException e) {//Todo:handle exceptione.printstacktrace ();} finally {try {if (fr! = null) {Fr.close ();}} catch ( IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

Code 1-2 Run Result:

[Email protected]:/home/software/.io# cat text Java Programming idea algorithm compilation principle [email protected]:/home/software/.io# java Filereadertest text Java Programming idea algorithm compiling principle

Code 1-3

Import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;public class fileoutputstreamtest {public static void main (string[] args) {if (args = = NULL | | Args.length < 2) {throw new Runtimeex Ception ("Please enter two Paths");} FileInputStream FIS = null; FileOutputStream fos = null;try {fis = new FileInputStream (args[0]); fos = new FileOutputStream (args[1]); byte[] Bbuf = new Byte[32];int Hasread = 0;while ((Hasread = Fis.read (bbuf)) > 0) {fos.write (bbuf, 0, Hasread);}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} finally {try {if (FIS! = null) {Fis.close () ;} if (fos! = null) {Fos.close ();}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

OutputStream and Writer:

    • void write (int b): Outputs the specified byte/character to the output stream, where C represents a byte, or it can represent a character
    • void Write (Byte B[]/char cbuf[]): Outputs data from a byte array/character array to the specified output stream
    • void Write (Byte B[]/char cbuf[], int off, int len): Starts from the off position in the byte array/character array, and the byte/character length len is output to the output stream

Writer also contains the following two methods:

    • void write (String str): Outputs the characters contained in the STR string to the specified output stream
    • void Write (string str, int off, int len): Starts from the off position in the STR string, and Len-length characters are output to the specified output stream

Use FileInputStream to perform input, fileoutputstream to perform output

Code 1-3 Run Result:

[Email protected]:/home/software/.io# cat input Java Programming Idea algorithm compilation principle [email protected]:/home/software/.io# cat Output [email protected]:/home/software/.io# java fileoutputstreamtest Input Output [email protected]:/home/software/.io# cat Output Java Programming Idea algorithm compiling principle

Using FileWriter to perform output

Code 1-4

Import Java.io.filewriter;import Java.io.ioexception;public class Filewritertest {public static void main (string[] args {if (args = = NULL | | args.length = = 0) {throw new RuntimeException ("Please enter Path");} FileWriter FW = null;try {fw = new FileWriter (args[0]); Fw.write ("Data structure and algorithm \ n"); Fw.write ("Basic Python tutorial \ n"); Fw.write ("C and pointers \ n ");} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} finally {try {if (fw! = null) {Fw.close ()} } catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

Code 1-4 Run Result:

[Email protected]:/home/software/.io# cat book [e-Mail protected]:/home/software/.io# java filewritertest book [email] protected]:/home/software/.io# Cat book data structure and algorithm Python basic tutorial C and pointers

After Java7, many IO resource classes implement the Autocloseable interface, as long as the IO stream declared in the Try statement can be automatically closed, but there are some IO streams like filewriter such as the output stream, if the IO stream does not show off, The data for the output stream buffer cannot be flush to the actual physical node, because the flush () method is executed before the close () method of the IO stream is executed

FileInputStream, FileReader, FileInputStream, FileWriter use summary

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.