Example usage for java.io FileWriter write

List of usage examples for java.io FileWriter write

Introduction

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

Prototype

public void write(int c) throws IOException 

Source Link

Document

Writes a single character.

Usage

From source file:it.intecs.pisa.toolbox.util.URLReader.java

public static void writeURLContentToXML(String infoXmlFile, String fileName) throws Exception {
    try {/*from   ww w .ja v a 2  s . com*/
        String host = null;
        String url = null;
        int port = 0;
        Element infoXml = new DOMUtil().fileToDocument(new File(infoXmlFile)).getDocumentElement();
        if (infoXml != null) {
            host = infoXml.getAttribute("newVersionHost");
            url = infoXml.getAttribute("newVersionUrl");
            String portStr = infoXml.getAttribute("newVersionPort");
            Integer portInt = new Integer(portStr);
            port = portInt.intValue();
        }

        String fileContent = (String) getURLContent(host, url, port);
        if (fileContent == null)
            return;
        fileContent = fileContent.substring(fileContent.indexOf("<"), fileContent.lastIndexOf(">") + 1);
        File newFile = new File(fileName);
        FileWriter fw = new FileWriter(newFile);
        fw.write(fileContent);
        fw.close();
    } catch (Exception e) {
        e.printStackTrace(System.out);
        return;
    }
}

From source file:com.sldeditor.test.SLDTestRunner.java

/**
 * Writes an InputStream to a temporary file.
 *
 * @param in the in/*from   w  w  w  .  j a va  2  s  .  c  om*/
 * @return the file
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static File stream2file(InputStream in) throws IOException {
    final File tempFile = File.createTempFile(PREFIX, SUFFIX);
    tempFile.deleteOnExit();
    try (FileOutputStream out = new FileOutputStream(tempFile)) {
        IOUtils.copy(in, out);
    }

    // Update the font for the operating system
    String newFont = getFontForOS();
    if (newFont.compareToIgnoreCase(DEFAULT_FONT) != 0) {
        BufferedReader br = new BufferedReader(new FileReader(tempFile));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line.replace(DEFAULT_FONT, newFont));
                sb.append("\n");
                line = br.readLine();
            }
            try {
                FileWriter fileWriter = new FileWriter(tempFile);
                fileWriter.write(sb.toString());
                fileWriter.flush();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } finally {
            br.close();
        }
    }
    return tempFile;
}

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

public static void transform(File input, File output, Charset dstCharset) throws IOException {
    BufferedReader br = null;// ww w  .  j  a v a  2 s. 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.bluexml.side.m2.repositoryBuilder.Builder.java

private static void writeShellScript(File script, List<File> poms) throws Exception {
    // create file
    if (!script.exists()) {
        script.createNewFile();//w  w w . j  a v a2 s . c o  m
    }

    FileWriter fw = new FileWriter(script);

    // write header
    String header = "#!/bin/bash\n";
    header += "if [ $# -eq 1 ]; then" + "\n";
    header += "  MAVENREPO=$1" + "\n";
    header += "else" + "\n";
    header += "  echo \"Usage: patcher.sh MAVENREPO\"" + "\n";
    header += "  echo \"       with MAVENREPO = maven.repo.local absolute path\"" + "\n";
    header += "  exit -2" + "\n";
    header += "fi" + "\n";
    fw.write(header);

    for (File file : poms) {
        fw.write("mvn dependency:go-offline -P public -f " + file.getAbsolutePath()
                + " -Dmaven.repo.local=$MAVENREPO\n");
    }

    // save close
    fw.flush();
    fw.close();
}

From source file:cn.kk.exia.MangaDownloader.java

private static void appendUrl(final File downloaded, final String mangaUrl) throws IOException {
    final FileWriter f = new FileWriter(downloaded, true);
    f.write(mangaUrl);
    f.write('\n');
    f.close();//  www .  j av  a  2  s  . c  om
}

From source file:com.blackberry.logtools.LogTools.java

public static void dosTounix(File f, File dos2unix) throws IOException {
    BufferedReader read = new BufferedReader(new FileReader(f));
    FileWriter fwri = new FileWriter(dos2unix);
    String line;/*ww w  .j a  v a2  s  .c o m*/
    while ((line = read.readLine()) != null) {
        fwri.write(line + "\n");
    }
    fwri.flush();
    fwri.close();
    read.close();
}

From source file:bizlogic.Records.java

public static void writeCSV(Connection DBcon, String record_id) throws SQLException {

    Statement st;//from w ww . ja  va2  s .co m
    ResultSet rs = null;

    System.out.println("WriteCSV started");

    try {
        st = DBcon.createStatement();
        rs = st.executeQuery("SELECT * FROM PUBLIC.t" + record_id);
        System.out.println("Result set read finished");
    } catch (SQLException ex) {
        Logger lgr = Logger.getLogger(Records.class.getName());
        lgr.log(Level.SEVERE, ex.getMessage(), ex);
    }

    try {

        String DELIMITER = ",";
        String NEW_LINE = "\n";
        String FILE_HEADER = "Time,Series";

        System.out.println("Delete old file");
        FileWriter csvFile = new FileWriter("/var/lib/tomcat8/webapps/ROOT/Records/Data/" + record_id + ".csv");

        //BufferedWriter csvFile = new BufferedWriter(
        //        new OutputStreamWriter(new FileOutputStream(new File(
        //                "/var/lib/tomcat8/webapps/ROOT/Records/Data/" + 
        //                        record_id + ".csv"))));

        csvFile.write("");

        csvFile.append(FILE_HEADER);
        csvFile.append(NEW_LINE);

        Calendar calendar = new GregorianCalendar();

        System.out.println("Writing file...");
        while (rs.next()) {

            long time_stamp = rs.getLong("time");
            double value = rs.getDouble("value");
            String _year;
            String _month;
            String _day;
            String _hour;
            String _min;
            String _sec;

            calendar.setTimeInMillis(time_stamp);

            _year = Integer.toString(calendar.get(Calendar.YEAR));
            _month = Integer.toString(calendar.get(Calendar.MONTH) + 1);
            _day = Integer.toString(calendar.get(Calendar.DAY_OF_MONTH));
            _hour = Integer.toString(calendar.get(Calendar.HOUR_OF_DAY));
            _min = Integer.toString(calendar.get(Calendar.MINUTE));
            _sec = Integer.toString(calendar.get(Calendar.SECOND));

            csvFile.append(_year + "/" + _month + "/" + _day + " " + _hour + ":" + _min + ":" + _sec + DELIMITER
                    + Double.toString(value) + NEW_LINE); //new Date("2009/07/19 12:34:56")

        }
        System.out.print("File written");
        rs.close();
        //csvFile.flush();
        csvFile.close();
    }

    catch (IOException ex) {
        Logger.getLogger(Records.class.getName()).log(Level.WARNING, null, ex);
    }
}

From source file:gedi.util.FileUtils.java

public static void writeAllText(String text, File file) throws IOException {
    FileWriter fw = new FileWriter(file);
    fw.write(text);
    fw.flush();/*from  ww  w  . jav  a  2  s.c  om*/
    fw.close();
}

From source file:gov.nasa.ensemble.common.io.FileUtilities.java

/**
 * Creates a file in the platform's working directory.
 * // w w  w.  ja  va2s  . c  o m
 * @param relativeFilename
 * @param ins
 * @return boolean
 * @throws IOException
 */
public static boolean createFileInWorkingDirectory(String relativeFilename, InputStream ins)
        throws IOException {
    Location instanceLocation = Platform.getInstanceLocation();
    if (instanceLocation == null || !instanceLocation.isSet()) {
        Logger logger = Logger.getLogger(FileUtilities.class);
        logger.warn("cannot create file '" + relativeFilename
                + "' because platform does not have a working directory.");
        return false;
    } else {
        File cacheFile = new File(instanceLocation.getURL().getFile() + File.separator + relativeFilename);
        // if cache file already exists, remove it because we are about to
        // overwrite it
        if (cacheFile.exists()) {
            cacheFile.delete();
        }
        cacheFile.getParentFile().mkdirs();
        cacheFile.createNewFile();
        BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
        FileWriter writer = new FileWriter(cacheFile);
        String line = reader.readLine();
        while (line != null) {
            writer.write(line);
            writer.write("\n");
            line = reader.readLine();
        }
        writer.flush();
        writer.close();
        return true;
    }
}

From source file:AIR.ResourceBundler.Console.ResourcesBuilder.java

private static void compressJS(FileWriter sw, BufferedReader sr, FileSet fileSet) throws IOException {
    // TODO Shajib: implement this
    // compress javascript into output file
    /*/*from w  w w . j a va2 s  .  c  o  m*/
     * JSMin jsMin = new JSMin (sw, sr); jsMin.SettingsRemoveEmptyLines =
     * fileSet.isRemoveEmptyLines (); jsMin.SettingsRemoveComments =
     * fileSet.isRemoveComments (); jsMin.SettingsRemoveSpaces =
     * fileSet.isRemoveSpaces (); jsMin.Compress ();
     */
    String line;
    while ((line = sr.readLine()) != null) {
        line = line.trim();
        sw.write(line + "\n");
    }
}