Java file read

Source: Internet
Author: User

 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

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.