70 likes | 216 Views
IC211 Lectures 10. Streams and File I/O (Core Java 2 Chapter 12). Streams and File I/O. Types of Streams Reading from and Writing to Streams File Input File Output. FileReader Example (Continued).
E N D
IC211Lectures 10 Streams and File I/O (Core Java 2 Chapter 12)
Streams and File I/O • Types of Streams • Reading from and Writing to Streams • File Input • File Output
FileReader Example(Continued) • Finished off last lecture with a program that will read the file mids.txt, break each line of input into its individual tokens (alpha, last name, first name, company, major) and display the contents to the console (see next slide). mids.txt M090066 AGUILERA ALEJANDRO 4 INFORMATION TECHNOLOGY M090270 ARONHALT DRAKE 1 INFORMATION TECHNOLOGY M090390 BARNES ELIZABETH 5 INFORMATION TECHNOLOGY M090654 BOSWORTH MICHAEL 28 COMPUTER SCIENCE ... • Next step • Create a class called Mid • Instantiate a Mid object for each line of data and store it in an ArrayList.
InputDemo.java import java.io.*; import java.util.*; public class InputDemo { public InputDemo() { } public void readFile() { String alpha, first, last, major; int company; try { FileReader inFile = new FileReader("mids.txt"); BufferedReader br = new BufferedReader(inFile); String input; input = br.readLine(); while (input != null) { Scanner parser = new Scanner(input); alpha = parser.next(); last = parser.next(); first = parser.next(); company = parser.nextInt(); major = parser.nextLine(); System.out.println(first + ' ' + last + " is in " + company + " company."); input = br.readLine(); } } catch (FileNotFoundException ex) { System.out.println("File not found"); } catch (IOException ex) { System.out.println("Unable to read from file"); } } public static void main(String[] args) { InputDemo id = new InputDemo(); id.readFile(); } }
Mid Class public class Mid { private String alpha, last, first, major; private int company; public Mid(String aNum, String l_name, String f_name, int co, String maj) { alpha = aNum; last = l_name; first = f_name; company = co;; major = maj; } public String getAlpha() { return alpha; } public String getLast() { return last; } public String getFirst() { return first; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public int getCompany() { return company; } public void setCompany(int company) { this.company = company; } public String toString() { return first + " " + last + " " + company + " " + major; } }
ArrayList(Core Java 2, pgs 179-184) • Once an array is created, its size is fixed. It can’t be changed. Ex: int size = 10; Circle circleArray[] = new Circle[size]; • The ArrayList class provides a structure that can grow and shrink to meet our storage requirements. It automatically adjusts its capacity as you add and remove elements. • An ArrayList that holds Mid objects is constructed like this: ArrayList<Mid> brigade = new ArrayList<Mid>(); • You can add, remove and access elements of the ArrayList using methods such as add, remove, get and set. See the java API for details. • In the following example we’ll see how to add and retrieve elements.
InputDemo with an ArrayList of Mid objects import java.io.*; import java.util.*; public class InputDemo { ArrayList<Mid> brigade = new ArrayList<Mid>(); public InputDemo() { } public void readFile() { String alpha, first, last, major; int company; try { FileReader inFile = new FileReader("mids.txt"); BufferedReader br = new BufferedReader(inFile); String input; input = br.readLine(); while (input != null) { Scanner parser = new Scanner(input); alpha = parser.next(); last = parser.next(); first = parser.next(); company = parser.nextInt(); major = parser.nextLine(); major = major.trim(); Mid m = new Mid(alpha, last, first, company, major); brigade.add(m); input = br.readLine(); } } catch (FileNotFoundException ex) { System.out.println("File not found"); } catch (IOException ex) { System.out.println("Unable to read from file"); } } public void displayByMajor(String major) { for(Mid m: brigade) { if (m.getMajor().equalsIgnoreCase(major)) System.out.println(m); } } public static void main(String[] args) { InputDemo id = new InputDemo(); id.readFile(); id.displayByMajor("Information Technology"); } }