Java file byte stream and character stream Fileinputstream,fileoutputstream,filereader,filewriter

Source: Internet
Author: User



One, file input stream and file output stream: Fileinputstream,fileoutputstream
FileInputStream can use the Read () method to read one byte at a time and return it as an int, or to read into a byte array when using the Read () method, the number of elements of a byte array, and how many bytes are enrolled in. In the process of completing or writing the entire file, such a byte array is usually used as a buffer because a byte array typically plays the intermediate role of the data.

intread();intread(byte b[]);intread(byte b[],int off,int len);

How to use: a data stream that takes a file as a source of data input. Or the class that opens the file, reads the data from the file to the memory.

//构造函数FileInputStream(String name);FileInputStream(Filefile);//使用方法(1)File f=newFile("d:/a.txt");   in=new FileInputStream( f);//使用方法(2in=new FileInputStream("d:/a.txt");

Examples of programs:

Import java.io.IOException; import Java.io.FileInputStream;; Public classtestfile { Public Static void Main(String args[]) throws IOException {Try{FileInputStream rf=NewFileInputStream ("D:/a.txt");intn= +;bytebuffer[]=New  byte[n]; while(Rf.read (Buffer,0, N)!=-1) && (n>0)){//Read the contents of the file into this cache array, the cache array is actually memorySystem. out. println (NewString (buffer)); } System. out. println ();         Rf.close (); }Catch(IOException IOe) {System. out. println (Ioe.tostring ()); }     }  }

The FileOutputStream class is used to process data streams with files as data output, or to read data into files from the memory area, and the FileOutputStream class is used to process data streams as data output purposes; A string representing the file name

//method   void  Write (byte  b[]); void  write (byte  b[], int  off,int  len);  
//构造函数FileOutputStream(String name);FileOutputStream(Filefile);//使用方法(1)File f=newFile("d:/a.txt");   in=new FileOutputStream( f);//使用方法(2in=new FileOutputStream("d:/a.txt");

Examples of programs:
Import java.io.*;

Import java.io.*; Public classTest { Public Static void Main(String args[]) {byte[] b="Do you want to be in Wuhan?". GetBytes ();Try{FileOutputStream out=NewFileOutputStream ("Hello.txt"); out. write (b); out. Write (b,0, b.length);//write the string into the file}Catch(IOException e) {System. out. println ("Error"+E); }  }}

Example: Copying a file to another file

ImportJava.io.File;ImportJava.io.IOException;ImportJava.io.FileOutputStream;ImportJava.io.FileInputStream; Public  class test{      Public Static void Main(String args[])throwsIOException {Try{File FromFile =NewFile ("A.txt"); File ToFile =NewFile ("B.txt"); FileInputStream FormS =NewFileInputStream (FromFile);//File input streamFileOutputStream ToS =NewFileOutputStream (ToFile);//File output stream            intC while((c = Forms.read ())! =-1) {Tos.write (c);//read from A to write to B} fins.close ();         Fouts.close (); }Catch(IOException e) {System.err.println ("Filestreamstest:"+ e); }     }  }

Two, file character stream: Filereader,filewriter
The read write method of the file byte stream uses the bytes read-write operation, that is, the data is processed in bytes as the basic unit, but the byte stream does not work well with Unicode characters, such as a Chinese character occupying two bytes.
Corresponding to the Fileinputstream,fileoutputstream is the FileReader and filewriter character streams, which are the subclasses of reader and writer respectively.

//FileReaderFileReader(String filename);FileReader(File file);//方法int read();int read(byte b[]);int read(byte b[],int off,int len);//FileWriterFileWriter(String filename);FileWriter(File file);//方法void write(byte b[]);void write(byte b[],int off,int len);

Examples of programs:

Import java.io.*; Public classTest { Public Static void Main(String args[]) {Chara[]="This is the file character stream". ToCharArray ();intn=0, m=0;Try{File f=NewFile ("Secret.txt"); for(intI=0; i<a.length;i++) {a[i]= (Char) (a[i]^' R ');//Array A is encrypted} FileWriter out=NewFileWriter (f); out. Write (A,0, a.length);//write encrypted array A to file F             out. Close (); FileReaderinch=NewFileReader (f);Chartom[]=New Char[Ten]; System. out. println ("Ciphertext:"); while((n=inch. Read (Tom,0,Ten))!=-1) {//read out the encrypted a-to-array Tom from the file F and then outputString s=NewString (Tom,0, n); System. out. print (s); }inch. Close (); System. out. printf ("\ nthe decryption: \ n");inch=NewFileReader (f); while((n=inch. Read (Tom,0,Ten))!=-1) { for(intI=0; i<n;i++) {tom[i]= (Char) (tom[i]^' R ');//Log Group Tom decryption} String s=NewString (Tom,0, n); System. out. print (s);//Output}inch. Close (); }Catch(IOException e) {System. out. println (E.tostring ()); }   }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java file byte stream and character stream Fileinputstream,fileoutputstream,filereader,filewriter

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.