announcements quiz 2 grades posted on blackboard
TRANSCRIPT
Announcements
• Quiz 2 Grades Posted on blackboard
Files• Data in Main Memory is “volatile”
• File: Place for “permanent” data storage
• C: drive, A: drive, Flash drive, etc.
Disk
File
Main Memory
main()
int num;
string firstname;
Scanner Input
Scanner scan = new Scanner(System.in);
while (scan.hasNextInt())
{
i = scan.nextInt();
}
// System.in is the keyboard “file”
Input Filesimport java.io.*;import java.util.Scanner;
class FileInput{public static void main(String [ ] args) throws IOException{File ifile = new File("data.txt");Scanner scan = new Scanner(ifile);while (scan.hasNextInt()){
i = scan.nextInt(); }}}
176432
data.txt file
Display Output
for (i=0;i < 100; i = i + 2)
{
System.out.println(i);
}
// System.out is the display window “file”
Output File Streamsimport java.io.*;class FileOutput{public static void main(String [ ] args) throws IOException{int i;FileOutputStream ofile = new FileOutputStream("data2.txt",false); //true:APPPrintWriter pw = new PrintWriter(ofile);for (i=0;i < 100; i = i + 2){pw.println(i);}pw.close(); // Writes data to file on disk
}}
02…98
data2.txt
import java.util.Scanner;import java.io.*; class FormatFileData{public static void main(String [ ] args) throws IOException{
int loops, integer, i;float decimal;String name;
File ifile = new File("mydata.txt");Scanner scan = new Scanner(ifile);loops = scan.nextInt();for(i= 0 ; i < loops; i++){integer = scan.nextInt();decimal = scan.nextFloat();name= scan.next();System.out.print(integer + " ");System.out.print(decimal + " ");System.out.print(name + " ");System.out.println();}
}}
mydata.txt file
58 9.3 Jon6 14.335 Bill0 35.67e9 Mary-23 -4.55 Smith-3 -4e3 xyz
8 9.3 Jon6 14.335 Bill0 3.567E10 Mary-23 -4.55 Smith-3 –4000.0 xyz
Output: