How to read and write large files
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