Question

This is a code of copying text of one file to 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 what this line does

char cbuff[]=new char[(int)f.length()];

How does FileReader recognizes to store the text of file it's reading into this cbuff[]?
When I comment this line

int c=fr.read(cbuff);

the code doesn't work.Why?

Was it helpful?

Solution 2

f.length(); // gives us the length in bytes, the type of the result is 'long'
(int) f.length(); // casts the long to an int
char cbuff[] = new char [(int) f.length()]; // create a new variable, which is an array of char and is called cbuff. assign a new array of char to it, with the length specified by the length of the file

Here's the link showing that FileReader.read(char[]) reads from the file into an array. It's actually a method inherited from the Reader class.

fr.read(cbuff); //this is the line that does the actual reading from the file f. The results are stored in the char array cbuff (which is why we initialized it to be the length of the file)

Now, the result of fr.read() is stored in an int, that's just the number of characters that have been read. Unless there is an error, this should be the exact same as the length of cbuff.

OTHER TIPS

OK, first of all, here is a code which will work for any file, not just text files:

final Path src = Paths.get("D:/test.txt");
final Path dst = Paths.get("D:/newTest.txt");
Files.copy(src, dst);

Now, the code you have is severely broken. Let us start with this:

char cbuff[]=new char[(int)f.length()];

This will create an array of chars the same size as the file size; but it does not account for the fact that byte<->char mapping is not 1 to 1. For instance, if the file is encoded in UTF-8, "à" is encoded in two bytes; the char array will therefore be at least too long by one element. It is even worse in UTF-16 (each char is two bytes) or even UTF-32.

Second problem:

FileReader fr=new FileReader(f);

No encoding is specified; the default JVM encoding is used.

As such, if you try and read a file encoded in UTF-8 with a default JVM encoding of windows-1252, you will corrupt the destination file.

The same goes for the output file: no encoding specified. Such a file is essentially "non portable".

Third problem: there is:

int c=fr.read(cbuff);

This will return the number of characters actually read; good.

But this isn't used when you write the file back! The write code should be:

fw.write(cbuff, 0, c);
char cbuff[]=new char[(int)f.length()];

Create the char array with the size equal to the length of the file (content of file)

int c=fr.read(cbuff);

Read the file and put the content in the char array

I wanna know what this line does

char cbuff[]=new char[(int)f.length()];

This line simply creates a new char array that has the same length as the input file.

int c=fr.read(cbuff);

This line then reads the input file and stores it in the char array (cbuff). If you comment this last line, the content of the input file is never written to the cbuff, so when you try to write it to the new file, it is empty.

The code

char cbuff[]=new char[(int)f.length()];

basicly initializes the container in the memory, where the file will be loaded, it does not read the file though. The line

int c=fr.read(cbuff);

reads the file into the memory buffer. How come you were expecting it to work without that line? And regarding your question - "How does FileReader recognizes to store the text of file it's reading into this cbuff[]?" - is basically because of that line...

fr.read(cbuff); // translation: File reader, please read the file into the buffer cbuff.

Hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top