Android Text File Write writeFileAsStringWorldWritable(File file, String text)

Here you can find the source of writeFileAsStringWorldWritable(File file, String text)

Description

write File As String World Writable

License

Apache License

Declaration

public static void writeFileAsStringWorldWritable(File file, String text) 

Method Source Code

/*/*w  w w.jav a2s . c  o m*/
Copyright (c) 2012 IBM Corp.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.channels.FileChannel.*;
import java.util.*;
import java.util.zip.*;

public class Main{
    private static HashMap<String, String> serial;
    public static final String SERIALPREFIX = "SER:";
    public static void writeFileAsStringWorldWritable(File file, String text) {
        ensureWorldWriteable(file);
        writeFileAsString(file, text);
        file.setWritable(true, false);
    }
    public static void ensureWorldWriteable(File f) {
        File parent = f.getParentFile();
        if (parent != null && !parent.exists()) {
            ensureWorldWriteable(parent);
            parent.mkdir();
            parent.setWritable(true, false);
            parent.setExecutable(true, false);
        }
    }
    public static void writeFileAsString(File file, String text,
            String encoding) {
        try {
            ensureWriteable(file);
            OutputStream os = new FileOutputStream(file);
            if (file.getName().endsWith(".gz")) {
                os = new GZIPOutputStream(os);
            }
            PrintStream out = new PrintStream(os, true, encoding);
            out.print(text);
            out.close();
        } catch (Exception e) {
            throw new Error(e);
        }
    }
    public static void writeFileAsString(File file, String text) {
        try {
            ensureWriteable(file);
            OutputStream os = new FileOutputStream(file);
            if (file.getName().endsWith(".gz")) {
                os = new GZIPOutputStream(os);
            }
            PrintStream out = new PrintStream(os);
            out.print(text);
            out.close();
        } catch (Exception e) {
            throw new Error(e);
        }
    }
    public static void writeFileAsString(String filename, String text) {
        if (filename.startsWith(SERIALPREFIX)) {
            filename = serialFileName(filename);
        }
        writeFileAsString(new File(filename), text);
    }
    public static void writeFileAsString(String filename, String text,
            String encoding) {
        if (filename.startsWith(SERIALPREFIX)) {
            filename = serialFileName(filename);
        }
        writeFileAsString(new File(filename), text, encoding);
    }
    public static boolean exists(String path) {
        return new File(path).exists();
    }
    /**
     * Ensures that the directories for the file exist
     * @param f
     */
    public static void ensureWriteable(File f) {
        File parent = f.getParentFile();
        if (parent != null) {
            parent.mkdirs();
        }
    }
    public static String serialFileName(String serId) {
        if (serId.startsWith(SERIALPREFIX)) {
            serId = serId.substring(SERIALPREFIX.length());
        }
        return serialProperties().get(serId);
    }
    private static HashMap<String, String> serialProperties() {
        if (serial == null) {
            Properties serialP = PropertyLoader
                    .loadProperties("serialization");
            HashMap<String, String> serialM = new HashMap<String, String>();
            for (Map.Entry<Object, Object> e : serialP.entrySet()) {
                if (e.getKey() instanceof String
                        && e.getValue() instanceof String) {
                    serialM.put((String) e.getKey(), (String) e.getValue());
                } else {
                    System.err.println("Bad property: " + e);
                }
            }
            //do replacement on the properties
            serial = new HashMap<String, String>();
            for (Map.Entry<String, String> e : serialM.entrySet()) {
                String path = e.getValue();
                for (Map.Entry<String, String> oe : serialM.entrySet()) {
                    if (e.getKey() != oe.getKey()) {
                        path = path.replace("${" + oe.getKey() + "}",
                                oe.getValue());
                    }
                }
                serial.put(e.getKey(), path);
            }
        }
        return serial;
    }
}

Related

  1. writeFile(String filePath, String content)
  2. writeFileAsString(File file, String text)
  3. writeFileAsString(File file, String text, String encoding)
  4. writeFileAsString(String filename, String text)
  5. writeFileAsString(String filename, String text, String encoding)
  6. writeFileAtomically(File file, String content, String encoding)
  7. writeFileWithBom(File file, String content, String encoding)
  8. writeLargerTextFile(File file, List aLines)
  9. writeLineTextFile(File file, String data)