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 ();
}
}
}
}