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 writeLifeLogCountInFile(int walking, int running, int vehicle, int bicycle, String filename)
        throws IOException {
    Date date = new Date();
    BufferedWriter writer = null;
    File dir = new File(Environment.getExternalStorageDirectory() + "/LifeLog");
    Log.d("Directory PATH", dir.getAbsolutePath());
    boolean flag = dir.mkdir();
    Log.d("Directory created?", "" + flag);
    File file = new File(dir.getAbsolutePath(), filename);
    if (file.exists() == false) {
        file.createNewFile();//from   www . j  a va 2s  . c  om
        writer = new BufferedWriter(new FileWriter(file, true));
        writer.write("Date,Walking,Running,Bicycle,Vehicle");
        writer.newLine();
        writer.write(date.toString() + "," + walking + "," + running + "," + bicycle + "," + vehicle);
        writer.newLine();
    } else {
        writer = new BufferedWriter(new FileWriter(file, true));
        writer.write(date.toString() + "," + walking + "," + running + "," + bicycle + "," + vehicle);
        writer.newLine();
        Log.d("Appended", "True");
    }
    writer.flush();
    writer.close();

}

From source file:at.ac.tuwien.dsg.cloudlyra.utils.IOUtils.java

public static void writeData(String data, String fileName) {

    String tomcatTempFolder = System.getProperty("java.io.tmpdir");
    //String tomcatTempFolder="/Volumes/DATA/BigData";
    fileName = tomcatTempFolder + "/" + fileName;
    FileWriter fstream;// w ww  . j a va 2s.  c  o  m
    try {
        fstream = new FileWriter(fileName, false);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(data);

        out.close();
    } catch (IOException ex) {
    }
}

From source file:component.Configuration.java

public static void createConfigFile() throws IOException {
    JSONObject config = new JSONObject();
    List<String> queryList = new ArrayList<>();
    Map<String, List<String>> queries = new HashMap<>();
    try (Writer writer = new FileWriter("config.json", false)) {
        queryList.add("full_message:(Exception)");
        queryList.add("full_message:(Error)");
        config.put("queries", queryList);
        config.put("range", 3600);

        config.writeJSONString(writer);/*  w w w.  ja v a2  s.com*/
        writer.flush();
    }
}

From source file:Main.java

public static boolean appendFile(String strThrift, String filePath) {
    PrintWriter out = null;//from w ww  .j  a  v  a2 s  .  co  m
    try {
        out = new PrintWriter(new BufferedWriter(new FileWriter(filePath, true)));
        out.println(strThrift);
        out.flush();
        out.close();
    } catch (IOException e) {
        return false;
    }
    return true;

}

From source file:Main.java

public static void writeInteractionInFile(Context context, String startTime, String endTime, long duration,
        String filename) throws IOException, Exception {

    BufferedWriter writer = null;
    //String path="sdcard/LifeTracker/lifetracker.csv";
    File dir = new File("sdcard/SleepLog");
    boolean flag = dir.mkdir();
    //Log.d("Directory created?",""+flag);
    File file = new File(dir.getAbsolutePath(), filename);
    if (file.exists() == false) {
        //                  Intent service = new Intent(context,DataBaseService.class);
        //                  context.startService(service);
        file.createNewFile();//from  w  w w. j  av a2 s  .  c o m
        writer = new BufferedWriter(new FileWriter(file, true));
        writer.write("Start Time,End Time,Duration");
        writer.newLine();
        writer.write(startTime + "," + endTime + "," + duration);
    } else {
        writer = new BufferedWriter(new FileWriter(file, true));
        writer.newLine();
        writer.write(startTime + "," + endTime + "," + duration);
    }
    Log.d("Appended", "True");
    writer.flush();
    writer.close();

}

From source file:Main.java

public static void writeToFile(String s, String filePath, boolean append) {
    File f = new File(filePath);
    Writer writer = null;//from   www.  jav a2  s .  co  m
    try {
        writer = new BufferedWriter(new FileWriter(f, append));
        writer.write(s);
        writer.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (writer != null) {
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void appendLog(String text) {
    Log.d("LOGFILE", text);
    SimpleDateFormat sTime = new SimpleDateFormat("dd/MMM/yyyy - hh:mm:ss");
    String timeFormat = sTime.format(new Date());

    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/Cura/Logs/");
    dir.mkdirs();/*  ww  w.  java 2 s  .c  om*/
    File logFile = new File(dir, "Cura_Logs_DEBUG.txt");
    if (!logFile.exists()) {
        try {
            logFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
        if (text.compareTo("wipe") == 0)
            logFile.delete();
        else {
            buf.append("[" + timeFormat + "] - ");
            buf.append(text);
            buf.newLine();
            buf.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean writeFile(String filename, String content) {
    boolean isSuccess = false;
    BufferedWriter bufferedWriter = null;
    try {/*from  www . j av  a2 s.c  o  m*/
        bufferedWriter = new BufferedWriter(new FileWriter(filename, false));
        bufferedWriter.write(content);
        isSuccess = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeIO(bufferedWriter);
    }
    return isSuccess;
}

From source file:Main.java

public static boolean writeFile(String filename, String content, boolean append) {
    boolean isSuccess = false;
    BufferedWriter bufferedWriter = null;
    try {/*  w  ww . j  av  a  2 s  .co  m*/
        bufferedWriter = new BufferedWriter(new FileWriter(filename, append));
        bufferedWriter.write(content);
        isSuccess = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeIO(bufferedWriter);
    }
    return isSuccess;
}

From source file:Main.java

public static void write(String in, File file, boolean append) {
    if (file.exists()) {
        file.delete();//from  w ww  .  jav  a 2 s  .c om
    }
    try {
        file.createNewFile();
        FileWriter fw = new FileWriter(file, append);
        fw.write(in);
        fw.flush();
        fw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}