CS 1054 Lecture 15 File Readers and Writers

  • Slides: 15
Download presentation
CS 1054: Lecture 15 - File Readers and Writers

CS 1054: Lecture 15 - File Readers and Writers

Text Input/Output Read from Your Java Classes Write to Input Text file Output Text

Text Input/Output Read from Your Java Classes Write to Input Text file Output Text file

Example: Averaging Student Scores Perform calculations

Example: Averaging Student Scores Perform calculations

Perform Calculation with Java Program Java Class Input. txt Reads from Input file Performs

Perform Calculation with Java Program Java Class Input. txt Reads from Input file Performs Calculations Writes to Output file Output. txt

Steps to Input/Output • Input – Open file – Read data – Close file

Steps to Input/Output • Input – Open file – Read data – Close file • Output – Open file – Write file – Close file

Sample File. Input. Output Class Import java. io. *; public class File. Input. Output

Sample File. Input. Output Class Import java. io. *; public class File. Input. Output { // To read from File private String input. File. Name = “input. txt”; private File. Input. Stream fis; private Buffered. Reader br; // To write to Files private String output. File. Name = “output. txt”; private File. Output. Stream fos; private Print. Stream ps; }

public File. Input. Output() { // Open files fis = new File. Input. Stream(input.

public File. Input. Output() { // Open files fis = new File. Input. Stream(input. File. Name); // For Input br = new Buffered. Reader (new Input. Stream. Reader (fis)); fos = new File. Output. Stream(output. File. Name); //For output ps = new Print. Stream(fos); //To perform required calculations read. File(); }

 • Error message: “Unreported exception java. io. IOException must be caught or declared

• Error message: “Unreported exception java. io. IOException must be caught or declared to be thrown” Add clause throws Exception ** For vs. while loop… ** why use read. Line() ** how to convert integers to characters?

public void perform. Calculation() throws Exception { String line = br. read. Line(); //

public void perform. Calculation() throws Exception { String line = br. read. Line(); // line = Students|Scores line = br. read. Line(); // line = Student 1|89 line = br. read. Line(); // line = Student 2|92 line = br. read. Line(); // line = Student 3|65 line = br. read. Line(); // line = Student 4|70 line = br. read. Line(); // line = Student 5|13 lines = br. read. Line() // line = ? ? ? }

public void perform. Calculation() throws Exception { String line = br. read. Line(); while

public void perform. Calculation() throws Exception { String line = br. read. Line(); while (line != null) // if not end of file { line = br. read. Line(); // read next line from file // The line has both Students and scores } }

Some Classes to Know String. Tokenizer – Allows you to break strings at specified

Some Classes to Know String. Tokenizer – Allows you to break strings at specified delimiters (characters) String. Tokenizer st = new String. Tokenizer("this is a test"); while (st. has. More. Tokens()) { System. out. println(st. next. Token()); } prints the following output: This is a test

In our Example: Students|Scores => delimiter = ‘|’ String 1 =>Student. Id; String 2

In our Example: Students|Scores => delimiter = ‘|’ String 1 =>Student. Id; String 2 =>Score;

import java. util. String. Tokenizer public void perform. Calculation() throws Exception { String line

import java. util. String. Tokenizer public void perform. Calculation() throws Exception { String line = br. read. Line(); while (line != null) // if not end of file { line = br. read. Line(); // read next line from file String. Tokenizer stk = new String. Tokernizer(line, “|”); String student. Id = stk. next. Token(); //Student. Id String score = stk. next. Token(); //Score } }

Integer Class • Provides Method to Convert ints to Strings and Strings to ints

Integer Class • Provides Method to Convert ints to Strings and Strings to ints – static int parse. Int(String s) Note: It’s a static method String score = “ 89”; int Student. Score = Integer. parse. Int(score);

import java. lang. Integer; public void perform. Calculation() throws Exception { String line =

import java. lang. Integer; public void perform. Calculation() throws Exception { String line = br. read. Line(); while (line != null) // if not end of file { line = br. read. Line(); // read next line from file String. Tokenizer stk = new String. Tokernizer(line, “|”); String student. Id = stk. next. Token(); //Student. Id String score = stk. next. Token(); //Score // To convert String to an int student. Score = Integer. parse. Int(score); } }