// Streams Lab. Solution for reading ASCII file package sample; import java.io.*; /** * The purpose of this class is to introduce two static methods * capable to read and write a text (ASCII) file * @@author Jeff Zhuk */ public class StreamsLab { /** * The readFile() method reads a file into a string * Example of usage: String info = StreamsLab.readFile("c:/uop/test.txt"); * @param iFilename * @return text or null if failure */ public static String readFile(String iFilename) { try { BufferedReader in = new BufferedReader(new FileReader(iFilename)); String text = ""; // start reading with empty string String line = null; // init line to read while((line = in.readLine()) != null) { // end of file text += line + "\r\n"; // windows line ending // for UNIX it would be just "\n" } return text; } catch (IOException e) { System.err.println("readFile failure: " + e); return null; } } /** * The writeFile() method writes a String of text into a file * Example of usage: boolean success = StreamsLab.writeFile("c:/uop/test.txt", "200000\r\n5.35\r\n30"); * @param iFilename * @param iString to write into a file * @return true if success, false if failure * @@author Jeff Zhuk */ public static boolean writeFile( String iFilename, String iString) { try { PrintWriter out = new PrintWriter( new BufferedWriter(new FileWriter(iFilename))); out.print(iString); out.flush(); out.close(); return true; // success ! } catch (IOException e) { System.err.println("writeFile failure: " + e); return false; // failure } } /** * The copyFile() method copies a file into a different location/name while using readFile() and writeFile() methods * Example of usage: boolean success = StreamsLab.copyFile("c:/uop/test.txt", "c:/uop/testCopy.txt"); * @param fname original filename * @param anotherName the target filename * @return true if success, false if failure * @@author Jeff Zhuk */ public static void copy (String fname, String anotherName) { String data = StreamsLab.readFile (fname); System.out.println("Successful Reading"); StreamsLab.writeFile (anotherName, data); System.out.println("Successful Writing"); } /** * The main() method will test the readFile and writeFile methods and * gives an example of parsing the text with the String.split() and Integer.parseInt() methods */ public static void main(String[] args) { boolean success = StreamsLab.writeFile("c:/uop/priceRateYears.txt", "200000\r\n5.35\r\n30"); System.out.println("Success of writing file is " + success); String info = StreamsLab.readFile("c:/uop/priceRateYears.txt"); System.out.println(info); // Parse the string to extract the price, rate and terms (years) String[] splits = info.split("\r\n"); String priceString = splits[0]; String rateString = splits[1]; String termString = splits[2]; int price = Integer.parseInt(priceString); float rate = Float.parseFloat(rateString); int term = Integer.parseInt(termString); System.out.println("Price="+price + " rate="+rate + " term="+ term + " years"); } // end of the main() method } // end of StreamsLab class Press the Browser's BACK button to return to In/Out Java classes...