Over a million developers have joined DZone.

Assignment 3

DZone's Guide to

Assignment 3

Free Resource
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 != '.'){
			ch = (char)System.in.read();
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true));
		} catch (IOException e) {
			// do something with the exception, like a nice error message
			System.err.println("Couldn't write to file: " + e.toString());

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}