Example usage for java.io FileWriter FileWriter

List of usage examples for java.io FileWriter FileWriter

Introduction

In this page you can find the example usage for java.io FileWriter FileWriter.

Prototype

public FileWriter(File file, Charset charset) throws IOException 

Source Link

Document

Constructs a FileWriter given the File to write and java.nio.charset.Charset charset .

Usage

From source file:Main.java

public static void writeCSV(String phoneNo, String Message) {
    File root = Environment.getExternalStorageDirectory();
    File csvFile = new File(root, "/adspammer/log.csv");
    FileWriter writer = null;/*w ww. ja  v a2s .  c o  m*/
    try {
        writer = new FileWriter(csvFile, true);

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd @ HH:mm:ss");
        Date date = new Date();
        System.out.println(dateFormat.format(date));

        writer.append(dateFormat.format(date).toString());
        writer.append(',');
        writer.append(phoneNo);
        writer.append(',');
        writer.append(Message);
        writer.append('\n');

        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * write a string To a File/*from  w  ww  .ja v  a 2s. c  o  m*/
 * 
 * @param context
 * @param file
 * @param string
 * @param isAppend
 * @return
 */
public static boolean writeStringToFile(File file, String string, boolean isAppend) {
    boolean isWriteOk = false;

    if (null == file || null == string) {
        return isWriteOk;
    }

    FileWriter fw = null;
    try {
        fw = new FileWriter(file, isAppend);

        fw.write(string, 0, string.length());
        fw.flush();
        isWriteOk = true;
    } catch (Exception e) {
        isWriteOk = false;
        e.printStackTrace();
    } finally {
        if (fw != null) {
            try {
                fw.close();
            } catch (IOException e) {
                isWriteOk = false;
                e.printStackTrace();
            }
        }
    }
    return isWriteOk;
}

From source file:Main.java

/**
 * Write in a text file/*  w w w  .j a va 2s  .  co m*/
 * 
 * @param file
 *            in which we write
 * 
 * @param text
 *            that we write
 * 
 * @param append
 *            indicates whether or not to append to an existing file
 * 
 * @throws IOException
 *             Input/Output exceptions
 */
public static void writeTextFile(File file, String text, boolean append) throws IOException {
    BufferedWriter writer = new BufferedWriter(new FileWriter(file, append));
    writer.write(text);
    writer.close();
}

From source file:Main.java

public static void writeLog(String log) {
    File file = new File("/sdcard/xh.log");
    if (!file.exists()) {
        try {/*w w  w . j a v  a2  s  .  c  om*/
            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) {
        }
    }
}

From source file:Main.java

public static void saveBoardToExternal(Context context, String fileName, JSONObject jsonObject) {
    File file = new File(getDirectoryBoards(), fileName);
    if (file.exists()) {
        file.delete();/*from   w w  w .jav a 2  s .com*/
    }

    try {
        file.createNewFile();
        BufferedWriter buf = new BufferedWriter(new FileWriter(file, true));
        buf.append(jsonObject.toString());
        buf.close();
        addTomMediaScanner(context, file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:edu.usc.qspr.Main.java

/**
 * The main method.//from  w w  w  .  j  a v a2  s . com
 *
 * @param args the arguments
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    double result;
    long start = System.currentTimeMillis();
    //TODO: to be commented
    //      args = new String("-i sample_inputs/5-1-3.qasm -f sample_inputs/fabric.ql -o output -p baseline -s 21 -v").split(" ");
    //      args = new String("-i ../sample_inputs/5-1-3.qasm -f ../sample_inputs/fabric.ql -p mvfb -s 2 -v").split(" ");

    parseInputs(args);

    if (RuntimeConfig.OUTPUT_TO_FILE) {
        outputFile = new PrintWriter(new BufferedWriter(new FileWriter(outputFileAddr, false)), true);
    } else { //writing to stdout
        outputFile = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), true);
    }

    //Prints the current working directory
    if (RuntimeConfig.DEBUG)
        System.out.println("Current directory: " + System.getProperty("user.dir"));

    layout = LayoutParser.LayoutParser(fabricFileAddr);

    qasm = QASMParser.QASMParser(qasmFileAddr);

    eds = new EventDrivenSimulator(layout, qasm.getCommandsList(), outputFile);

    if (placementMethod.compareTo("center") == 0) {
        result = center();
    } else if (placementMethod.compareTo("baseline") == 0) {
        result = baseLine();
    } else if (placementMethod.compareTo("mc") == 0) {
        result = mc(m, false);
    } else { //default is mvfb
        result = mvfb(m);
    }
    outputFile.println("------------------------------------");
    outputFile.println("Execution latency: " + result + " us");
    long end = System.currentTimeMillis();
    outputFile.println("QSPR runtime " + (end - start) + " ms");

    if (RuntimeConfig.OUTPUT_TO_FILE) {
        outputFile.close();
    } else {
        outputFile.flush();
    }

    if (RuntimeConfig.VERBOSE) {
        System.out.println("Done.");
    }
}

From source file:Main.java

public static boolean writeFile(String filePath, String fileName, String content, boolean append) {
    FileWriter fileWriter = null;
    boolean result = false;
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
        try {//w w  w  .j a v  a 2s  .  co m

            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            Log.i("file", filePath);
            fileWriter = new FileWriter(filePath + fileName, append);
            fileWriter.write(content);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("file", e.toString());
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
    return result;
}

From source file:Main.java

public static void createNewResultFileForConnexion() {
    Date currentDate = new Date();
    SimpleDateFormat localDateFormat = new SimpleDateFormat("yyyyMMdd");
    String logPath = LOG_FOLDER + "result_" + localDateFormat.format(currentDate) + "_"
            + System.currentTimeMillis() + ".txt";

    try {/*from w  ww  .  j ava2s.c o  m*/
        File logFile = new File(logPath);
        FileWriter writer = new FileWriter(logFile, Boolean.TRUE);
        writer.write("Command\tCommand(hex)\tResult\tResult(Hex)\tLatency\n");
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean writeErrorLogToSDCard(String path, String errorLog) {
    if (!(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))) // check if SD card is mounted
        return false;

    File file;//from   w  ww.  ja v  a  2s  . c om
    file = new File(path);
    if (!file.exists()) {
        try {
            file.getParentFile().mkdirs();
            file.createNewFile();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return false;
        }
    }

    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); //true means append to file
        writer.append(errorLog);
        writer.newLine();
        writer.append("-------------------------------"); // seperator
        writer.newLine();
        writer.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

/**
 * write file/* w w  w  . j av a  2  s .  c o m*/
 *
 * @param filePath
 * @param content
 * @param append
 *            is append, if true, write to the end of file, else clear content of file and write into it
 * @return return true
 * @throws IOException
 *             if an error occurs while operator FileWriter
 */
public static boolean writeFile(String filePath, String content, boolean append) {
    FileWriter fileWriter = null;
    try {
        fileWriter = new FileWriter(filePath, append);
        fileWriter.write(content);
        fileWriter.close();
        return true;
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (fileWriter != null) {
            try {
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
}