80 likes | 207 Views
Java Review 2. Rem Collier. The Agenda. The following topics were highlighted to me as issues: File IO (Rem) Wrappers (Rem) Interfaces (Rem) Asymptotic Notation (Elani). File Input / Output. File I/O is stream based!
E N D
Java Review 2 Rem Collier
The Agenda • The following topics were highlighted to me as issues: • File IO (Rem) • Wrappers (Rem) • Interfaces (Rem) • Asymptotic Notation (Elani)
File Input / Output • File I/O is stream based! • To read data from a file, we must create an instance of the FileInputStream class. • To write data to a file, we must create an instance of the FileOutputStream class. • In Java, specific files are represented as instances of the File class. • E.g.File myFile = new File(“c:\\myfile.txt”);here myFile references a File object that represents the file myfile.txt on my c drive. • The File class is part of the java.io package. • i.e we must put the following at the top of our code:import java.io.File;
Reading from a file • To read from a file: • Create an instance of the File class that references the file.i.e. File file = new File(“c:\\example.txt”); • Create a FileInputStream for that file.i.e. FileInputStream fin = new FileInputStream(file); • Create the InputStreamReader and BufferedReader classes.i.e. InputStreamReader isr = new InputStreamReader(fin); BufferedReader in = new BufferedReader(isr); • Read from the file.i.e.String line = in.readLine();
Example – Reading a User Specified File import java.io.*; class MyProgram { publicstaticvoid main(String[] args) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String line = null; System.out.println("Input a filename:" ); try { line = in.readLine(); } catch (IOException ie) { System.out.println("Exception caught: " + ie); } File f = new File(line); try { FileInputStream fin = new FileInputStream(f); in = new BufferedReader(new InputStreamReader(fin)); while ((line = in.readLine()) != null) { System.out.println(line); } } catch (FileNotFoundException fnfe) { System.out.println("File " + f.getAbsolutePath() + " not found"); } catch (IOException ie) { System.out.println("IO Exception caught: " + ie); } } }
Writing to a File • To write data to a file: • Create an instance of the File class that references the file.i.e. File file = new File(“c:\\example.txt”); • Create a FileOutputStream for that file.i.e. FileOutputStream fout = new FileOutputStream(file); • Create the PrintWriter class.i.e. PrintWriter out = new PrintWriter(fout); • Write data to the file.i.e. out.println(“data”);
Writing Data to a File import java.io.*; class MyProgram { publicstatic String readUserInput() { String line = null; BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); try { line = in.readLine(); } catch (IOException ie) { System.out.println("Exception caught: " + ie); } return line; }
Writing Data to a File publicstaticvoid main(String[] args) { System.out.println("Input a filename:" ); String line = readUserInput(); File f = new File(line); try { FileOutputStream fout = new FileOutputStream(f); PrintWriter out = new PrintWriter(fout); System.out.println("Enter the data:"); line = readUserInput(); while (!line.equals("EOF")) { out.println(line); line = readUserInput(); } out.close(); } catch (FileNotFoundException fnfe) { System.out.println("File " + f.getAbsolutePath() + " not found"); } catch (IOException ie) { System.out.println("IO Exception caught: " + ie); } } }