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 Boolean canWrite(File f) {
    if (f.isDirectory()) {
        FileWriter w = null;/*from   w w  w .j  a v a 2s .  c  o m*/
        String testFilename = f.getPath() + "/.EASYRPG_WRITE_TEST";
        try {
            w = new FileWriter(testFilename);
            // Permissions are checked on open, but it is Android, better be save
            w.write("Android >.<");
        } catch (IOException e) {
            return false;
        } finally {
            try {
                if (w != null) {
                    w.close();
                }
            } catch (IOException e) {
            }
        }

        File testFile = new File(testFilename);
        if (testFile.exists()) {
            // Does not throw
            testFile.delete();
        }
    } else {
        boolean deleteAfter = f.exists();
        try {
            FileWriter w = new FileWriter(f, true);
            w.close();
        } catch (IOException e) {
            return false;
        }

        if (deleteAfter) {
            f.delete();
        }
    }

    return true;
}

From source file:msuresh.raftdistdb.JSONString.java

public static void ConvertJSON2File(JSONObject json, String floc) {
    try (FileWriter file = new FileWriter(floc)) {
        file.write(json.toJSONString());
    } catch (Exception ex) {

    }/*from  w  ww .ja  va  2 s .c  om*/

}

From source file:Main.java

public static void writeListToFile(String filepath, List<String> data) {
    try {//from   ww w .  j a  va 2 s  . com
        File file = new File(filepath);
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fileWriter = new FileWriter(file);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        for (String line : data) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }

        bufferedWriter.flush();
        bufferedWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void writeNewFile(String filePath, String fileContents) {
    File f = new File(filePath);
    if (f.exists()) {
        f.delete();//  w  w w  .  j  av  a2 s.  c om
    }

    try {
        // Create file
        FileWriter fstream = new FileWriter(f);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(fileContents);
        //Close the output stream
        out.close();
    } catch (Exception e) {
        Log.d(TAG, "Failed to create " + filePath + " File contents: " + fileContents);
    }
}

From source file:Main.java

public static void writeNewFile(String filePath, String fileContents) {
    File f = new File(filePath);
    if (f.exists()) {
        f.delete();//  w w  w.  ja v a 2s. c om
    }

    try {
        // Create file 
        FileWriter fstream = new FileWriter(f);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(fileContents);
        //Close the output stream
        out.close();
    } catch (Exception e) {
        Log.d(TAG, "Failed to create " + filePath + " File contents: " + fileContents);
    }
}

From source file:Main.java

/**
 * Writes the given string into the given file.
 * //from  w  ww . j a  v  a  2s  .  c  o m
 * @param contents
 *            String representing the file contents.
 * @param filename
 *            Name of the file to be written.
 * @throws IOException
 */
public static void writeFile(String contents, String filename) throws IOException {
    FileWriter fw = new FileWriter(filename);
    fw.write(contents);
    fw.flush();
    fw.close();
}

From source file:Main.java

public static void writeFile(String filepath, InputStream is) throws IOException {
    File file = new File(filepath);
    BufferedWriter bw = new BufferedWriter(new FileWriter(file));

    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String line;//w ww.j a v  a 2 s.  c o  m
    while ((line = br.readLine()) != null) {
        bw.write(line + "\n");
    }

    bw.close();
    br.close();
}

From source file:Main.java

public static void writeFile(String filename, String content) {
    File file = new File(filename);

    if (!file.exists()) {
        createFile(file);//from  w  w w .  j a  v a2s . c om
    }

    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(filename));
        writer.write(content);
        writer.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close();
                writer = null;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

From source file:Main.java

/**
 * Saves passed string to file/*w ww  . j  a  v a  2  s. c  o  m*/
 *
 * @param filename      path to file
 * @param stringToWrite string to save
 */
public static void stringToFile(String filename, String stringToWrite) {
    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
        writer.write(stringToWrite);
        writer.flush();
        writer.close();
    } catch (Exception ignored) {
    }
}

From source file:Main.java

public static boolean createFile(String name) {
    File outfile = new File(name);
    BufferedWriter writer = null;
    if (!outfile.isFile()) {
        try {/*  w  w w . j  ava 2  s  .  co m*/
            outfile.createNewFile();
            writer = new BufferedWriter(new FileWriter(outfile));
            writer.write(
                    "#_*_ coding: iso8859_1\n# Script API\n\nfrom com.android.python import AndroidDriver\n\n");
            writer.flush();
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}