Transcript Lecture 15

CS1054: Lecture 15
- File Readers and Writers
Text Input/Output
Read from
Your Java
Classes
Write to
Input
Text file
Output
Text file
Example: Averaging Student
Scores
Perform
calculations
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
• Output
– Open file
– Write file
– Close file
Sample FileInputOutput Class
Import java.io.*;
public class FileInputOutput
{
// To read from File
private String inputFileName = “input.txt”;
private FileInputStream fis;
private BufferedReader br;
// To write to Files
private String outputFileName = “output.txt”;
private FileOutputStream fos;
private PrintStream ps;
}
public FileInputOutput() throws Exception
{
// Open files
fis = new FileInputStream(inputFileName); // For Input
br = new BufferedReader (new InputStreamReader (fis));
fos = new FileOutputStream(outputFileName); //For output
ps = new PrintStream(fos);
//To perform required calculations
readFile();
}
• 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 readLine()
** how to convert integers to characters?
public void performCalculation() throws Exception
{
String line = br.readLine(); // line = Students|Scores
line = br.readLine(); // line = Student1|89
line = br.readLine(); // line = Student2|92
line = br.readLine(); // line = Student3|65
line = br.readLine(); // line = Student4|70
line = br.readLine(); // line = Student5|13
lines = br.readLine() // line = ?????
}
public void performCalculation() throws Exception
{
String line = br.readLine();
while (line != null) // if not end of file
{
line = br.readLine(); // read next line from file
// The line has both Students and scores
}
}
Some Classes to Know
StringTokenizer
– Allows you to break strings at specified delimiters (characters)
StringTokenizer st = new StringTokenizer("this is a test");
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
prints the following output:
This
is
a
test
In our Example:
Students|Scores => delimiter = ‘|’
String1 =>StudentId;
String2 =>Score;
import java.util.StringTokenizer
public void performCalculation() throws Exception
{
String line = br.readLine();
while (line != null) // if not end of file
{
line = br.readLine(); // read next line from file
StringTokenizer stk = new StringTokernizer(line, “|”);
String studentId = stk.nextToken(); //StudentId
String score = stk.nextToken(); //Score
}
}
Integer Class
• Provides Method to Convert ints to Strings
and Strings to ints
– static int parseInt(String s)
Note: It’s a static method
String score = “89”;
int StudentScore = Integer.parseInt(score);
import java.lang.Integer;
public void performCalculation() throws Exception
{
String line = br.readLine();
while (line != null) // if not end of file
{
line = br.readLine(); // read next line from file
StringTokenizer stk = new StringTokernizer(line, “|”);
String studentId = stk.nextToken(); //StudentId
String score = stk.nextToken(); //Score
// To convert String to an int
int studentScore = Integer.parseInt(score);
}
}