Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void writeLog(String log) {
        File file = new File("/sdcard/xh.log");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                //            e.printStackTrace();
            }
        }
        BufferedWriter out = null;
        try {
            // Create file 
            FileWriter fstream = new FileWriter("/sdcard/xh.log", true);
            out = new BufferedWriter(fstream);
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss   ");
            String time = sdf.format(new Date(System.currentTimeMillis()));
            out.append(time);
            out.append(log);
            out.append("\r\n\r\n");
        } catch (Exception e) {//Catch exception if any
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
            }
        }
    }
}