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();
}
} |