Java Utililty Methods FileWriter Write

List of utility methods to do FileWriter Write

Description

The list of methods to do FileWriter Write are organized into topic(s).

Method

voidsaveContent(String context, String filename)
save Content
File file = new File(filename);
if (!file.exists()) {
    try {
        file.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
try {
    FileWriter writer = new FileWriter(file, true);
    writer.append(context);
    writer.flush();
} catch (IOException e) {
    e.printStackTrace();
voidsaveData(Map> data, File file)
Saves the dataset.
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
for (Entry<String, List<double[]>> classEntry : data.entrySet()) {
    String classLabel = classEntry.getKey();
    for (double[] arr : classEntry.getValue()) {
        String arrStr = Arrays.toString(arr).replaceAll("[\\]\\[\\s]+", "");
        bw.write(classLabel + "," + arrStr + CR);
bw.close();
voidsaveData(Map> data, String file)
save Data
try {
    File f = new File(file);
    if (!f.exists()) {
        f.createNewFile();
    FileWriter fw = new FileWriter(f.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.newLine();
...
voidsaveData(String fileName, double totalTime, int[] resultSubset)
save Data
FileWriter fw = null;
fw = new FileWriter(fileName);
String str1 = "totalTime is:" + totalTime + "\n";
String str2 = "resultSubset: " + "\n";
fw.write(str1);
fw.write(str2);
String str = "";
for (int i = 0; i < resultSubset.length; i++) {
...
voidsaveDictionary(String fileName, Map values)
save Dictionary
final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
values.entrySet().forEach(p -> {
    try {
        writer.write(p.toString());
        writer.newLine();
    } catch (IOException e) {
        e.printStackTrace();
});
writer.flush();
writer.close();
FilesaveDotGraph(final String dotGraph, final File outputDirectory)
Save the specified Graphviz's dot file at the top of the specified project.
File returnValue;
FileWriter fileWriter;
returnValue = new File(outputDirectory, "schemas.dot");
fileWriter = new FileWriter(returnValue, false);
fileWriter.write(dotGraph);
fileWriter.close();
return returnValue;
voidsaveDoubleArray2File(Double[] doubleArray, String fileName)
create a new file and save the doubleArray to the fileName
FileWriter fw = null;
fw = new FileWriter(fileName);
String str = "";
for (int i = 0; i < doubleArray.length; i++) {
    str = str + doubleArray[i];
    str = str + "\n";
fw.write(str);
...
voidsaveDoubleArray2FileAppend(Double[] doubleArray, String fileName)
save doubleArray to fileName, if the file exists, we just append the double array to the file
FileWriter fw = null;
fw = new FileWriter(fileName, true);
String str = "";
for (int i = 0; i < doubleArray.length; i++) {
    str = str + doubleArray[i];
    str = str + "\n";
fw.write(str);
...
voidsaveEasyQuestImpl(final String quest, final File targetFile)
save Easy Quest Impl
final File backupFile = new File(targetFile.getAbsolutePath() + ".bak"); 
FileWriter fw = null;
try {
    if (backupFile.exists()) {
        backupFile.delete();
    if (targetFile.exists()) {
        targetFile.renameTo(backupFile);
...
voidsaveFile(File directory, String fileName, String content)
This method save the String content in a file which name is fileName .
BufferedWriter output;
FileWriter file = new FileWriter(directory.getAbsolutePath() + File.separator + fileName);
output = new BufferedWriter(file);
output.write(content);
output.close();