Download presentation
Presentation is loading. Please wait.
1
Announcements Quiz 2 Grades Posted on blackboard
2
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;
3
Scanner Input Scanner scan = new Scanner(System.in); while (scan.hasNextInt()) { i = scan.nextInt(); } // System.in is the keyboard “file”
4
Input Files import 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(); } 17 6 432 data.txt file
5
Display Output for (i=0;i < 100; i = i + 2) { System.out.println(i); } // System.out is the display window “file”
6
Output File Streams import java.io.*; class FileOutput { public static void main(String [ ] args) throws IOException { int i; FileOutputStream ofile = new FileOutputStream("data2.txt",false); //true:APP PrintWriter pw = new PrintWriter(ofile); for (i=0;i < 100; i = i + 2) { pw.println(i); } pw.close(); // Writes data to file on disk }} 0 2 … 98 data2.txt
7
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 5 8 9.3 Jon 6 14.335 Bill 0 35.67e9 Mary -23 -4.55 Smith -3 -4e3 xyz 8 9.3 Jon 6 14.335 Bill 0 3.567E10 Mary -23 -4.55 Smith -3 –4000.0 xyz Output:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.