Over a million developers have joined DZone.

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 != '.'){
			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());

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}