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(FileDescriptor fd) 

Source Link

Document

Constructs a FileWriter given a file descriptor, using the platform's java.nio.charset.Charset#defaultCharset() default charset .

Usage

From source file:Main.java

public static void saveToFile(String fileName, String str) throws IOException {
    File f = new File(fileName);
    if (!f.exists()) {
        if (f.getParent() != null) {
            new File(f.getParent()).mkdirs();
        }//  w  w  w  .  ja va2  s  .c  o m
        f.createNewFile();
    }
    FileWriter fw = new FileWriter(f);
    fw.write(str);
    fw.close();
}

From source file:Main.java

/**
 * Write data row./*from  ww  w  . jav  a 2 s. c  o m*/
 * 
 * @param data the data
 * @param outputPath the output path
 */
public static void writeDataRow(String data, String outputPath) {
    File file = new File(outputPath);
    try {
        file.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
        System.exit(0);
    }
    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        writer.write(data);
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(0);
    }
}

From source file:Main.java

public static void write(Element root, String xmlPath) {
    try {/*w  w w.j a v a  2  s.co m*/
        Document doc = root.getOwnerDocument();
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("encoding", "utf-8");

        FileWriter fw = new FileWriter(xmlPath);

        transformer.transform(new DOMSource(doc), new StreamResult(fw));

        fw.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Write data column./*from   w w  w. j a v a2s .co  m*/
 * 
 * @param data the data
 * @param outputPath the output path
 */
public static void writeDataColumn(List<? extends Number> data, String outputPath) {
    File file = new File(outputPath);
    try {
        file.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
        System.exit(0);
    }
    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        for (Number value : data) {
            writer.write(value.toString() + "\n");
        }
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(0);
    }
}

From source file:naftoreiclag.villagefive.SaveLoad.java

public static void save(World data) throws IOException {
    JSONObject obj = new JSONObject();
    obj.put("world", data);
    FileWriter fw = new FileWriter(new File("saves/save.json"));
    obj.writeJSONString(fw);//from  w  w w  .  j a  v a2 s.c o  m
    fw.flush();
}

From source file:Main.java

/**
 * Converts a plain text file into TE3-input file
 *
 * @param plainfile//w w  w  .j  av  a 2 s .com
 * @return
 */
public static String Plain2TE3(String plainfile) {
    String outputfile = null;
    try {
        String line;
        boolean textfound = false;
        String header = "";
        String footer = "";
        String text = "";

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

        try {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dctvalue = sdf.format(new Date());

            te3writer.write("<?xml version=\"1.0\" ?>");
            te3writer.write(
                    "\n<TimeML xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://timeml.org/timeMLdocs/TimeML_1.2.1.xsd\">\n");
            te3writer.write("\n<DOCID>" + (new File(plainfile)).getName() + "</DOCID>\n");
            te3writer.write("\n<DCT><TIMEX3 tid=\"t0\" type=\"DATE\" value=\"" + dctvalue
                    + "\" temporalFunction=\"false\" functionInDocument=\"CREATION_TIME\">" + dctvalue
                    + "</TIMEX3></DCT>\n");

            // read out text
            while ((line = inputReader.readLine()) != null) {
                text += line + "\n";
            }

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

        } finally {
            if (inputReader != null) {
                inputReader.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:csv.FileManager.java

static public void writeItems(String fileName, ArrayList<ArrayList<String>> data) {
    try {//  w w  w  .j av  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

public static void writeToFile(final String str, final File file) {
    try {//  w w w.  j a  v  a2s . com
        final BufferedWriter output = new BufferedWriter(new FileWriter(file), 8192);
        output.write(str);
        output.close();
    } catch (final IOException exception) {
        exception.printStackTrace();
    }
}

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  w  w .j av a  2  s.c o  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 void putString(File file, String contents) throws IOException {
    FileWriter output = new FileWriter(file);
    output.write(contents, 0, contents.length());
    output.close();/*from   w  w  w  . j  ava2 s  . c  o m*/
}