Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

File input/output 1 Create a program, fileio.java, that reads in 3 numbers (inte

ID: 3778552 • Letter: F

Question

File input/output

1 Create a program, fileio.java, that reads in 3 numbers (integers) from the keyboard, and writes them to the screen.

2 Modify the program so that it reads in the 3 numbers (integers) from a file (and writes them to the screen as before).

To do this, you will also need to create a text file. It must be also be created in the same folder that contains fileio.java. The easiest way to do this is to open a window to the folder that contains fileio.java. Next, right-click in a blank area within this window. Then choose New and then Text Document. This will create a text file called New Text Document. Change the name of this file to data. Finally, double-click on this file to run Notepad which will allow you to enter numbers into this file. Don't forget to save your changes. You may now read a file name data.txt in your Java program.

3 Now modify your program to also write the numbers back to a different output file.

4 Now, please make another modification. In your input file, make sure that the integers are two digits in length (i.e. between 10 and 99). Instead of writing the numbers back to the screen and to a file, I would like you to write them backwards to the screen and to the output file. In other words, if the numbers were 21, 17 and 36, I'd like you to write the numbers 12, 71, and 63 to the screen and to the output file. How can you do this? Here is an idea. If the number was 36, store the first digit of the number (3) in a variable first_digit, and the second digit of the number (6) in a variable second_digit. Now multiply the second_digit by 10 (so it now is 60) and add it to the first_digit, giving 63!

5 Please make one final modification. Prompt the user for the name of the output file and read it into a string (and use it to write the output to that file).

Explanation / Answer

//Question 1

package fileio;

import java.util.Scanner;

public class Fileio {

   /**
   * @param args
   */
   public static void main(String[] args) {
       int num1,num2,num3;
       Scanner scan= new Scanner(System.in);
       System.out.println("Enter num1");
       num1=scan.nextInt();
      
       System.out.println("Enter num2");
       num2=scan.nextInt();
      
       System.out.println("Enter num3");
       num3=scan.nextInt();
      
       System.out.println("Num1: "+num1);
       System.out.println("Num2: "+num2);
       System.out.println("Num3: "+num3);
   }

}

------output-----
Enter num1
10
Enter num2
20
Enter num3
30
Num1: 10
Num2: 20
Num3: 30
-----------

//Question 2

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Scanner;

public class Fileio {

   /**
   * @param args
   * @throws FileNotFoundException
   */
   public static void main(String[] args) throws FileNotFoundException {
       int num1,num2,num3;
       URL url=Fileio.class.getResource("data.txt");
       FileInputStream fis = new FileInputStream(url.getPath());
Scanner scanner = new Scanner(fis);
  
//reading file line by line using Scanner in Java
System.out.println("Reading file line by line in Java using Scanner");
  
while(scanner.hasNextLine()){
System.out.println(scanner.nextLine());
}

   }

}
-----output---
Reading file line by line in Java using Scanner
24
56
98
-------------------

Question 3

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;

public class Fileio {

   /**
   * @param args
   * @throws IOException
   */
   public static void main(String[] args) throws IOException {
       int num1,num2,num3;
       URL url=Fileio.class.getResource("data.txt");
       FileInputStream fis = new FileInputStream(url.getPath());
Scanner scanner = new Scanner(fis);
  
File file = new File("output.txt");

       // if file doesnt exists, then create it
       if (!file.exists()) {
           file.createNewFile();
       }

       FileWriter fw = new FileWriter(file.getAbsoluteFile());
       BufferedWriter bw = new BufferedWriter(fw);
while(scanner.hasNextLine()){
   //wrinting to the file
   bw.write(scanner.nextLine());
   bw.write(" ");
}
bw.close();

   }

}
------
//creates a output.txt file in the workspace. If not found please refresh the worksspace

----------------
Question 4 & 5


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;

public class Fileio {

   /**
   * @param args
   * @throws IOException
   */
   public static void main(String[] args) throws IOException {
       int num1,num2,num3;
       String outputFileName;
       Scanner scan= new Scanner(System.in);
       System.out.println("Provide the output file name:");
       outputFileName=scan.nextLine();
       URL url=Fileio.class.getResource("data.txt");
       FileInputStream fis = new FileInputStream(url.getPath());
Scanner scanner = new Scanner(fis);
  
File file = new File(outputFileName+".txt");

       // if file doesnt exists, then create it
       if (!file.exists()) {
           file.createNewFile();
       }

       FileWriter fw = new FileWriter(file.getAbsoluteFile());
       BufferedWriter bw = new BufferedWriter(fw);
while(scanner.hasNextLine()){
   String value=scanner.nextLine();
   int first_digit=Integer.parseInt(value.charAt(0)+"");
   int second_digit=Integer.parseInt(value.charAt(1)+"");
   int rev=second_digit*10+first_digit;
   //wrinting to the file
   bw.write(String.valueOf(rev));
   bw.write(" ");
}
bw.close();

   }

}
---------output-------
Provide the output file name:
revFile
---contains of input file is---
24
56
98

---contains of output file is---
42
65
89

//feel free to ask queries. God bless you!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote