Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

45
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1

Transcript of Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

Page 1: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

1

Chapter 10

Ch 1 – Introduction toComputers and Java

Streams and File IO

Page 2: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

2

Chapter 10

10.1 File IO Overview

10.2 Text-File IO

10.3 File Techniques

10.6 Graphics Supplement

Page 3: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

3

10.1File IO

Overview

Page 4: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

4

A stream is a stream is a stream

All IO can be viewed as a stream of data

Page 5: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

5

An Input stream delivers data toyour program

Streaming data

Page 6: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

6

An Output stream accepts data from your program

Streaming data

Page 7: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

7

A file could be treated astext or binary

All data is binary at the end of the day

Page 8: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

8

A text file treats the binary data as characters

1 2 3 4 5 - 4 0 2 7 8

Each digit uses 1 (ASCII) or 2 (Unicode) bytes

Page 9: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

9

A binary file manipulates the binary data

12345 -4027 8

Each data item uses the same number of bytes (4 for ints)

Page 10: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

10

Recap

A program reads data from an input stream

A program writes data to an output stream

A text file interprets its binary data as text

A binary file deals with the binary data itself

Page 11: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

11

10.2Text-File IO

Page 12: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

12

There are four steps in using file IO

Open the file stream1

Test the Connection2

Perform the IO3

Close the stream4

Page 13: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

13

(1) Open the file stream// ReadingScanner fin = null;

try { fin = new Scanner(new File(filename));}

// WritingPrintWriter fout = null;

try { // Create empty file fout = new PrintWriter(filename); // or fout = new PrintWriter(new FileOutputStream(filename, false));

// Append to the file fapp = new PrintWriter(new FileOutputStream(filename, true));}

Page 14: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

14

(2) Test the Connection

catch (FileNotFoundException e) { System.err.println("Error opening file " + filename); System.exit(1);}

Page 15: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

15

(3) Perform the IO

// Readingwhile (fin.hasNextLine()) { String line = fin.nextLine(); // process the line ...}

// Writingfout.println("Write some output");...

Page 16: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

16

(4) Close the Stream

// Readingfin.close();

// Writingfout.close();

Page 17: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

17

Application Deconstructed<FileOutput.java>

package fileoutput;

import java.io.PrintWriter;import java.io.FileNotFoundException;

public class FileOutput { public static void main(String[] args) { PrintWriter fout = null; String filename = "fileOutput.txt"; // Open and test the output stream. try { fout = new PrintWriter(filename); } catch (FileNotFoundException e) { System.err.println("Error opening the file " + filename); System.exit(1); }// end try

Page 18: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

18

Application Deconstructed<FileOutput.java>

// Perform some IO. for (int line = 1; line <= 5; line++) { fout.println(line + ": " + "This is just a line"); } // Close the stream. fout.close(); System.out.println("File " + filename + " was created successfully."); }// end main()} // end FileOutput

Page 19: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

19

Application Deconstructed<FileOutput.java>

Page 20: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

20

Application Deconstructed<FileInput.java>

package fileinput;

import java.util.Scanner;import java.io.File;import java.io.FileNotFoundException;

public class FileInput { public static void main(String[] args) { String filename = "fileOutput.txt"; Scanner fin = null;

// Open and test the stream. try { fin = new Scanner(new File(filename)); } catch (FileNotFoundException e) { System.err.println(e); System.exit(1); }// end try

Page 21: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

21

Application Deconstructed<FileInput.java>

// Perform the IO. while (fin.hasNext()) { int lineNumber = Integer.parseInt( fin.next().replace(":", "")); String lineText = fin.nextLine(); System.out.println(lineText + lineNumber); }// end while // Close the stream. fin.close(); }// end main()}// end FileInput

Page 22: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

22

Recap

Open the file stream first in a try block

Catch any errors that occur during opening

Perform the IO

Close the stream as soon as IO is finished

Page 23: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

23

10.3File

Techniques

Page 24: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

24

The File class offers useful functionality

public boolean canRead() // Can program read from the file?

public boolean canWrite() // Can program write to the file?

public boolean delete() // Was file detected?

public boolean exists() // Does the file exist?

String getName() // Get file's name.

String getPath() // Get file's path.

public long length() // Get file's path.

Page 25: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

25

Application Deconstructed<FileClass.java>

package fileclass;

import java.io.File;import java.util.Scanner;

public class FileClass {

public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); File file = null; // Ask the user for the file. System.out.print("Enter the filename: "); file = new File(keyboard.nextLine());

Page 26: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

26

Application Deconstructed<FileClass.java>

// Let's test some File methods. System.out.println(""); System.out.println("Does file exist? " + String.valueOf(file.exists())); ... }// end main()}// end FileClass

Page 27: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

27

Application Deconstructed<FileClass.java>

// Let's test some File methods. ... System.out.println("Can file be read? " + String.valueOf(file.canRead())); ... }// end main()}// end FileClass

Page 28: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

28

Application Deconstructed<FileClass.java>

// Let's test some File methods. ... System.out.println("Can file be written? " + String.valueOf(file.canWrite())); ... }// end main()}// end FileClass

Page 29: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

29

Application Deconstructed<FileClass.java>

// Let's test some File methods. ... System.out.println("Filename: " + file.getName()); ... }// end main()}// end FileClass

Page 30: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

30

Application Deconstructed<FileClass.java>

// Let's test some File methods. ... System.out.println("File path: " + file.getPath()); ... }// end main()}// end FileClass

Page 31: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

31

Application Deconstructed<FileClass.java>

// Let's test some File methods. ... System.out.println("File absolute path: " + file.getAbsolutePath()); ... }// end main()}// end FileClass

Page 32: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

32

Application Deconstructed<FileClass.java>

// Let's test some File methods. ... System.out.println("File length: " + file.length() + " bytes"); }// end main()}// end FileClass

Page 33: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

33

Application Deconstructed<FileClass.java>

// Let's test some File methods. System.out.println(""); System.out.println("Does file exist? " + String.valueOf(file.exists())); System.out.println("Can file be read? " + String.valueOf(file.canRead())); System.out.println("Can file be written? " + String.valueOf(file.canWrite())); System.out.println("Filename: " + file.getName()); System.out.println("File path: " + file.getPath()); System.out.println("File absolute path: " + file.getAbsolutePath()); System.out.println("File length: " + file.length() + " bytes"); }// end main()}// end FileClass

Page 34: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

34

Application Deconstructed<FileClass.java>

Page 35: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

35

Application Deconstructed<CSVFile.java>

package csvfile;

import java.io.PrintWriter;import java.io.File;import java.io.FileNotFoundException;import java.util.Scanner;

public class CSVFile {

public static void main(String[] args) { Scanner fin = null; PrintWriter fout = null; String filename = "csv.txt";

Page 36: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

36

Application Deconstructed<CSVFile.java>

try { fin = new Scanner(new File(filename)); fout = new PrintWriter("csvout.txt"); } catch (FileNotFoundException e) { System.err.println(e); System.exit(1); }// end try

Page 37: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

37

Application Deconstructed<CSVFile.java>

String line = null; int pos;

// Read from the input file and write to output file. while (fin.hasNextLine()) { line = fin.nextLine(); // Write the record number. pos = line.indexOf(","); fout.print(line.substring(0, pos) + ":"); line = line.substring(pos + 1); // Write the first name. pos = line.indexOf(","); fout.print(line.subSequence(0, pos) + ":"); line = line.substring(pos + 1); // Write the last name. fout.println(line); }// end while

Page 38: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

38

Application Deconstructed<CSVFile.java>

fin.close(); fout.close(); System.out.println(filename + " was read and " + "csvout.txt was written."); }// end main()}// end CSVFile

Page 39: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

39

Application Deconstructed<CSVFile.java>

Page 40: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

40

Recap

Use a File object for useful file information

A csv file requires special handling (for now anyway)

Page 41: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

41

10.6Graphics

Supplement

Page 42: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

42

Application Deconstructed<FileFrame.java>

package fileframe;

import javax.swing.JFrame;import javax.swing.JTextArea;import java.awt.Container;import java.util.Scanner;import java.io.File;import java.io.FileNotFoundException;

public class FileFrame extends JFrame { JTextArea contentsTextArea; public static void main(String[] args) { FileFrame frame = new FileFrame(); frame.setSize(600, 500); frame.setTitle("File Viewer :: fileOutput.txt"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }// end main()

Page 43: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

43

Application Deconstructed<FileFrame.java>

// Constructor. public FileFrame() { initComponents(); }// end FileFrame() private void initComponents() { // Create the TextArea. contentsTextArea = new JTextArea(); Container contentPane = getContentPane(); // Add the control to the content pane. contentsTextArea.setSize(590, 470); contentPane.add(contentsTextArea); // Load the text file contents into the TextArea. LoadTextAreaWithFile("fileOutput.txt"); }// end initComponents()

Page 44: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

44

Application Deconstructed<FileFrame.java>

private void LoadTextAreaWithFile(String filename) { Scanner fin = null; try { fin = new Scanner(new File(filename)); } catch (FileNotFoundException e) { System.err.println(e); System.exit(1); }// end try while (fin.hasNextLine()) { contentsTextArea.append(fin.nextLine() + "\n"); }// end while fin.close(); }// end LoadTextAreaWithFile()}// end FileFrame

Page 45: Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

45

Application Deconstructed<FileFrame.java>