
-----------------------------------
Azzy
Tue Nov 02, 2004 9:33 am

Read/Write program help
-----------------------------------
I need help with this read write program that i need help with. The writing to a file part is not working on it so please help.

import java.io.*;
import java.text.*;

public class ReadWrite
{
public static void main (String[] args)throws Exception
{
DataInput keyboard = new DataInputStream(System.in);
String temp;
char response;
	
PrintStream out;
BufferedReader in;
String fileName;
String fileContents;
String readOrWrite;
String cont;
		
readWriteExitLoop:
while (true)
{
System.out.println("'Read', 'Write' or 'Exit'");
readOrWrite = keyboard.readLine();
		
if (readOrWrite.equalsIgnoreCase("read"))
{
System.out.println("Please enter the file to read from?");
fileName = keyboard.readLine();
in = new BufferedReader(new FileReader(fileName)); 
fileContents = in.readLine();
System.out.println(fileContents);
System.out.print("\n\nPress any key to continue.");
cont = keyboard.readLine();
}
else if (readOrWrite.equalsIgnoreCase("write"))
{
System.out.println("Please enter the file to write to?");
fileName = keyboard.readLine();
out = System.out;
System.out.println("Please type what you want to be writen");
fileContents = keyboard.readLine();
out.println(fileContents);
System.out.print("\n\nPress any key to continue.");
cont = keyboard.readLine();
}
else if (readOrWrite.equalsIgnoreCase("exit"))
{
break readWriteExitLoop;
}
else
{
System.out.println("Improper input.");
System.out.print("\n\nPress any key to continue.");
cont = keyboard.readLine();
}
readOrWrite = ("");
cont = ("");
System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}

System.out.println("");
System.out.println("");
System.out.print("Hit 'Enter' to end...");
response = (char)System.in.read();
}
}

-----------------------------------
wtd
Tue Nov 02, 2004 2:13 pm


-----------------------------------
Your problem is that you were never opening a new PrintStream pointed at the output file.  You were just setting "out" to "System.out", so "out.println" just went to the console.

Cleaned up:

import java.io.*;
import java.text.*;

public class ReadWrite
{
	private static BufferedReader keyboard = new BufferedReader(
		new InputStreamReader(System.in));

	public static void main (String[] args) throws Exception
	{
		readWriteExitLoop:
		
		while (true)
		{
			System.out.println("'Read', 'Write' or 'Exit'");
			String choice = keyboard.readLine();

			if (readOrWrite.equalsIgnoreCase("read"))
			{
				System.out.println("Please enter the file to read from?");
				String fileName = keyboard.readLine();
				
				BufferedReader inputFile = new BufferedReader(new FileReader(fileName));
				String fileContents = inputFile.readLine();
				
				System.out.println(fileContents);
				
				pause("Press any key to continue.");
			}
			else if (readOrWrite.equalsIgnoreCase("write"))
			{
				System.out.println("Please enter the file to write to?");
				String fileName = keyboard.readLine();
				PrintStream outputFile = new PrintStream(fileName);
				
				System.out.println("Please type what you want to be writen");
				String fileContents = keyboard.readLine();
				outputFile.println(fileContents);
				
				pause("Press any key to continue.");
			}
			else if (readOrWrite.equalsIgnoreCase("exit"))
			{
				break readWriteExitLoop;
			}
			else
			{
				System.out.println("Improper input.");
				pause("Press any key to continue.");
			}

			/* This is so silly.  Attempting to clear the
			 * screen this way should not be done.  You can't 
			 * control the output terminal, so don't try.
			 *    readOrWrite = ("");
			 *    cont = ("");
			 *    System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
			 */
		}

		pause("Hit 'Enter' to end...");
	}
	
	private static void pause(String message)
	{
		System.out.println("\n\n" + message);
		keyboard.readLine();
	}
}
