Finish the following program “SplitFile.java” below that does the opposite of In
ID: 3683943 • Letter: F
Question
Finish the following program “SplitFile.java” below that does the opposite of Interleave. That is, it takes as input one file, and writes out interleaved lines to two different files. In this case, all three file names will be given on the command line.
import j ava. util- import java.io- public class 8plitFile public sttic void main (3tring[] args) // Usage statement 3 inputs needed) Your code herel PrintWriter file1 = null; PrintWriter file2 = null; tEy // Open 3canner for file named rqs[0] 3canne r can // [Add code here] // Open a Printwriter for file named rgs [1] file1 ner PrintWriter (ne r File (args [1] ; // Open a Printwriter for file named rgs [2] file2 = // [Add code here] /I Read a line fro scan // Write that line to file1 / Add code herel // Read a line from can // rite that line to file2 add code here] // ctch the IOException / Add code herel // crete fin11y b1ck that cl e the two PrintWriter // Add code hereExplanation / Answer
import java.util.*;
import java.io.*;
public class SplitFile {
public static void main(String[] args)
{
if(args.length!=3)
{
System.err.println("Please provide three filenames for the program");
return;
}
PrintWriter file1=null;
PrintWriter file2=null;
try
{
Scanner scan = new Scanner(new File(args[0]));
file1 = new PrintWriter(new File(args[1]));
file2 = new PrintWriter(new File(args[2]));
while(scan.hasNextLine())
{
String s = scan.nextLine();
file1.print(s);
if(scan.hasNextLine())
{
String s1 = scan.nextLine();
file2.print(s1);
}
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
file1.close();
file2.close();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.