



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Information about java input/output (i/o) for cs230 students at wellesley college. It includes problem set details, lecture notes, and code examples for reading and writing files using java. Students will learn how to read and display file contents, create files from strings, and copy files. The document also covers the basics of input streams and reading lines from different sources.
Typology: Assignments
1 / 7
This page cannot be seen from the preview
Don't miss anything!




04 - 1
Problem Set: Assignment #1 due Tuesday, Feburary 13 Wellesley College CS Lecture 04 Thursday, February 8 Handout # 04 - 2
04 - 3
[fturbak@puma utils] cat ../text/cat-in-hat-4.txt The sun did not shine. It was too wet to play. So we sat in the house All that cold, cold, wet day. [fturbak@puma utils] wc ../text/cat-in-hat-4.txt 4 23 100 ../text/cat-in-hat-4.txt [fturbak@puma utils] wc ../text/cat-in-hat.txt 349 1620 7440 ../text/cat-in-hat.txt [fturbak@puma utils] cp ../text/cat-in-hat.txt copycat [fturbak@puma utils] wc copycat 349 1620 7440 copycat 04 - 4
import java.io.; // Import classes from the Java I/O package import java.net.; // Import classes from the Java web package public class FileOps { // We will write all of our code in this class, which can // be found in your ~/cs230/utils/FileOps.java file. }
04 - 7
/** This version of displayFile() avoids reading entire file into a String object in Java memory. But it’s way more complex! The simpler version is fine for many applications. */ public static void displayFile (String infile) { try { BufferedReader reader = new BufferedReader(new FileReader(infile)); String line = reader.readLine(); // Read the first line of the file. while (line != null) { // Line becomes null at end of file System.out.println(line); line = reader.readLine(); // Read the next line of the file } reader.close(); } catch (IOException ex) { System.out.println(ex); } } 04 - 8
/** Writes the contents string to the file named by outfile. Displays an errors message if outfile cannot be created. */ public static void stringToFile (String outfile, String contents) { try { BufferedWriter writer = new BufferedWriter(new FileWriter(outfile)); // create a new file writer writer.write(contents); // write contents string to file writer.close(); // close file writer } catch (IOException ex) { System.out.println(ex); // Handle file-not-found } }
04 - 9
/** A simple version of a method that copies infile to outfile */ public static void copyFile (String infile, String outfile) { stringToFile(outfile, fileToString(infile)); } [fturbak@puma utils] java FileOps copyFile ../text/cat-in-hat- 4.txt copycat- [fturbak@puma utils] java FileOps displayFile copycat- The sun did not shine. It was too wet to play. So we sat in the house All that cold, cold, wet day. We could avoid the large intermediate string of copyFile() by expanding fileToString() and writing one line at a time. 04 - 10
/** A simple version of a Linx-like word count (wc) method */ public static void wordCount (String infile) { String contents = fileToString(infile); int chars = contents.length(); // number of chars in file int lines = contents.split("\n").length; // number of lines in file int words = contents.split("\s+").length; // contents.split("\s+") splits contents around every // substring of one or more whitespace chars. System.out.println(lines + "\t" + words + "\t" + chars + "\t" + infile); } [fturbak@puma utils] java FileOps wordCount ../text/cat-in-hat.txt 349 1620 7440 ../text/cat-in-hat.txt [fturbak@puma utils] wc ../text/cat-in-hat.txt 349 1620 7440 ../text/cat-in-hat.txt
04 - 13
public static String readLineFromInputStreamReader (InputStreamReader isReader) { try { BufferedReader reader = new BufferedReader(isReader); return reader.readLine(); } catch (IOException ex) { System.out.println(ex); return ""; } } 04 - 14
public static String readLineFromFile (String infile) throws IOException { InputStreamReader fr = new FileReader(infile); return readLineFromInputStreamReader(fr); } public static String readLineFromURL (String urlName) throws IOException { InputStreamReader ir = new InputStreamReader(new URL(urlName).openStream()); return readLineFromInputStreamReader(ir); } public static String readLineFromKeyboard (String prompt) throws IOException { System.out.println(prompt); InputStreamReader ir = new InputStreamReader(System.in); return readLineFromInputStreamReader(ir); }