Android Text File Write writeFileAsString(String filename, String text, String encoding)

Here you can find the source of writeFileAsString(String filename, String text, String encoding)

Description

write File As String

License

Apache License

Declaration

public static void writeFileAsString(String filename, String text,
            String encoding) 

Method Source Code

/*//from  w ww  .j  a  v  a 2s .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 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);
    }
    /**
     * 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 fileName, String text, boolean isAppend)
  2. writeFile(String filePath, String content)
  3. writeFileAsString(File file, String text)
  4. writeFileAsString(File file, String text, String encoding)
  5. writeFileAsString(String filename, String text)
  6. writeFileAsStringWorldWritable(File file, String text)
  7. writeFileAtomically(File file, String content, String encoding)
  8. writeFileWithBom(File file, String content, String encoding)
  9. writeLargerTextFile(File file, List aLines)