如何在java中打印txt文件中的异常
这是我的代码,我想打印E:\logfile.txt中出现的Evre异常。请帮助我,我无法执行此操作。使用printStackTrace重载,该重载包含一个参数: 或者,使用日志框架,例如。使用printStackTrace重载,该重载接受一个参数:如何在java中打印txt文件中的异常,java,Java,这是我的代码,我想打印E:\logfile.txt中出现的Evre异常。请帮助我,我无法执行此操作。使用printStackTrace重载,该重载包含一个参数: 或者,使用日志框架,例如。使用printStackTrace重载,该重载接受一个参数: 或者,使用日志框架,例如。您可以为此编写一个类的静态方法 public class myclass { // Main method public static void main (String args[]) {
或者,使用日志框架,例如。您可以为此编写一个类的静态方法
public class myclass
{
// Main method
public static void main (String args[])
{
// Stream to write file
try {
int a=100;
int b=a/0;
System.out.println(b);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
并记录消息
public class Logbook {
// path to logbook
private String path = "E:\logfile.txt";
// static method
public static boolean Log(String message) {
try {
File f = new File(path);
// append the text file
FileWriter fileWriter = new FileWriter(file,true);
// append text to file by using a buffer
BufferedWriter bw = new BufferedWriter(fileWriter);
fileWriter.append(message);
// close buffer
bufferFileWriter.close();
// tell that it has succeed
return true;
}
catch (Exception e) {
return false;
}
}
您可以为此编写一个类的静态方法
public class myclass
{
// Main method
public static void main (String args[])
{
// Stream to write file
try {
int a=100;
int b=a/0;
System.out.println(b);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
并记录消息
public class Logbook {
// path to logbook
private String path = "E:\logfile.txt";
// static method
public static boolean Log(String message) {
try {
File f = new File(path);
// append the text file
FileWriter fileWriter = new FileWriter(file,true);
// append text to file by using a buffer
BufferedWriter bw = new BufferedWriter(fileWriter);
fileWriter.append(message);
// close buffer
bufferFileWriter.close();
// tell that it has succeed
return true;
}
catch (Exception e) {
return false;
}
}
您需要替换e.printStackTrace…是否有任何原因不想使用某些日志框架,例如Log4j?我将如何替换。printStackTrace我必须上传到服务器上,任何异常都需要收集到E:\\log中的.txt文件中。txt@AnilKumar这就是日志库所做的,重写日志库可能是浪费时间。您需要替换e.printStackTrace…有什么原因不想使用一些日志记录吗框架,例如Log4j?我将如何替换。printStackTrace我必须上传到服务器上,任何异常都需要收集到E:\\log中的.txt文件中。txt@AnilKumar这就是日志库所做的,重写日志库可能是浪费时间。