Example usage for java.io BufferedWriter BufferedWriter

List of usage examples for java.io BufferedWriter BufferedWriter

Introduction

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

Prototype

public BufferedWriter(Writer out) 

Source Link

Document

Creates a buffered character-output stream that uses a default-sized output buffer.

Usage

From source file:csv.FileManager.java

static public void writeItems(String fileName, ArrayList<ArrayList<String>> data) {
    try {/*from  w w  w.  j  a v a2  s .  c  o  m*/
        BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
        CSVWriter writer = new CSVWriter(out);
        String[] t = new String[0];
        for (ArrayList<String> row : data) {
            boolean found = false;
            for (String str : row) {
                if (!str.isEmpty()) {
                    found = true;
                }
            }
            if (found)
                writer.writeNext(row.toArray(t));
        }
        out.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:Main.java

/**
 * Converts a TimeML 1.2 file into a non-tagged TE3 TimeML input
 * get TE3-input, TE3input from tml//from   w ww  .jav  a 2 s .co m
 *
 * @param tmlfile
 * @return
 */
public static String TML2TE3(String tmlfile) {
    String outputfile = null;
    try {
        String line;
        boolean textfound = false;
        String header = "";
        String footer = "";
        String text = "";

        //process header (and dct)/text/footer
        outputfile = tmlfile + ".TE3input";
        BufferedWriter te3writer = new BufferedWriter(new FileWriter(new File(outputfile)));
        BufferedReader TE3inputReader = new BufferedReader(new FileReader(new File(tmlfile)));

        try {

            // read out header
            while ((line = TE3inputReader.readLine()) != null) {
                if (line.length() > 0) {
                    // break on TEXT
                    if (line.matches(".*<TEXT>.*")) {
                        textfound = true;
                        break;
                    }
                }
                header += line + "\n";
            }

            if (!textfound) {
                throw new Exception("Premature end of file (" + tmlfile + ")");
            }

            // read out text
            while ((line = TE3inputReader.readLine()) != null) {
                if (line.length() > 0) {
                    // break on TEXT
                    if (line.matches(".*</TEXT>.*")) {
                        textfound = false;
                        break;
                    }
                }
                text += line.replaceAll("<[^>]*>", "") + "\n";
            }

            if (textfound) {
                throw new Exception("Premature end of file (" + tmlfile + ")");
            }

            // read out footer
            while ((line = TE3inputReader.readLine()) != null) {
                line = line.replaceAll("<(!--|[TSA]LINK|MAKEINSTANCE)[^>]*>", "").trim();
                if (line.length() > 0) {
                    footer += line + "\n";
                }
            }

            te3writer.write(header + "\n");
            te3writer.write("\n<TEXT>\n" + text + "</TEXT>\n");
            te3writer.write(footer + "\n");

            System.err.println("Processing file: " + tmlfile);

        } finally {
            if (TE3inputReader != null) {
                TE3inputReader.close();
            }
            if (te3writer != null) {
                te3writer.close();
            }
        }
    } catch (Exception e) {
        System.err.println("Errors found (TML_file_utils):\n\t" + e.toString() + "\n");
        if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) {
            e.printStackTrace(System.err);
        }
        return null;
    }
    return outputfile;
}

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 w  w  .  j a  v  a 2  s.  c o  m*/
    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

public static BufferedWriter toBufferedWriter(Writer writer) {
    return writer instanceof BufferedWriter ? (BufferedWriter) writer : new BufferedWriter(writer);
}

From source file:Main.java

public static void writeUserInfo(String[] info, Context c) throws IOException {
    File f = c.getFileStreamPath(userInfoPath);
    if (!f.exists()) {
        f.createNewFile();//from  w ww . java2  s.  c  o m
    }

    String output = "";
    for (int n = 0; n < info.length; n++) {
        output += info[n] + eol + separator + eol;
    }
    BufferedWriter out = null;
    out = new BufferedWriter(new OutputStreamWriter(c.openFileOutput(userInfoPath, Context.MODE_PRIVATE)));

    out.write(output);
    out.flush();
    out.close();
    userInfo = info;
}

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   w w w.j a  va2s  .  c o  m
        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:Main.java

public static void writeTextFile(String[] text, String filename) throws IOException {
    BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
    for (int i = 0; i < text.length; i++) {
        writer.write(text[i]);/* w w  w.  j a  v a2 s.co  m*/
        writer.newLine();
    }
    writer.close();
}

From source file:Main.java

public static boolean appendFile(String strThrift, String filePath) {
    PrintWriter out = null;//from w w  w. ja va 2 s  .c o  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 .  jav a 2  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   w  w w.  j av a 2 s  . c  om
    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();
        }
    }
}