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
Lab 10
- Create, execute, and submit each example below with the output
- Modify exampleSix to read file tooutfile2.txt compute and display the total
Then write the total to a file named tooutfile2A.txt
- 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
- 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
*/
Trending now
This is a popular solution!
Step by step
Solved in 2 steps