Program to swap the contents of two files in Java

Swap-The-Content-Of-Two-Files-In-Java

Swap-The-Content-Of-Two-Files-In-JavaThis program was asked to me during an interview. I was given a computer with an IDE and was even allowed to use the internet. But there was a time limit. I managed to do the program and finally got the job. Though it’s a very simple program what the interviewer was trying to see was if I knew the use of File Handling properly. And luckily I managed to impress him with my program. And one more thing that was the first time I used File Handling in Java.

Program to swap the contents of two files in Java?

Its simple. Just use the normal method of swapping two variables. These are the steps to swap two variables var1 and var2.
1. Copy the content of var1 to a temporary variable temp.
2. Copy the content of var2 to var1.
3. Copy the content of temp to var2.

Now replace it with File1.txt and File2.txt for the swapping with files. File 1 and File2 are the names of the files to be swapped.

Let’s see the code.


import java.io.*;
import java.io.BufferedWriter;

public class SwapFiles {

public static void main(String[] args) {
 try {
 //copy the contents of file1.txt to temp.txt
 String temp = "";
 BufferedReader br = new BufferedReader(new FileReader("File1.txt"));
 BufferedWriter bw = new BufferedWriter(new FileWriter("temp.txt"));
 while ((temp = br.readLine()) != null) {
 bw.write(temp);
 bw.newLine();
 bw.flush();
 }
 //copy the contents of file2.txt to a file1.txt
 br = new BufferedReader(new FileReader("File2.txt"));
 bw = new BufferedWriter(new FileWriter("File1.txt"));
 while ((temp = br.readLine()) != null) {
 bw.write(temp);
 bw.newLine();
 bw.flush();
 }
 //copy the contents of temp.txt to file2.txt
 br = new BufferedReader(new FileReader("temp.txt"));
 bw = new BufferedWriter(new FileWriter("File2.txt"));
 while ((temp = br.readLine()) != null) {
 bw.write(temp);
 bw.newLine();
 bw.flush();
 }
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
}

I used BufferedReader and BufferedWriter and not any other classes which actually impressed the interviewer and he even asked me why I didn’t use Scanner. Be sure that you know it. The answer is BufferedReader is a lot faster than Scanner because it buffers the character so you don’t have to access the file each time you want to read a char from it. Also he asked questions about the connection I made with the BufferedReader and BufferedWriter with FileReader and FileWriter.

There is also an interesting addition that is the flush() method which actually writes the lines into the files from the buffers. If you ignore this line nothing will get printed. But this was not it. I realized that I had to do the same thing thrice that is copying the content of one file to another so I declared another function copy(String, String) to do it. This was what impressed the interviewer. The program I submitted was this.


import java.io.*;
import java.io.BufferedWriter;

public class SwapFiles {
 //function to copy the contents of one file to another
 public void copy(String a, String b) {
 try {
 //copy the contents one file to another
 String temp = "";
 BufferedReader br = new BufferedReader(new FileReader(a));
 BufferedWriter bw = new BufferedWriter(new FileWriter(b));
 while ((temp = br.readLine()) != null) {
 bw.write(temp);
 bw.newLine();
 bw.flush();
 }
 } catch (IOException e) {
 e.printStackTrace();
 }
 }

public static void main(String[] args) {
 SwapFiles s = new SwapFiles();
 s.copy("file1.txt","temp.txt");
 s.copy("file2.txt","file1.txt");
 s.copy("temp.txt","file2.txt");
 }
}

And I got the joining call the next day. Hopefully you enjoyed reading this tutorial. If you have any doubts please use the comments.  Thank you and visit www.intechgrity.com for more tutorials.

2 comments

  1. Swashata

    Very intelligent programming. But isn’t it just better to swap just the path (filename) of the two files? Will save you even more time.

    • arnab Post author

      This was the first thing I told him I’ll do and was told to actually do the swapping. Sad me 🙁

Comments are closed.