Android Text File Write writeFile(String filepath, String text)

Here you can find the source of writeFile(String filepath, String text)

Description

write File

Declaration

public static void writeFile(String filepath, String text) 

Method Source Code

//package com.java2s;

import java.io.Closeable;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class Main {
    public static void writeFile(String filepath, String text) {
        OutputStreamWriter wr = null;
        try {/*from   w  w  w  .jav  a  2 s .  c o m*/
            File file = new File(filepath);
            if (!file.exists()) {
                file.getParentFile().mkdirs();
            }
            wr = new OutputStreamWriter(new FileOutputStream(filepath,
                    false), "UTF8");
            wr.write(text);
            wr.flush();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (wr != null) {
                try {
                    wr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void writeFile(InputStream inputStream,
            OutputStream outputStream) throws IOException {
        final byte[] buf = new byte[10240];
        int len;
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.flush();
        closeIOStream(outputStream);
        closeIOStream(inputStream);
    }

    public static void closeIOStream(Closeable stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. writeToFile(String textToWrite, String fileName)
  2. writeToFile(StringBuffer sb, String directory, String fileName)
  3. writeToFile(final String path, final String content)
  4. saveFile(String content, File file)
  5. writeFileSdcard(File file, String message)
  6. writeStringToFile(String text, String filePath)
  7. writeToSDFile(String directory, String file_name, String text)
  8. writeFileEnd(String filepath, String text)
  9. WriteFile(String file, String message)