java - How does reading a file from a char[] array work in File handling? -
this code of copying text of 1 file another.
public class writer { public static void main(string args[]) throws ioexception { file f=new file("d:/test.txt"); filereader fr=new filereader(f); char cbuff[]=new char[(int)f.length()]; int c=fr.read(cbuff); fr.close(); filewriter fw=new filewriter("d:/newtest.txt"); fw.write(cbuff); fw.close(); } }
i wanna know line does
char cbuff[]=new char[(int)f.length()];
how filereader recognizes store text of file it's reading cbuff[]?
when comment line
int c=fr.read(cbuff);
the code doesn't work.why?
f.length(); // gives length in bytes, type of result 'long' (int) f.length(); // casts long int char cbuff[] = new char [(int) f.length()]; // create new variable, array of char , called cbuff. assign new array of char it, length specified length of file
here's link showing filereader.read(char[]) reads file array. it's method inherited reader class.
fr.read(cbuff); //this line actual reading file f. results stored in char array cbuff (which why initialized length of file)
now, result of fr.read() stored in int, that's number of characters have been read. unless there error, should exact same length of cbuff.
Comments
Post a Comment