33.JAVAI/O Operation Two

Source: Internet
Author: User

    1. How to read and write large files

    2. How to use character streams: when reading and writing files, based on characters

      BYTE input stream: Reader<--filereader int read (char[] c,int off,int len)

BYTE output stream: Writer<--filewriter void Write (char[] c,int off,int len)

//large file read/write method//First step   import class    import java.io.*;class  test{public static void main (string args[])  {fileinputstream fis =  null; //declares an input stream reference fileoutputstream fos = null;try{//generates an object that represents the input stream fis = new  FileInputStream ("E:/src/from.txt")  ;//generates an output stream object Fos = new fileoutputstream ("E:/src/to.txt"); /  generates a byte array byte[] buffer = new byte[1024];while (TRUE) { //loops through the contents of a large file// Call the Read method of the input stream object   Read Data int temp = fis.read (buffer,0,buffer.length); if (temp == -1) {   //read end of file   jump out of loop break;} Fos.write (buffer,0,temp);}} catch (exception e) {System.out.println (e);} finally{    try{ //captures the exception fis.close () that could be generated by closing the IO stream;  //close file Fos.close (); catch (exception e) {System.out.println (e);}}}} 
Import java.io.*; Import IO stream character stream operation public class Testchar{public static void Main (String args[]) {FileReader FR = null; FileWriter FW = NULL;TRY{FR = new FileReader ("E:/src/from.txt"), FW = new FileWriter ("E:/src/to.txt"); char buffer[] = new C Har[100];int temp = Fr.read (buffer,0,buffer.length); Fw.write (buffer,0,temp);} catch (Exception e) {System.out.println (e);} Finally{try{fr.close (); Fw.close ();} catch (Exception e) {System.out.println (e);}}}}


33.JAVAI/O Operation Two

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.