Download Java Input/Output (I/O) - CS230 Wellesley College and more Assignments Data Structures and Algorithms in PDF only on Docsity! 1 04 - 1 Java Input/Output (I/O) Problem Set: Assignment #1 due Tuesday, Feburary 13 Wellesley College CS230 Lecture 04 Thursday, February 8 Handout #11 04 - 2 What is Input/Output (I/0)? I/O is any means of receiving information from or transmitting information to user/file system/web. Today we focus on textual information that can be entered/displayed in Linux shell. Later in the semester we will cover Graphical User Interfaces (GUIs). 2 04 - 3 Some examples of File I/O in Linux [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 Our Goal: Similar Operations in Java 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. } 5 04 - 9 Copying Files (like Linux cp) /** 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-4 [fturbak@puma utils] java FileOps displayFile copycat-4 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 wordCount() (like Linux wc) /** 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 6 04 - 11 Testing wordCount() /** 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); } 04 - 12 Input Streams • IO comes in InputStreams – The keyboard: System.in – A file: FileInputStream() – The web: url.openStream() • What you read is unformatted – Format it into chars with an InputStreamReader – For a file you use FileReader (String filename) • What you read has unpredictable length – Restrict it into lines with a BufferReader 7 04 - 13 Reading lines from an Input Stream 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 Reading lines from other Sources 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); }