Java IO file read and write example (Outputstream,inputstream,writer,reader)

Source: Internet
Author: User
Tags array length

One, file creates files

File File = new file ("D:" + file.separator + "Yi.txt");

code example:

 PackageCom.hbut.io;ImportJava.io.File;Importjava.io.IOException; Public classFiledemo { Public Static voidMain (string[] args) {File file=NewFile ("D:" + file.separator + "Yi.txt"); if(File.exists ()) {file.delete (); System.out.println ("File already exists"); } Else {               Try {                  Booleanstates=false; States=File.createnewfile (); if(states==true) {System.out.println ("File creation succeeded"); }            } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }           }       }   }  
View Code

Two, OutputStream (byte stream) write file

out= new FileOutputStream (file);//Get the actual byte stream output object, content overlay

String info= "Hello";//What to enter

Byte[] B=info.getbytes ();//convert character to byte array

Out.write (b);

code example

 PackageCom.hbut.io;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.OutputStream; Public classOutputdemo { Public Static voidMain (string[] args) {File file=NewFile ("D:" + file.separator + "Yi.txt");//specify the file to write toOutputStream out=NULL;//define a byte stream output object        Try{ out=NewFileOutputStream (file);//gets the actual byte stream output object, covering the contents of the}Catch(FileNotFoundException e) {e.printstacktrace (); } String Info= "Hello";//What to enter        byte[] b=info.getbytes ();//Convert a character to a byte array        Try{out.write (b); } Catch(IOException e) {e.printstacktrace (); }           Try{out.close (); } Catch(IOException e) {e.printstacktrace (); }       }   }  
View Code

Third, InputStream (byte stream) read out the file

InputStream in = null;//defines a byte stream input object

in = new FileInputStream (file);//Get the actual byte stream input object

Byte[] B = new byte[1024];//open space, read content

Len = In.read (b);//Read

System.out.println (New String (b, 0, Len));//Output Read content

code example

 PackageCom.hbut.io;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.io.InputStream; Public classInputstreamdemo { Public Static voidMain (string[] args) {File file=NewFile ("D:" + file.separator + "Yi.txt");//Specify the files to readInputStream in =NULL;//defining a byte stream input object        Try {            //out= New FileOutputStream (file,true);//whether the byte append functionin =NewFileInputStream (file);//get the actual byte stream input object}Catch(FileNotFoundException e) {e.printstacktrace (); }        intlen = 0;//Input Array Length        byte[] B =New byte[1024];//open space, read content//byte[] b=new byte[(int) file.length ()];//open up space based on file size        Try{len= In.read (b);//Read}Catch(IOException E1) {e1.printstacktrace (); }        Try{in.close (); } Catch(IOException e) {e.printstacktrace (); } System.out.println (NewString (b, 0, Len)); }}
View Code

Four, writer (character stream) write file

Writer Write = null;//defines the character output stream

Write = new FileWriter (file);

String infor = "Hello,writerdemo";//information that will be written to the file

Write.write (infor);//write file

code example

 PackageCom.hbut.io;ImportJava.io.File;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.Writer; Public classWriterdemo { Public Static voidMain (string[] args) {File file=NewFile ("D:" + file.separator + "WriterDemo.txt");//specify the file to write toWriter Write =NULL;//defining the character output stream        Try{Write=NewFileWriter (file); } Catch(IOException e) {e.printstacktrace (); } String infor= "Hello,writerdemo"; Try{write.write (infor); } Catch(IOException e) {e.printstacktrace (); }        Try{write.close (); } Catch(IOException e) {e.printstacktrace (); }    }}
View Code

Five, reader (character stream) read out the contents of the file

Reader Read = null;//defines the character input stream

Read = new FileReader (file);

Char[] B=new char[1024];//Set the length of the character

int Len=read.read (b);//file contents read into b[]

System.out.println (New String (b));

code example

 PackageCom.hbut.io;ImportJava.io.File;ImportJava.io.FileReader;Importjava.io.IOException;ImportJava.io.Reader; Public classReaderdemo { Public Static voidMain (string[] args) {File file=NewFile ("D:" + file.separator + "Yi.txt");//specify the files to be read outReader Read =NULL;//defining character input streams        Try{Read=Newfilereader (file); } Catch(IOException e) {e.printstacktrace (); }           Char[] b=New Char[1024];//set the length of a character        Try {               intlen=Read.read (b); System.out.println (NewString (b)); } Catch(IOException e) {e.printstacktrace (); }           Try{read.close (); } Catch(IOException e) {e.printstacktrace (); }       }   }  
View Code

Java IO file read and write example (Outputstream,inputstream,writer,reader)

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.