Question

Write a program in Java that reads the file and does two things: Write to an...

Write a program in Java that reads the file and does two things:

Write to an output file called

dataSwitch.txt

the same data from the input file, but switch the

order of the data entries on each line. For example, if the first line of the input file is

“90001 57110”, the first line of the output file would be “57110 90001”.

Additionally, print to the screen the number of lines in the file with a label

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
 *  Java Program for data switch
 */

import java.io.*;
import java.util.*;

public class Main {

  public static void main(String args[]) {
    try {
      Scanner inFile = new Scanner(new File("in.txt"));
      FileWriter outFile = new FileWriter("out.txt");
      int countLines = 0;
      
      while (inFile.hasNextLine()) {
        int first = inFile.nextInt();
        int last = inFile.nextInt();
        outFile.write(Integer.toString(last) + " " + Integer.toString(first) + "\n");
        countLines++;
      }
      
      System.out.println("Number of lines in a file: " + countLines);

      inFile.close();
      outFile.close();
    }
    catch (FileNotFoundException e) {
      System.err.println("File not found. Please scan in new file.");
    }
    catch (IOException e) {
      System.out.println("IO Error occurred.");
      e.printStackTrace();
    }
  }
}

D OO https://AggravatingCheapTrials.imtusharsharma.repl.run Files D Main.java in.txt Main.java 3 saved 1 /* 2 * Java Program

Add a comment
Know the answer?
Add Answer to:
Write a program in Java that reads the file and does two things: Write to an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT