Print the Stack Trace of the Exception to log file

In Java, when an Exception occurres , the following code snippent can be used to print it in a log file


import java.io.PrintWriter;
import java.io.StringWriter;
    public static String logStackTrace(Throwable t)
    {
         StringWriter sw = new StringWriter();
         PrintWriter pw = new PrintWriter(sw, true);
         exp.printStackTrace(pw);
         pw.flush();
         sw.flush();
         nLog("exception  "+ sw.toString());
    }

    public void nLog(String msg) {
    try {
        BufferedWriter out = new
            BufferedWriter(new FileWriter("/home/temp/ex.log",true));
        out.write(msg+"n");
        out.close();
        } catch (IOException e) {
           e.printStackTrace();
        }

Quick read files in Java (Scanner)

According to me reading files in JAVA is bit more complex as compared to C or C++

I came across Java Scanner API (JDK 1.5 onwards ) which easier than than common ways how we read fie using java line by line
Here is the sample code

private static String quickReadFile(String inFileName) {
String contents = null;
try {
Scanner scanner = new Scanner(new File(inFileName)).useDelimiter(“Z”);
contents = scanner.next();
//System.out.println(contents);
scanner.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(ParseTagged.class.getName()).log(Level.SEVERE, null, ex);
}
return contents;
}