Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying the Contents of One text file to Another in Java

I am trying to copy the contents of one text file ("1.txt") which contains 2-3 integer numbers (ex: 1 2 3) to another text file ("2.txt") but I am getting the following error upon compilation

import java.io.*;
class FileDemo {
    public static void main(String args[]) {
      try {
          FileReader fr=new FileReader("1.txt");
          FileWriter fw=new FileWriter("2.txt");
          int c=fr.read();
          while(c!=-1) {
            fw.write(c);
          }
      } catch(IOException e) {
          System.out.println(e);
      } finally() { 
          fr.close();
          fw.close();
      }
    }
}

Command prompt:-

C:\Documents and Settings\Salman\Desktop>javac FileDemo.java
FileDemo.java:20: error: '{' expected
                finally()
                       ^
FileDemo.java:20: error: illegal start of expression
                finally()
                        ^
FileDemo.java:20: error: ';' expected
                finally()
                         ^
FileDemo.java:27: error: reached end of file while parsing
}
 ^
4 errors

But upon checking the code, I find that the finally() block is properly closed.

like image 362
Salman Avatar asked May 02 '13 05:05

Salman


People also ask

How do you copy the contents of a file to another file in Java?

In Java, we can copy the contents of one file to another file. This can be done by the FileInputStream and FileOutputStream classes. It is a byte input stream class which helps in reading the bytes from a file. It provides different methods to read the data from a file.

How do you copy and paste a file in Java?

Another common way to copy a file with Java is by using the commons-io library. The latest version can be downloaded from Maven Central. Then, to copy a file we just need to use the copyFile() method defined in the FileUtils class. The method takes a source and a target file.

Can you copy and paste in Java?

You should now be able to copy and paste between Java Swing applets and other computer applications such as Microsoft Excel. Please note to use Ctrl-C and Ctrl-V to copy and paste. If you still cannot copy and paste, try restarting your computer as some computers may require this.


1 Answers

It's finally, not finally():

try {
    //...
} catch(IOException e) {
    //...
} finally {
    //...
}

By the way, you have an endless loop there:

int c=fr.read();
while(c!=-1) {
    fw.write(c);
}

You must read the data inside the loop in order to let it finish:

int c=fr.read();
while(c!=-1) {
    fw.write(c);
    c = fr.read();
}

In the finally block, your fr and fw variables can't be found since they're declared in the scope of the try block. Declare them outside:

FileReader fr = null;
FileWriter fw = null;
try {
    //...

Now, since they are initialized with null value, you must also do a null check before closing them:

finally {
    if (fr != null) {
        fr.close();
    }
    if (fw != null) {
        fw.close();
    }
}

And the close method on both can throw IOException that must be handled as well:

finally {
    if (fr != null) {
        try {
            fr.close();
        } catch(IOException e) {
            //...
        }
    }
    if (fw != null) {
        try {
            fw.close();
        } catch(IOException e) {
            //...
        }
    }
}

In the end, since you don't want to have a lot of code to close a basic stream, just move it into a method that handles a Closeable (note that both FileReader and FileWriter implements this interface):

public static void close(Closeable stream) {
    try {
        if (stream != null) {
            stream.close();
        }
    } catch(IOException e) {
        //...
    }
}

In the end, your code should look like:

import java.io.*;
class FileDemo {
    public static void main(String args[]) {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader("1.txt");
            fw = new FileWriter("2.txt");
            int c = fr.read();
            while(c!=-1) {
                fw.write(c);
                c = fr.read();
            }
        } catch(IOException e) {
            e.printStackTrace();
        } finally {
            close(fr);
            close(fw);
        }
    }
    public static void close(Closeable stream) {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch(IOException e) {
            //...
        }
    }
}

Since Java 7, we have try-with-resources, so code above could be rewritten like:

import java.io.*;
class FileDemo {
    public static void main(String args[]) {
        //this will close the resources automatically
        //even if an exception rises
        try (FileReader fr = new FileReader("1.txt");
             FileWriter fw = new FileWriter("2.txt")) {
            int c = fr.read();
            while(c!=-1) {
                fw.write(c);
                c = fr.read();
            }
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}
like image 122
Luiggi Mendoza Avatar answered Sep 20 '22 11:09

Luiggi Mendoza