Copying the contents of one text file to another in Java

I try to copy the contents of one text file ("1.txt") that contains 2-3 integers (for example: 1 2 3) to another text file ("2.txt"), but I get the following error when compiling

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 line: -

 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 after checking the code, I found that the finally () block is correctly closed.

+6
source share
8 answers

This is finally , not finally() :

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

By the way, you have an infinite loop:

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

You must read the data inside the loop to complete it:

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

In the finally block, the variables fr and fw cannot be found because they are declared in the try scope. Declare them outside:

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

Now, since they are initialized to null , you should 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 an IOException , which should also be handled:

 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 the main thread, just move it to the method that processes Closeable (note that both FileReader and FileWriter implement 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 this:

 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) { //... } } } 

Starting with Java 7, we have try-with-resources , so the code above can be rewritten like this:

 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(); } } } 
+25
source

More efficient way ...

 public class Main { public static void main(String[] args) throws IOException { File dir = new File("."); String source = dir.getCanonicalPath() + File.separator + "Code.txt"; String dest = dir.getCanonicalPath() + File.separator + "Dest.txt"; File fin = new File(source); FileInputStream fis = new FileInputStream(fin); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); FileWriter fstream = new FileWriter(dest, true); BufferedWriter out = new BufferedWriter(fstream); String aLine = null; while ((aLine = in.readLine()) != null) { //Process each line and add output to Dest.txt file out.write(aLine); out.newLine(); } // do not forget to close the buffer reader in.close(); // close buffer writer out.close(); } } 
+2
source

Compilation error

 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 // finally doesn't accept any arguments like catch { fr.close(); fw.close(); } } 
0
source
Block

A Finally should not have parentheses.

Try:

 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); c = fr.read(); // Add this line } } catch(IOException e) { System.out.println(e); } finally { fr.close(); fw.close(); } } } 
0
source

Check javapractices , you will get a more complete idea. this will help you understand more about trying to catch at last.

0
source
 import java.io.*; class FileDemo { public static void main(String args[])throws IOException { 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); } } catch(IOException e) { System.out.println(e); } finally { fr.close(); fw.close(); } } } 

1. Your code is incorrect> finally block does not take parentheses in front if it is. 2. Parameters always go only before methods. 3.Dear your volume of FileReader and FileWrier objects ends in try blocks, so you get another error in the finally block, which is not found and fr is not found 4. "throw IOEXception" also mentions the front of the main function

0
source
 public class Copytextfronanothertextfile{ public static void main(String[] args) throws FileNotFoundException, IOException { FileReader fr = null; FileWriter fw = null; try{ fr = new FileReader("C:\\Users\\Muzzammil\\Desktop\\chinese.txt"); fw = new FileWriter("C:\\Users\\Muzzammil\\Desktop\\jago.txt"); int c; while((c = fr.read()) != -1){ fw.write(c); } }finally{ if (fr != null){ fr.close(); } if(fw != null){ fw.close(); } } } } 
-1
source

Try this code:

 class CopyContentFromToText { public static void main(String args[]){ String fileInput = "C://Users//Adhiraj//Desktop//temp.txt"; String fileoutput = "C://Users//Adhiraj//Desktop//temp1.txt"; try { FileReader fr=new FileReader(fileInput); FileWriter fw=new FileWriter(fileoutput); int c; while((c=fr.read())!=-1) { fw.write(c); } fr.close(); fw.close(); } catch(IOException e) { System.out.println(e); } } } 
-2
source

Source: https://habr.com/ru/post/944096/


All Articles