xampleTwo   This program loops through fileLoop.txt, computation, and display results       package charioloop;   import java.util.Scanner; import java.io.*; // public class Charioloop {      public static void main(String[] args) throws IOException     {    //Create a reference to the physical file    File loopfile = new   File("fileLoop.txt");              // connect a Scanner to the file    Scanner getAll = new Scanner( loopfile );          int num = 0, square = 0;         while(num != -1){       num = getAll.nextInt();       square = num * num ;             System.out.println("The square of " + num + " is " + square);     }     getAll.close();     }     }     /* Create fileLoop.txt in  I:\\Ajava\161\WPPractice\IO\charstream\charioloop   My fileLoop.txt looks like this:   2 3 2 10 -1

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter8: I/o Streams And Data Files
Section8.2: Reading And Writing Character-based Files
Problem 9E
icon
Related questions
Question

Lab 10 

 

  1. Create, execute, and submit each example below with the output

 

  1. Modify exampleSix to read file tooutfile2.txt compute and display the total

    Then write the total to a file named tooutfile2A.txt

 

  1. Modify exampleFour to read integer values from the keyboard, store them
    into a 15 element array, then write the array into a file named userin.txt

 

  1. Write a program to create
    two files with the names file4.txt and file4a.txt


    two 10 element int arrays with the names arrfile4 and arrfile4a; these 
    arrays are to have different values

    write the contents of arrfile4 to file4.txt

    write the contents of arrfile4a to file4a.txt

 

Examples

 

For examples 1 to 4, you must create the data files with a text tool, such as Notepad

 

 

exampleOne

 

This program reads data
   from a physical file,
   create a reference to the file,
   perform a computation,

   display the result,

   close the file

 

 

package chario;

 

import java.util.Scanner;

import java.io.*;

//

public class Chario {

 

//

    public static void main(String[] args) throws IOException

    {

    File file1 = new File("fileOne.txt"); //Create a reference to the physical file


/*
Use Notepad to create a text file named fileOne.txt
which must be in the same directory that src is in

 

To find src, open your project in NetBeans and hover its name, which is in the top left just below the Projects menu; this will display the path to your project

 

My project path is, yours will be different
I:\\Ajava\161\WPPractice\IO\charstream\chario
My  fileOne.txt is located in chario

*/

 

   

    Scanner getit = new Scanner( file1 );      // connect a Scanner to the file

    int num, square;

 

    num = getit.nextInt();

    square = num * num ;  

 

    System.out.println("The square of " + num + " is " + square);

    getit.close(); //Close the stream

    }

   

}

 

 

 

 

/*

 

 

This is not part of your Java code but the file your code will read
Your path will be different from mine

Create fileOne.txt in 


I:\\Ajava\161\WPPractice\IO\charstream\chario

 

Type an integer

Save fileOne.txt
Execute your program from NetBeans

 

 

*/

 

ExampleTwo

 

This program loops through fileLoop.txt, computation, and display results

 

 

 

package charioloop;

 

import java.util.Scanner;

import java.io.*;

//

public class Charioloop {

 

   public static void main(String[] args) throws IOException

    {

   //Create a reference to the physical file

   File loopfile = new   File("fileLoop.txt");  

      

    // connect a Scanner to the file

   Scanner getAll = new Scanner( loopfile );     

    int num = 0, square = 0;

   

    while(num != -1){

      num = getAll.nextInt();

      square = num * num ;      

      System.out.println("The square of " + num + " is " + square);

    }

    getAll.close();

    }

   

}

 

 

/*

Create fileLoop.txt in 


I:\\Ajava\161\WPPractice\IO\charstream\charioloop

 

My fileLoop.txt looks like this:

 

2

3

2

10

-1

 

 

 

*/

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Graphical User Interface
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr