Java 如何在日志文件中显示/写入结果?
我应该将“结果”(计算器)的输入写入/显示到日志文件中。。。例如:results.txt 但如果我做的不止一个计算。。。在results.txt中只打印最后一个Java 如何在日志文件中显示/写入结果?,java,logfile,Java,Logfile,我应该将“结果”(计算器)的输入写入/显示到日志文件中。。。例如:results.txt 但如果我做的不止一个计算。。。在results.txt中只打印最后一个 public class Calculator { public static void main(String[] args) throws FileNotFoundException { String filename = "results.txt"; //PrintWri
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');
}
}
我在lus的末尾添加了output.close()。但是我只得到最后一次计算。您可以创建一个文件并直接写入,请参阅“java.io”类,如OutputStream、FileWriter等。 您基本上需要创建对文件的引用,并使用“write”方法对其进行写入
// on Windows use "c:\temp\my-file.log"
FileOutputStream fos = new FileOutputStream("/tmp/my-file.log");
fos.write("My log message".getBytes());
但是如果可能的话,不要重新发明轮子并使用LOG4J来记录日志。
LOG4J是一个广泛使用的库,它使日志记录工作更加容易
要管理项目的依赖关系,请使用Maven。
创建一个maven项目并在POM.XML中添加依赖项,这将大大简化您的工作。如果您希望获得与控制台中打印的完全相同的输出,那么System.out+System.in,一种方法是创建一个自定义InputStreamReader以提供给扫描仪,以及一个自定义PrintStream以将System.out设置为。这些自定义实现可以在读取/写入数据时写入文件 以下是一个例子:
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();
}
}
}
}
斯坦,有更简单的方法吗?@PedroCosta我不知道有更简单的方法。代码很简单,对吗?U基本上是围绕输入流和打印流包装一个类。我查看了Scanner类的API,没有看到任何现成的解决方案:(