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
  • IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and...

    IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. For example if file input is: This is a test File output should be 1. This is a test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BELOW FROM NOTEPAD FILE. This file contains lines of text to determine if you can properly read and write from a file.

  • Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list...

    Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list of positive and negative integers (duplicates are possible) separated by spaces and/or line breaks. Zero may be included. After reading the integers, the program saves them in a singly linked list in the same order in which they appear in the input file. Then, without changing the linked list, the program should print whether there exists two subsets of the list whose sums are...

  • java question: Write a program that reads in data from a text file named in.txt. Compute...

    java question: Write a program that reads in data from a text file named in.txt. Compute the sum of all the valid integers in the input file. Likewise, compute the sum of all the valid doubles in the input file. Write the former to an output file called int_total.txt, and write the latter to an output file called double_total.txt. B) Write a program that converts the Java source code from the next-line brace style to the end-of-line brace style. For...

  • Write a complete Java program in a file caled Module1Progrom.java that reads all the tokens from...

    Write a complete Java program in a file caled Module1Progrom.java that reads all the tokens from a file named dota.txt that is to be found in the same directory as the running program. The program should ignore al tokens that cannot be read as an integer and read only the ones that can. After reading all of the integers, the program should print all the integers back to the screen, one per line, from the smalest to the largest. For...

  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

  • java Program Write a program called Copy that accepts the names of an input file and...

    java Program Write a program called Copy that accepts the names of an input file and an output file on the command line and copies the contents of the input file to the output file, for example, csc$ java Copy infile.txt outfile.txt

  • java Write a program called Copy that accepts the names of an input file and an...

    java Write a program called Copy that accepts the names of an input file and an output file on the command line and copies the contents of the input file to the output file, for example, csc$ java Copy infile.txt outfile.txt

  • Write a program using Java Streams that reads an arbitrary text file and creates pairs from...

    Write a program using Java Streams that reads an arbitrary text file and creates pairs from everything it finds in the file that is a number and the word that precedes that number. Example input: I need to buy 5 notebooks and 4 folders in 2 days for school. Output. buy 5 and 4 in 2

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

  • In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a seri...

    In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a series of characters separated by a single space - The second line contains the number of states as an integer k 2 1; states will be numbered 0,1,..., k -1. The start state is always state O The third line contains a series...

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