DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Assignment 3
Assignment 3
public class AppendIt { public static void main (String [] args) throws IOException{ // myOutput collects all of the chars from the input stream // fileName holds the file name StringBuffer myOutput = new StringBuffer(); String fileName = StringUtils.join(args, ' ') + ".dat"; //Prompt the user to enter some text. System.out.print ("Enter some text:\n"); // added a newline after this - nicer // initalize the ch variable with the first character from the input stream char ch = (char)System.in.read(); // now our do loop can just append characters so long as they are not lone-periods // this logic also takes care of ensuring that our final character added to myOutput while(ch != '.'){ myOutput.append(ch); ch = (char)System.in.read(); } try { BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true)); out.write(myOutput.toString()); out.close(); } catch (IOException e) { // do something with the exception, like a nice error message System.err.println("Couldn't write to file: " + e.toString()); } } }