PackageA.ab;ImportJava.io.*; Public classFileWrite { Public Static voidMain (string[] args) {FileReader fr=NULL; Try{File F=NewFile ("D:\\dd\\d.txt"); intNum=0; Fr=NewFileReader (f); Char[] buf =NULL; for(intI=0;i<f.length (); i++) {buf=New Char[3]; Num=Fr.read (BUF); if(Num==-1) {System.out.println ("The file has been read!" "); Break; } System.out.println ("Read" +num+ "characters, respectively:" +NewString (buf,0, num)); } fr.close (); }Catch(Exception ex) {ex.printstacktrace (); } }}View Code
Package A.ab;import java.io.*;p ublic class FileWrite {public static void main (string[] args) {FileReader fr=null;try{file F=new File ("D:\\dd\\d.txt"), int num=0;fr=new filereader (f); Char [] cbuf =new Char[3];while ((Num=fr.read (CBUF))!=-1) { System.out.println ("read" +num+ "characters, respectively:" + new String (Cbuf,0,num));} System.out.println ("The file has been read! "); Fr.close ();} catch (Exception ex) {ex.printstacktrace ();}}}
Read (char[] cbuf) Throws IOException
-
Reads a character into an array. This method is blocked until an input is available, an I/O error occurs, or the end of the stream has been reached.
-
-
-
Parameters:
-
cbuf-Target Buffer
-
Return:
-
The number of characters read, or 1 if the end of the stream has been reached
-
Thrown:
-
IOException -If an I/O error occurs
-
first read the contents of the file into the char[] array, and then call the constructor of string (char[] ch), convert the char array to a string variable output
/c4>
Java file read