如何 show/write 导致日志文件?
How to show/write result in log file?
我应该 write/show 我的“结果”(计算器)输入到日志文件...例如:results.txt
但是,如果我做的计算多于 1 次……只有最后一个会打印在 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');
}
}
我在 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 以提供给扫描仪,以及将 System.out 设置为的自定义 PrintStream。这些自定义实现可以在 read/write 数据时写入文件。
这是一个例子:
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();
}
}
}
}
我应该 write/show 我的“结果”(计算器)输入到日志文件...例如:results.txt 但是,如果我做的计算多于 1 次……只有最后一个会打印在 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');
}
}
我在 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 以提供给扫描仪,以及将 System.out 设置为的自定义 PrintStream。这些自定义实现可以在 read/write 数据时写入文件。
这是一个例子:
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();
}
}
}
}