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 BufferedWriter getWriter(File file, final String group, String comment) throws IOException {
    BufferedWriter bw = new BufferedWriter(new FileWriter(file)) {

        @Override// ww  w . ja va  2 s  . c o m
        public void close() throws IOException {
            write("</" + group + ">");
            super.close();
        }
    };
    bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    bw.newLine();
    bw.newLine();
    bw.write("<!--" + comment + "-->");
    bw.newLine();
    bw.newLine();
    bw.write("<" + group + ">");
    bw.newLine();

    return bw;
}

From source file:Main.java

static public void savelog(String processedStr) {
    String fullFilename = "D:/test.txt";
    try {/*from  www  .j  av a2s.  c  o  m*/
        File newTextFile = new File(fullFilename);
        FileWriter fw;
        fw = new FileWriter(newTextFile);
        fw.write(processedStr);
        fw.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean saveStringToFile(String fileName, String saveString) {

    boolean saved = false;
    BufferedWriter bw = null;/*from   w ww .  java 2s.c  o  m*/

    try {
        bw = new BufferedWriter(new FileWriter(fileName));

        try {
            bw.write(saveString);
            saved = true;
        } finally {
            bw.close();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return saved;
}

From source file:Main.java

public static boolean writeOneLine(String fname, String value) {
    if (!new File(fname).exists()) {
        return false;
    }//from   www  .  j a va  2 s .co  m
    try {
        FileWriter fw = new FileWriter(fname);
        try {
            fw.write(value);
        } finally {
            fw.close();
        }
    } catch (IOException e) {
        String Error = "Error writing to " + fname + ". Exception: ";
        Log.e("TAG", Error, e);
        return false;
    }
    return true;
}

From source file:Main.java

public static boolean saveFile(String str, File path) {
    BufferedWriter bw = null;/*from   w w  w  . jav  a  2s .co m*/

    try {
        bw = new BufferedWriter(new FileWriter(path));
        bw.write(str);
        bw.flush();
        return true;

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

    } finally {
        if (bw != null) {
            try {
                bw.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

From source file:Main.java

public static File WriteStringToFile(String str) {
    File tmpFile = null;// www.j a v  a2s . c  om
    try {
        tmpFile = File.createTempFile("z4-", ".tmp");
        BufferedWriter out = new BufferedWriter(new FileWriter(tmpFile));
        out.write(str);
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return tmpFile;
}

From source file:Main.java

public static void writeStringAsFile(final String fileContents, String fileName) {
    try {//from  ww w .  j  a  va  2  s .c  o  m
        String path = Environment.getExternalStorageDirectory().getAbsolutePath();
        FileWriter out = new FileWriter(new File(path, fileName));
        out.write(fileContents);
        out.close();
    } catch (IOException e) {
        //!TODO
    }
}

From source file:Main.java

public static boolean writeOneLine(String filename, String value) {
    FileWriter fileWriter = null;
    try {/*  ww  w  . jav a  2 s  . com*/
        fileWriter = new FileWriter(filename);
        fileWriter.write(value);
    } catch (IOException e) {
        String Error = "Error writing { " + value + " } to file: " + filename;
        Log.e(TAG, Error, e);
        return false;
    } finally {
        if (fileWriter != null) {
            try {
                fileWriter.close();
            } catch (IOException ignored) {
                // failed to close writer
            }
        }
    }
    return true;
}

From source file:Main.java

public static boolean writeString(String filePath, String content) {
    File file = new File(filePath);
    if (!file.getParentFile().exists())
        file.getParentFile().mkdirs();//  w  ww. java2  s .c o  m

    FileWriter writer = null;
    try {

        writer = new FileWriter(file);
        writer.write(content);

    } catch (IOException e) {
    } finally {
        try {
            if (writer != null) {

                writer.close();
                return true;
            }
        } catch (IOException e) {
        }
    }
    return false;
}

From source file:Main.java

public static boolean writeTextContentToFile(String content, String dstPath) {
    BufferedWriter writer = null;
    try {//from www  .j  a  va 2s .  c om
        writer = new BufferedWriter(new FileWriter(dstPath));
        writer.write(content);
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}