Example usage for java.io FileWriter flush

List of usage examples for java.io FileWriter flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the stream.

Usage

From source file:com.moss.appsnap.keeper.freedesktop.FreeDesktopAppHandler.java

public static void writeDesktopFile(String name, File location, File target) throws IOException {
    FileWriter writer = new FileWriter(location);
    writer.append("[Desktop Entry]\n");
    writer.append("Type=Application\n");
    writer.append("Name=");
    writer.append(name);/*from  w w  w.j a va2 s .  c  o  m*/
    writer.append("\n");
    writer.append("Exec=");
    writer.append(SHELL);
    writer.append(" ");
    writer.append(target.getAbsolutePath());
    writer.append("\n");
    writer.flush();
    writer.close();
}

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

/**
 * Writes an InputStream to a temporary file.
 *
 * @param in the in//w ww.jav a 2 s. com
 * @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:Main.java

public static void writeContentToFile(String fileName, String contents) throws IOException {
    Log.d("writeContentToFile", fileName);
    File f = new File(fileName);
    f.getParentFile().mkdirs();/*from  w ww. j a va 2 s  .  co  m*/
    File tempFile = new File(fileName + ".tmp");
    FileWriter fw = new FileWriter(tempFile);
    BufferedWriter bw = new BufferedWriter(fw);
    int length = contents.length();
    if (length > 0) {
        bw.write(contents);
        //         int apart =  Math.min(length, 65536);
        //         int times = length / apart;
        //         for (int i = 0; i < times; i++) {
        //            bw.write(contents, i * apart, apart);
        //         }
        //         if (length % apart != 0) {
        //            bw.write(contents, times * apart, length - times * apart);
        //         }
        bw.flush();
        fw.flush();
        bw.close();
        fw.close();
        f.delete();
        tempFile.renameTo(f);
    }
}

From source file:uk.ac.horizon.aestheticodes.controllers.ExperienceListUpdater.java

public static void save(Context context, ExperienceListController experiences) {
    try {/*from w w  w .j a v  a2s . c o m*/
        final File experienceFile = new File(context.getFilesDir(), "experiences.json");
        final FileWriter writer = new FileWriter(experienceFile);
        final Collection<Experience> saveExperiences = new ArrayList<>();
        for (Experience experience : experiences.get()) {
            if (experience.getOp() != Experience.Operation.temp) {
                saveExperiences.add(experience);
            }
        }
        Gson gson = ExperienceParser.createParser();
        gson.toJson(saveExperiences, writer);

        writer.flush();
        writer.close();
    } catch (Exception e) {
        Log.e("", e.getMessage(), e);
    }
}

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 ava  2 s  .c  om*/
    while ((line = read.readLine()) != null) {
        fwri.write(line + "\n");
    }
    fwri.flush();
    fwri.close();
    read.close();
}

From source file:au.org.ala.layers.grid.GridClassBuilder.java

static void exportSLD(String filename, String name, ArrayList<Integer> maxValues, ArrayList<String> labels) {
    StringBuffer sld = new StringBuffer();
    /* header *//*from  w ww .  jav  a2s. c  o  m*/
    sld.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    sld.append(
            "<sld:StyledLayerDescriptor xmlns=\"http://www.opengis.net/sld\" xmlns:sld=\"http://www.opengis.net/sld\" xmlns:ogc=\"http://www.opengis.net/ogc\" xmlns:gml=\"http://www.opengis.net/gml\" version=\"1.0.0\">");
    sld.append("<sld:NamedLayer>");
    sld.append("<sld:Name>raster</sld:Name>");
    sld.append(" <sld:UserStyle>");
    sld.append("<sld:Name>raster</sld:Name>");
    sld.append("<sld:Title>A very simple color map</sld:Title>");
    sld.append("<sld:Abstract>A very basic color map</sld:Abstract>");
    sld.append("<sld:FeatureTypeStyle>");
    sld.append(" <sld:Name>name</sld:Name>");
    sld.append("<sld:FeatureTypeName>Feature</sld:FeatureTypeName>");
    sld.append(" <sld:Rule>");
    sld.append("   <sld:RasterSymbolizer>");
    sld.append(" <sld:Geometry>");
    sld.append(" <ogc:PropertyName>geom</ogc:PropertyName>");
    sld.append(" </sld:Geometry>");
    sld.append(" <sld:ChannelSelection>");
    sld.append(" <sld:GrayChannel>");
    sld.append("   <sld:SourceChannelName>1</sld:SourceChannelName>");
    sld.append(" </sld:GrayChannel>");
    sld.append(" </sld:ChannelSelection>");
    sld.append(" <sld:ColorMap type=\"intervals\">");

    //sort labels
    List<String> sortedLabels = new ArrayList<String>(labels);
    Collections.sort(sortedLabels);

    /* outputs */
    sld.append("\n<sld:ColorMapEntry color=\"#ffffff\" opacity=\"0\" quantity=\"1\"/>\n");
    for (int j = 0; j < sortedLabels.size(); j++) {
        int i = 0;
        while (i < labels.size()) {
            if (labels.get(i).equals(sortedLabels.get(j)))
                break;
            i++;
        }
        sld.append("<sld:ColorMapEntry color=\"#" + getHexColour(colours[i % colours.length]) + "\" quantity=\""
                + (maxValues.get(i) + 1) + ".0\" label=\"" + labels.get(i) + "\" opacity=\"1\"/>\r\n");
    }

    /* footer */
    sld.append(
            "</sld:ColorMap></sld:RasterSymbolizer></sld:Rule></sld:FeatureTypeStyle></sld:UserStyle></sld:NamedLayer></sld:StyledLayerDescriptor>");

    /* write */
    FileWriter fw = null;
    try {
        fw = new FileWriter(filename);
        fw.append(sld.toString());
        fw.flush();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (fw != null) {
            try {
                fw.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
}

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);//  w ww  .  j  av  a2s  .c  o  m
    fw.flush();
    fw.close();
}

From source file:loadTest.loadTestLib.LUtil.java

public static void writeCSVResult(LoadTestConfigModel model, long resTime) throws IOException {
    FileWriter writer = new FileWriter("loadTestResults.csv", true);
    writer.append(String.valueOf(model.getNodeCount()));
    writer.append(",");
    writer.append(String.valueOf(model.getFileCount()));
    writer.append(",");
    writer.append(String.valueOf(model.getFileSize()));
    writer.append(",");
    writer.append(String.valueOf(resTime));
    writer.append("\n");
    writer.flush();
    writer.close();//  w  w w. j a v a  2  s  .co  m
}

From source file:com.fbartnitzek.tasteemall.data.csv.CsvFileWriter.java

public static String writeFile(String[] headers, List<List<String>> entries, File file) {

    FileWriter fileWriter = null;
    CSVPrinter csvFilePrinter = null;//  w w w . j  a va 2  s.c  om
    String msg = null;
    try {
        fileWriter = new FileWriter(file); //initialize FileWriter object
        csvFilePrinter = new CSVPrinter(fileWriter, CSV_FORMAT_RFC4180); //initialize CSVPrinter object
        csvFilePrinter.printRecord(Arrays.asList(headers)); //Create CSV file header

        //Write a new student object list to the CSV file
        for (List<String> dataEntry : entries) {
            csvFilePrinter.printRecord(dataEntry);
        }

    } catch (Exception e) {
        e.printStackTrace();
        msg = "Error in CsvFileWriter !!!";
    } finally {
        try {
            if (fileWriter != null) {
                fileWriter.flush();
                fileWriter.close();
            }
            if (csvFilePrinter != null) {
                csvFilePrinter.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            msg = "Error while flushing/closing fileWriter/csvPrinter !!!";

        }
    }
    return msg;
}

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

/**
 * Creates a file in the platform's working directory.
 * /* ww  w.  ja v a 2  s .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;
    }
}