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 getStringFromXML(Node node, String dtdFilename, String outputFileName)
        throws TransformerException, IOException {
    File file = new File(outputFileName);
    if (!file.isFile())
        file.createNewFile();//  w w  w . jav  a  2s .c o  m
    BufferedWriter out = new BufferedWriter(new FileWriter(file));

    String workingPath = System.setProperty("user.dir", file.getAbsoluteFile().getParent());
    try {
        getStringFromXML(node, dtdFilename, out);
    } finally {
        System.setProperty("user.dir", workingPath);
    }

    out.flush();
    out.close();
}

From source file:com.oozierunner.core.FileManager.java

public static BufferedWriter getFileWriter(String fileName) throws IOException {

    System.out.println("File to write in :-> " + fileName);

    File file = new File(fileName);

    // if file doesnt exists, then create it
    if (!file.exists()) {
        file.createNewFile();//from  w  w w  . j av  a 2 s. c o  m
    }

    FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

    return bufferedWriter;
}

From source file:Main.java

/**
 * Writes out the contents.//  ww w  . ja v a  2  s. c om
 * @param outFile outFile
 * @param contents contents
 * @throws FileNotFoundException FileNotFoundException
 * @throws IOException IOException
 */
public static void setContents(final File outFile, final String contents)
        throws FileNotFoundException, IOException {
    if (outFile == null) {
        throw new IllegalArgumentException("File should not be null."); //$NON-NLS-1$
    }
    if (outFile.exists()) {
        if (!outFile.isFile()) {
            throw new IllegalArgumentException("Should not be a directory: " + outFile); //$NON-NLS-1$
        }
        if (!outFile.canWrite()) {
            throw new IllegalArgumentException("File cannot be written: " + outFile); //$NON-NLS-1$
        }
    }

    Writer output = null;
    try {
        output = new BufferedWriter(new FileWriter(outFile));
        if (contents != null) {
            output.write(contents);
            output.flush();
        }

    } finally {
        if (output != null) {
            output.close();
        }
    }
}

From source file:foss.filemanager.core.Utils.java

public static void transform(File input, File output, Charset dstCharset) throws IOException {
    BufferedReader br = null;/*w  w w.ja va  2s .  c om*/
    FileWriter fileWriter = new FileWriter(output);
    try {
        String sCurrentLine;
        br = new BufferedReader(new FileReader(input));
        int i = 0;
        while ((sCurrentLine = br.readLine()) != null) {
            //Charset srcCharset = Charset.forName("UTF-8");
            InputStream is = new FileInputStream(input);
            Charset srcCharset = Charset.forName(guessEncoding(is));
            byte[] isoB = encode(sCurrentLine.getBytes(), srcCharset, dstCharset);
            fileWriter.write(new String(isoB, dstCharset));
            fileWriter.write("\n");
            System.out.println(i++);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fileWriter.flush();
            fileWriter.close();
            if (br != null)
                br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:com.discursive.jccook.io.CopyExample.java

public void start() {
    try {/*from  w  w w . j ava 2s.co m*/
        Writer writer = new FileWriter("test.dat");
        InputStream inputStream = getClass().getResourceAsStream("./test.resource");
        CopyUtils.copy(inputStream, writer);
        writer.close();
        inputStream.close();
    } catch (IOException e) {
        System.out.println("Error copying data");
    }

}

From source file:JSON.GenererJSONSave.java

/**
 * Methode pour sauvegarder le plateau d'un jeu
 * @param p //  ww  w.  j a v  a 2  s.co  m
 */
public void Save(Plateau p) {
    ArrayList<Sauvegarde> Liste = new ArrayList();
    int k = 0;
    for (int i = 0; i < p.getAbsci(); i++) {
        for (int j = 0; j < p.getOrdonne(); j++) {
            if (p.grille[i][j] != null)
                Liste.add(new Sauvegarde(i, j, p.grille[i][j].nom));

        }
    }
    JSONObject objet = new JSONObject();
    JSONArray jsArray = new JSONArray(Liste);
    objet.put("coordonne", jsArray);
    // Cration du fichier de sortie
    FileWriter fs = null;
    try {
        fs = new FileWriter("C:\\Users\\Alphonse\\Desktop\\output2.txt");
    } catch (IOException e) {
        System.err.println("Erreur lors de l'ouverture du fichier ");
        System.err.println(e);
        System.exit(-1);
    }

    // Sauvegarde dans le fichier
    try {
        objet.write(fs);
        fs.flush();
    } catch (IOException e) {
        System.err.println("Erreur lors de l'criture dans le fichier : " + e);
        System.exit(-1);
    }
}

From source file:it.unibas.spicy.persistence.idgenerator.utils.ExportCsv.java

public void performAction() {
    //        CSVWriter csvWriter;
    BufferedWriter bwriter;//  w  ww  .  j  a  v a 2  s.co m
    try {
        bwriter = new BufferedWriter(new FileWriter(FilenameUtils.separatorsToSystem(pathToExport)));
        //            csvWriter = new CSVWriter(new FileWriter(FilenameUtils.separatorsToSystem(pathToExport)), CSVWriter.DEFAULT_SEPARATOR, CSVWriter.DEFAULT_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER);
        //            csvWriter.writeNext(header, false);
        int line = 1;
        String str = "";
        for (int i = 0; i < header.length; i++) {
            str += "\"" + header[i].trim() + "\",";
        }
        str = str.substring(0, str.length() - 1);
        str += "\n";
        for (InputDataModel output : targetValues) {
            String row = "";
            for (String s : output.getValue()) {
                if (s != null)
                    row += "\"" + s + "\",";
                else
                    row += ",";
                //                    row = row.replace("\"", "");
            }
            //                csvWriter.writeNext(row.substring(0, row.length()-1).split(","),false);
            row = row.substring(0, row.length() - 1);
            str += row + "\n";
        }
        bwriter.write(str);
        bwriter.close();
        //            csvWriter.close();
    } catch (IOException e) {
        System.err.println(e.getMessage());
        System.exit(-1);
    }

}

From source file:Main.java

/**
 * Transforms an xml-file (xmlFilename) using an xsl-file (xslFilename) and
 * writes the output into file (outputFilename).
 * /* w  w w  .  j a  va2 s. c  o  m*/
 * @param xmlFile
 *            File: the xml-source-file to be transformed
 * @param xslFile
 *            File: the xsl-file with the transformation rules
 * @param outputFilename
 *            String: the name of the file the result will be written to
 */
public static void applyXSL(File xmlFile, File xslFile, String outputFilename) {
    try {
        // DocumentBuilderFactory docBFactory = DocumentBuilderFactory
        // .newInstance();
        // DocumentBuilder docBuilder = docBFactory.newDocumentBuilder();
        StreamSource xslStream = new StreamSource(xslFile);
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer(xslStream);
        StreamSource xmlStream = new StreamSource(xmlFile);
        StreamResult result = new StreamResult(new BufferedWriter(new FileWriter(outputFilename)));
        transformer.transform(xmlStream, result);
        result.getWriter().close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:org.jodconverter.boot.LocalConverterITest.java

/**
 * Creates an input file to convert and an output test directory just once.
 *
 * @throws IOException if an IO error occurs.
 *//*from  w ww. ja va2  s  .  co m*/
@BeforeClass
public static void setUpClass() throws IOException {

    inputFileTxt = testFolder.newFile("inputFile.txt");
    try (final PrintWriter writer = new PrintWriter(new FileWriter(inputFileTxt))) {
        writer.println("This is the first line of the input file.");
        writer.println("This is the second line of the input file.");
    }
}

From source file:io.proleap.vb6.TestGenerator.java

public static void generateTestClass(final File vb6InputFile, final File outputDirectory,
        final String packageName) throws IOException {
    final String inputFilename = getInputFilename(vb6InputFile);
    final File outputFile = new File(
            outputDirectory + "/" + inputFilename + OUTPUT_FILE_SUFFIX + JAVA_EXTENSION);

    final boolean createdNewFile = outputFile.createNewFile();

    if (createdNewFile) {
        LOG.info("Creating unit test {}.", outputFile);

        final PrintWriter pWriter = new PrintWriter(new FileWriter(outputFile));
        final String vb6InputFileName = vb6InputFile.getPath().replace("\\", "/");

        pWriter.write("package " + packageName + ";\n");
        pWriter.write("\n");
        pWriter.write("import java.io.File;\n");
        pWriter.write("\n");
        pWriter.write("import org.junit.Test;\n");
        pWriter.write("import io.proleap.vb6.runner.VbParseTestRunner;\n");
        pWriter.write("import io.proleap.vb6.runner.impl.VbParseTestRunnerImpl;\n");
        pWriter.write("\n");
        pWriter.write("public class " + inputFilename + "Test {\n");
        pWriter.write("\n");
        pWriter.write("   @Test\n");
        pWriter.write("   public void test() throws Exception {\n");
        pWriter.write("      final File inputFile = new File(\"" + vb6InputFileName + "\");\n");
        pWriter.write("      final VbParseTestRunner runner = new VbParseTestRunnerImpl();\n");
        pWriter.write("      runner.parseFile(inputFile);\n");
        pWriter.write("   }\n");
        pWriter.write("}");

        pWriter.flush();/*from  w  ww .j av a2s.  c om*/
        pWriter.close();
    }
}