How to print exception Printstacktrace () to a file

Source: Internet
Author: User
Tags getmessage

Example 1:

catch (Exception e)
{
Log (E.getmessage ());
}
public void log (String s)
{
Randomaccessfile RAF = new Randomaccessfile ("Log.txt", "RW");
Raf.seek (Raf.length ());
Raf.writebytes (s);
Raf.close ();
}
Is that all right?

Example 2:

private static void WriteToFile (Exception ex)
{
String strlogfilename = "D://abc//your.log";
FileWriter fwfilewriter = null;
FileOutputStream filout = null;
try {
FileWriter thefile = new FileWriter (Strlogfilename, true);
PrintWriter out = new PrintWriter (thefile);
try {
if (! ( ex = null)) {
Ex.printstacktrace (out);
Ut.println ();
}
Out.close ();
Thefile.close ();
}
catch (Exception e) {
E.printstacktrace ();
System.out.println (E.getmessage ());
}
finally {
if (out! = null) {
Out.close ();
}
if (thefile! = null) {
Thefile.close ();
}
}
}
catch (FileNotFoundException e) {
E.printstacktrace ();
System.out.println (E.getmessage ());
}
catch (Exception e) {
E.printstacktrace ();
System.out.println (E.getmessage ());
}
}

Example 3:

public void Printstacktrace (PrintStream s)
public void Printstacktrace (PrintWriter s)

My example:
Execution: Java

Import java.net.*;
Import java.io.*;

public class Simpleurlcheck {

public static void Main (String args[]) {
if (Args.length = = 0) {
System.err.println
("Provide a URL to check");
} else {
PrintWriter pw = null;
String urlstring = args[0];
try {
PW = new PrintWriter (New FileOutputStream ("OUT.txt"));
URL url = new URL (urlstring);
URLConnection connection =
Url.openconnection ();
if (connection instanceof HttpURLConnection) {
HttpURLConnection httpconnection =
(httpurlconnection) connection;
Httpconnection.connect ();
int response =
Httpconnection.getresponsecode ();
System.out.println (
"Response:" + Response);
InputStream is =
Httpconnection.getinputstream ();
byte[] buffer = new byte [256];
while (is.read (buffer)! =-1) {}
Is.close ();
}
} catch (IOException e) {
E.printstacktrace (PW);
Pw.flush ();
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.