Reputation: 21
I am supposed to write/show input of my "result"(calculator) to a log file... for example: results.txt But If I do more then 1 calculation... only the last one gets printed in results.txt.
public class Calculator {
public static void main(String[] args) throws FileNotFoundException {
String filename = "results.txt";
//PrintWriter output = new PrintWriter(filename);
int number1;
int number2;
char mathchoice;
int result;
char userchoice;
do {
PrintWriter output = new PrintWriter(filename);
Scanner choice = new Scanner(System.in);
System.out.println("Enter first number...");
number1 = choice.nextInt();
System.out.println("Math choice...");
mathchoice = choice.next().charAt(0);
System.out.println("Enter second number...");
number2 = choice.nextInt();
if (mathchoice == '+') {
result = number1 + number2;
System.out.println("Result is: " + result);
output.print("Result is: " + result);
} else if (mathchoice == '-') {
result = number1 - number2;
System.out.println("Result is: " + result);
output.print("Result is: " + result);
} else if (mathchoice == '*') {
result = number1 * number2;
output.print("Result is: " + result);
System.out.println("Result is: " + result);
} else if (mathchoice == '/') {
if (number2 == 0) {
throw new java.lang.ArithmeticException("Devided by zero not aloud");
} else {
result = number1 / number2;
output.print("Result is: " + result);
System.out.println("Result is: " + result);
}
} else if (mathchoice == '%') {
result = number1 % number2;
output.print("Result is: " + result);
System.out.println("Result is: " + result);
} else if (mathchoice == '^') {
result = (int) Math.pow(number1, number2);
output.print("Result is: " + result);
System.out.println("Result is: " + result);
} else {
System.out.println("Wrong choice");
}
output.close();
System.out.println("Again?");
userchoice = choice.next().charAt(0);
}while (userchoice == 'j');
}
}
I added the output.close(), at the end of the lus. But I only get the last calculation.
Upvotes: 0
Views: 731
Reputation: 505
If you want to get the exact same output as printed in the console, so System.out + System.in, one way to go about this is by creating a custom InputStreamReader to give to the Scanner, and a custom PrintStream to set System.out as. These custom implementations can then write to the file whenever they read/write data.
Here is an example:
import java.io.*;
import java.nio.file.Paths;
import java.util.Scanner;
public class ScannerTest {
static boolean alive = true;
public static void main(String[] args){
int number1;
int number2;
char mathchoice;
int result;
char userchoice;
final File file = Paths.get("result.txt").toFile();
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
file.createNewFile();
System.setOut(new PrintStreamFileForwarder(System.out, fileOutputStream));
} catch (IOException e) {
e.printStackTrace();
}
Scanner choice = new Scanner(new InputStreamFileForwarder(System.in, fileOutputStream));
do {
System.out.println("Enter first number...");
number1 = choice.nextInt();
System.out.println("Math choice...");
mathchoice = choice.next().charAt(0);
System.out.println("Enter second number...");
number2 = choice.nextInt();
if (mathchoice == '+') {
result = number1 + number2;
System.out.println("Result is: " + result);
} else if (mathchoice == '-') {
result = number1 - number2;
System.out.println("Result is: " + result);
} else if (mathchoice == '*') {
result = number1 * number2;
System.out.println("Result is: " + result);
} else if (mathchoice == '/') {
if (number2 == 0) {
throw new java.lang.ArithmeticException("Devided by zero not aloud");
} else {
result = number1 / number2;
System.out.println("Result is: " + result);
}
} else if (mathchoice == '%') {
result = number1 % number2;
System.out.println("Result is: " + result);
} else if (mathchoice == '^') {
result = (int) Math.pow(number1, number2);
System.out.println("Result is: " + result);
} else {
System.out.println("Wrong choice");
}
System.out.println("Again?");
userchoice = choice.next().charAt(0);
} while (userchoice == 'j');
alive = false;
}
public static class InputStreamFileForwarder extends InputStreamReader {
private final FileOutputStream fileOutputStream;
public InputStreamFileForwarder(InputStream console, FileOutputStream fileOutputStream) {
super(console);
this.fileOutputStream = fileOutputStream;
}
@Override
public int read(char[] cbuf, int offset, int length) throws IOException {
int read = super.read(cbuf, offset, length);
if(read > 0) {
char[] allRead = new char[read];
System.arraycopy(cbuf, offset, allRead, 0, read);
fileOutputStream.write(new String(allRead).getBytes());
}
return read;
}
}
public static class PrintStreamFileForwarder extends PrintStream {
private final FileOutputStream fileOutputStream;
public PrintStreamFileForwarder(PrintStream console, FileOutputStream fileOutputStream) {
super(console);
this.fileOutputStream = fileOutputStream;
}
@Override
public void write(byte[] buf, int off, int len) {
super.write(buf, off, len);
try {
fileOutputStream.write(buf, off, len);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Upvotes: 1
Reputation: 121
You can create a file and write directly to it, see the "java.io" classes, such as OutputStream, FileWriter and etc. You basically need to create a reference to the file and use the "write" method to write to it.
// on Windows use "c:\temp\my-file.log"
FileOutputStream fos = new FileOutputStream("/tmp/my-file.log");
fos.write("My log message".getBytes());
But if possible, do not reinvent the wheel and use LOG4J to record your logs. LOG4J is a widely used library and it makes logging work much easier.
And to manage your project's dependencies, use Maven. Create a maven project and add the dependencies in POM.XML, it will greatly simplify your life.
Upvotes: 1