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:base.compilations.PascalCompilation.java

@Override
protected void writeUserCodeInto(File file, String code) throws IOException {
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
        writer.write(code);/*from   w  w  w  .  j  a  va  2s  .c o  m*/
        writer.flush();
    }
}

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

private static void readClass() throws IOException {
    String fromFile = "C:\\Documents and Settings\\Administrator\\?\\a.txt";
    String toFile = "C:\\Documents and Settings\\Administrator\\?\\b.sql";
    String template = "insert into `maintain_icon` (`id`, `identity`, `css_class`, `type`) values(%1$d, '%2$s', '%2$s', 'css_class');;";

    List<String> cssClassList = FileUtils.readLines(new File(fromFile));
    List<String> hasReadList = Lists.newArrayList();
    FileWriter writer = new FileWriter(toFile);

    for (int i = 0, l = cssClassList.size(); i < l; i++) {
        if (!hasReadList.contains(cssClassList.get(i))) {
            writer.write(String.format(template, i + 1, cssClassList.get(i).trim()));
            writer.write("\r\n");
            hasReadList.add(cssClassList.get(i));
        }//from  ww  w .  ja  va2s.  c  o m
    }

    writer.close();
}

From source file:fridgegameinstaller.MCJsonConf.java

public static void getJson(String path, int mb) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet("http://api.fridgegame.com/mcconf/Fridgegame.json");

    CloseableHttpResponse response1 = httpclient.execute(httpGet);
    try {//from  www  .j  a  v  a2 s .c  om
        System.out.println(httpGet.toString());
        System.out.println(response1.getStatusLine());
        BufferedReader br = new BufferedReader(new InputStreamReader(response1.getEntity().getContent()));
        String a;
        String output = "";
        while ((a = br.readLine()) != null) {
            output += a + "\n";
        }
        System.out.println(output);
        try {

            JSONObject json = new JSONObject(output);
            String mcArgs = json.getString("minecraftArguments");
            String regex = "(-Xmx[^ ]*)";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(mcArgs);
            String newArgs = m.replaceAll("-Xmx" + mb + "M");
            json.put("minecraftArguments", newArgs);
            FileWriter file = new FileWriter(path);

            try {
                file.write(json.toString());

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

            } finally {
                file.flush();
                file.close();
            }

        } catch (JSONException e) {

        }
    } finally {
        response1.close();
    }

}

From source file:de.unisb.cs.st.javalanche.mutation.util.DBPerformanceTest.java

private static void testWrite() {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();//w  w w . jav  a 2 s .  co  m
    try {
        File file = new File("test.txt");
        file.deleteOnExit();
        FileWriter fw = new FileWriter(file);
        BufferedWriter w = new BufferedWriter(fw);
        for (int i = 0; i < 50 * 1024 * 1024; i++) {
            w.append('x');
        }
        w.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    stopWatch.stop();
    System.out.printf("Writing file took %s\n", DurationFormatUtils.formatDurationHMS(stopWatch.getTime()));

}

From source file:com.zimbra.cs.db.MySQL.java

public static void main(String args[]) {
    // command line argument parsing
    Options options = new Options();
    CommandLine cl = Versions.parseCmdlineArgs(args, options);

    String outputDir = cl.getOptionValue("o");
    File outFile = new File(outputDir, "versions-init.sql");
    outFile.delete();/* ww w .  j ava  2 s . co  m*/

    try {
        String redoVer = com.zimbra.cs.redolog.Version.latest().toString();
        String outStr = "-- AUTO-GENERATED .SQL FILE - Generated by the MySQL versions tool\n" + "USE zimbra;\n"
                + "INSERT INTO zimbra.config(name, value, description) VALUES\n" + "\t('db.version', '"
                + Versions.DB_VERSION + "', 'db schema version'),\n" + "\t('index.version', '"
                + Versions.INDEX_VERSION + "', 'index version'),\n" + "\t('redolog.version', '" + redoVer
                + "', 'redolog version')\n" + ";\nCOMMIT;\n";

        Writer output = new BufferedWriter(new FileWriter(outFile));
        output.write(outStr);
        if (output != null)
            output.close();
    } catch (IOException e) {
        System.out.println("ERROR - caught exception at\n");
        e.printStackTrace();
        System.exit(-1);
    }
}

From source file:net.firejack.platform.core.utils.MiscUtils.java

/**
 * @param properties//w  w  w .  ja  v  a2  s  . com
 * @param checkFileExists
 * @param property
 * @param value
 * @throws java.io.IOException
 */
public static void setProperties(File properties, boolean checkFileExists, String property, String value)
        throws IOException {
    if (checkFileExists && !properties.exists()) {
        logger.error("Properties file [" + properties.getAbsolutePath() + "] does not exist.");
        throw new FileNotFoundException("Properties file does not found.");
        //            IOHelper.delete(dbProperties);
    }
    Properties props = new Properties();
    if (properties.exists()) {
        FileReader reader = new FileReader(properties);
        props.load(reader);
        reader.close();
    }
    props.put(property, value);
    FileWriter writer = new FileWriter(properties);
    props.store(writer, null);
    writer.flush();
    writer.close();
}

From source file:com.pullup.app.util.ImageUtils.java

public String saveImage(InputStream stream, String token) {
    byte[] bytes = null;
    String path = "C:\\pullup\\profile\\" + token + ".png";
    try {//from  w w  w .java  2 s .  co m
        bytes = IOUtils.toByteArray(stream);
        log.info("Saving image");
        IOUtils.write(token, new FileWriter(new File(path)));
    } catch (IOException ex) {
        log.severe("Could not write image: " + ex.getMessage());
    }

    return path;
}

From source file:com.redhat.rhn.common.util.FileUtils.java

/**
 * Save a String to a file on disk using specified path.
 *
 * WARNING:  This deletes the original file before it writes.
 *
 * @param contents to save to file on disk
 * @param path to save file to.//from   ww  w .  ja  va  2s.  co  m
 */
public static void writeStringToFile(String contents, String path) {
    try {
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
        Writer output = new BufferedWriter(new FileWriter(file));
        try {
            output.write(contents);
        } finally {
            output.close();
        }
    } catch (Exception e) {
        log.error("Error trying to write file to disk: [" + path + "]", e);
        throw new RuntimeException(e);
    }
}

From source file:AuthenticateNT.java

public Object run() {
    try {//from w  w w.  j av  a 2s. c o  m
        File file = new File("D:/", "privilegedFile.txt");
        FileWriter fileWriter = new FileWriter(file);

        fileWriter.write("Welcome to JAAS!");
        fileWriter.close();
    } catch (IOException ioException) {
        ioException.printStackTrace();
    }

    return null;

}

From source file:com.gargoylesoftware.htmlunit.source.JQuery173Extractor.java

/**
 * Transforms the raw expectation, to the needed one by HtmlUnit.
 * This methods puts only the main line of the test output, without the details.
 *
 * @param input the raw file of real browser, with header and footer removed
 * @param output the expectation/*from  w  w w .  j  a v a  2 s .c o m*/
 * @throws IOException if an error occurs
 */
public static void extractExpectations(final File input, final File output) throws IOException {
    final BufferedReader reader = new BufferedReader(new FileReader(input));
    final BufferedWriter writer = new BufferedWriter(new FileWriter(output));
    int testNumber = 1;
    String line;
    while ((line = reader.readLine()) != null) {
        line = line.trim();
        if (line.startsWith("" + testNumber + '.') && line.endsWith("Rerun")) {
            line = line.substring(0, line.length() - 5);
            writer.write(line + "\n");
            testNumber++;
        }
    }
    System.out.println("Last output #" + (testNumber - 1));
    reader.close();
    writer.close();
}