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

/**
 * Formats a file nice looking/*from  w w w .  j  a  v  a 2  s .c om*/
 * @param file The XML file to format
 * @throws Exception If the file isn't an XML file
 */
public static void format(File file) throws Exception {
    if (file.isFile()) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        Document document = null;
        builder = factory.newDocumentBuilder();
        document = builder.parse(file);

        OutputFormat format = new OutputFormat(document);
        format.setLineWidth(65);
        format.setIndenting(true);
        format.setIndent(2);
        Writer out = new FileWriter(file);
        XMLSerializer serializer = new XMLSerializer(out, format);
        serializer.serialize(document);
        out.close();
    }
}

From source file:JGraphLoader.GraphSaver.java

public static void saveG(String filename, Graph g) {

    final AbstractLayout layout = new StaticLayout(g);

    GraphMLWriter<node, edge> graphWriter = new GraphMLWriter<node, edge>();
    try {//from w  w  w .  j  a va2s  .  co  m
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename)));

        graphWriter.addEdgeData("id", null, "0", new Transformer<edge, String>() {
            @Override
            public String transform(edge i) {
                return i.getValue();
            }
        });
        /*
         graphWriter.addVertexData("y", null, "0",
         new Transformer<node, String>() {
         public String transform(node v) {
         return Double.toString(layout.getY(v));
         }
         }
         );*/

        graphWriter.save(g, out);
    } catch (IOException ex) {
        Logger.getLogger(GraphSaver.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:Main.java

public static void WriteAllText(String path, String text) throws IOException {
    BufferedWriter writer = null;
    try {/*from   ww  w.ja v  a 2s .com*/
        FileWriter output = new FileWriter(path);
        writer = new BufferedWriter(output);
        writer.write(text);
    } finally {
        if (writer != null)
            writer.close();
    }
}

From source file:com.lunix.cheata.utils.file.FileManager.java

public static void createFile(String fileName) {
    try {/*ww  w  .j av  a  2 s .com*/
        BufferedWriter writer = new BufferedWriter(new FileWriter(new File(dir.getAbsolutePath(), fileName)));
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:FileUtils.java

public static void makeFile(String Path, String content) {
    try {//w ww.  ja  v a 2  s  .c  o  m
        // Create file
        FileWriter fstream = new FileWriter(Path);
        BufferedWriter bf = new BufferedWriter(fstream);
        bf.write(content);
        // Close the output stream
        bf.close();
    } catch (Exception e) {// Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
}

From source file:net.chunkyhosting.Roe.CHGManagerLauncher.utils.JSON.java

public static void writeJsonToFile(JSONObject json, File file) throws IOException {

    FileWriter writer = new FileWriter(file.toString());

    try {//w  ww.j  ava2s  . c o  m

        writer.write(json.toString(4));

    } finally {

        writer.flush();
        writer.close();

    }

}

From source file:Main.java

public static int removeUnusedLines(String filename, SortedSet<Integer> linesSet) {

    try {/*from w  ww  .j a v a 2 s .c o  m*/
        BufferedReader br = new BufferedReader(new FileReader(filename));
        //String buffer to store contents of the file
        StringBuffer sb = new StringBuffer("");

        int lineNumber = 1;
        String line;
        Iterator iterator = linesSet.iterator();
        int lineNumberToBeDeleted = (int) iterator.next();

        int count = 0;

        while ((line = br.readLine()) != null) {
            if (lineNumber == lineNumberToBeDeleted) {
                if (iterator.hasNext()) {
                    lineNumberToBeDeleted = (int) iterator.next();
                    count++;
                }
            }

            else {
                sb.append(line + "\n");
            }

            lineNumber++;
        }

        FileWriter fw = new FileWriter(new File(filename));
        //Write entire string buffer into the file
        fw.write(sb.toString());
        fw.close();

        System.err.println("deleted lines" + count);

    } catch (Exception e) {
        System.err.println("error:" + e.getMessage());
        e.printStackTrace();
        return -10;

    }

    return 10;

}

From source file:it.sardegnaricerche.voiceid.sr.Voiceid.java

public static void main(String[] args) {
    logger.info("Voiceid main method");
    logger.info("First argument: '" + args[0] + "'");
    long startTime = System.currentTimeMillis();
    Voiceid voiceid = null;//www.j a  va 2  s  . co  m
    GMMVoiceDB db = null;
    try {
        db = new GMMVoiceDB(args[1], new UBMModel(args[0]));
        File f = new File(args[2]);
        voiceid = new Voiceid(db, f, new LIUMStandardDiarizator());
        voiceid.extractClusters();
        voiceid.matchClusters();
        // voiceid.toWav();
        // voiceid.printClusters();
        JSONObject obj = voiceid.toJson();

        for (VCluster c : voiceid.getClusters()) {
            logger.info("" + c.getSample().getResource().getAbsolutePath());
        }

        // FileWriter fstream = new
        // FileWriter(f.getAbsolutePath().replaceFirst("[.][^.]+$", "") +
        // ".json");
        String filename = Utils.getBasename(f) + ".json";
        FileWriter fstream = new FileWriter(filename);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(obj.toString());
        // Close the output stream
        out.close();

        // voiceid.makeAllModels();
    } catch (IOException e) {
        logger.severe(e.getMessage());
    } catch (Exception ex) {
        logger.severe(ex.getMessage());
    }
    long endTime = System.currentTimeMillis();
    long duration = endTime - startTime;
    logger.info("Exit (" + ((float) duration / 1000) + " s)");
    // logger.info("Max Threads: " + (int) db.maxThreads);
}

From source file:Main.java

public static void createTestDataFile(String filename, String[] lines) throws IOException {
    FileWriter writer = null;// w  w  w  . j a  v  a 2 s  .c  om
    try {
        File file = new File(filename);
        file.deleteOnExit();
        writer = new FileWriter(file);
        for (String line : lines) {
            writer.write(line + "\n");
        }
    } finally {
        if (writer != null) {
            writer.close();
        }
    }

}

From source file:com.daphne.es.maintain.icon.web.controller.tmp.GenCssSql.java

private static void readUploadFile() throws IOException {
    String fromFile = "E:\\workspace\\git\\es\\web\\src\\main\\webapp\\WEB-INF\\static\\comp\\zTree\\css\\zTreeStyle\\img\\diy\\icon.txt";
    String toFile = "C:\\Documents and Settings\\Administrator\\?\\b.sql";
    String template = "insert into `maintain_icon` (`id`, `identity`, `img_src`, `type`, `width`, `height`) values(%1$d, '%2$s', '%3$s', 'upload_file', %4$d, %5$d);";

    List<String> list = FileUtils.readLines(new File(fromFile));
    FileWriter writer = new FileWriter(toFile);

    int count = 300;
    for (int i = 0, l = list.size(); i < l; i += 2) {
        writer.write(String.format(template, count++, list.get(i), list.get(i + 1), 16, 16));
        writer.write("\r\n");
    }//from  w w  w .j  a v a2  s.co  m

    writer.close();
}