Download presentation
Presentation is loading. Please wait.
1
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Files
2
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 2 Files To use files, use File class File class can’t read/write To read/write files, use Reader/Writer classes File input can be text-based (what we’ll do), stream-based
3
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 3 Readers, writers, streams Readers and writers deal with textual input. –Based around the char type. Streams deal with binary data. –Based around the byte type.
4
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 4 Text output Use the FileWriter class. –Tie File object to diskfile name –Open a FileWriter; tie to File. –Write to the FileWriter. –Close the FileWriter. Failure at any point results in an IOException.
5
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 5 Text output try { FileWriter writer = new FileWriter("name of file"); while(there is more text to write) {... writer.write(next piece of text);... } writer.close(); } catch(IOException e) { something went wrong with accessing the file }
6
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 6 // file output. public void writeto(String fileout, int nbrlines) throws IOException // have throws or throw { File outfile = new File(fileout); FileWriter fw = new FileWriter(outfile); fw.write("There were " + nbrlines + “ lines in the input file."); fw.close(); }
7
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 7 import java.io.*; public class countlines { // all methods that do something inserted here public static void main(String[] args) { if (args.length != 2) { System.out.println("to run, type: 'countlines filein fileout'"); System.exit(1); } // create a new countline object with first arg as input file, second as output countlines cl = new countlines(args[0], args[1]); }
8
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 8 Write a program to count number of lines in a file and write the number to an output file java countlines myinfile.txt myoutfile.txt would return 2 if file is: This is a file with two lines. myinfile.txt
9
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 9 // constructor public countlines(String filein, String fileout) { try { int nbrLines = readfrom(filein); writeto(fileout, nbrLines); } catch (IOException e) { // will catch IO exception not caught by readfrom and writeto System.out.println("Problems with IO. " + " Program aborted without successful write."); }
10
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 10 Scanner Class: for reading files public void readfrom(String filein) { Scanner scanner; try { scanner = new Scanner(new File(filein)); String line = scanner.nextLine( ); // also next( ), nextInt( ), nextDouble( ) while (line != null) { System.out.println(line); // for debugging only; countlines++; } catch (FileNotFoundException ex) { System.out.println("File not found...aborting program."); System.exit(1); } }
11
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 11 Console input Scanner class: Java 5 import java.util.*; class MyScanner { public MyScanner ( ) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number: "); int x = scanner.nextInt(); int y = scanner.nextInt(); System.out.println("The sum of " + x + " + " + y + " is " + (x+y)); }
12
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 12 File input using Scanner import java.util.*; import java.io.*; class MyScanner { public MyScanner ( ) { Scanner scanner; try { scanner = new Scanner(new File("infile.java")); int x = scanner.nextInt( ); int y = scanner.nextInt( ); System.out.println("The sum of " + x + " + " + y + " is " + (x+y)); } catch (FileNotFoundException ex) { System.out.println("File not found...aborting program."); System.exit(1); } }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.