convert File Encoding And Get Text - Android File Input Output

Android examples for File Input Output:UTF File

Description

convert File Encoding And Get Text

Demo Code


//package com.java2s;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;

import android.os.Environment;
import android.util.Log;

public class Main {
    public static void convertCodeAndGetText(String str_filepath) {

        File file = new File(str_filepath);
        BufferedReader reader;/*from  w  w w  .j a  v  a  2  s  .c o  m*/
        try {
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream in = new BufferedInputStream(fis);
            in.mark(4);
            byte[] first3bytes = new byte[3];
            in.read(first3bytes);
            in.reset();
            Log.d("liudong", "first3bytes[0]=" + first3bytes[0]
                    + "first3bytes[1]=" + first3bytes[1]
                    + "first3bytes[2]=" + first3bytes[2]);
            if (first3bytes[0] == (byte) 0xEF
                    && first3bytes[1] == (byte) 0xBB
                    && first3bytes[2] == (byte) 0xBF) {// utf-8
                reader = new BufferedReader(new InputStreamReader(in,
                        "utf-8"));
            } else if (first3bytes[0] == (byte) 0xFF
                    && first3bytes[1] == (byte) 0xFE) {
                reader = new BufferedReader(new InputStreamReader(in,
                        "unicode"));
            } else if (first3bytes[0] == (byte) 0xFE
                    && first3bytes[1] == (byte) 0xFF) {
                reader = new BufferedReader(new InputStreamReader(in,
                        "utf-16be"));
            } else if (first3bytes[0] == (byte) 0xFF
                    && first3bytes[1] == (byte) 0xFF) {
                reader = new BufferedReader(new InputStreamReader(in,
                        "utf-16le"));
            } else {
                reader = new BufferedReader(
                        new InputStreamReader(in, "GBK"));
            }
            StringBuffer content = new StringBuffer();
            String str;
            String line_separator = System.getProperty("line.separator");
            while ((str = reader.readLine()) != null) {
                content.append(str + line_separator);
            }
            // Log.d("liudong", "content="+content);
            reader.close();
            File outFile = new File(
                    Environment.getExternalStorageDirectory()
                            + "/Subtitle.srt");
            if (outFile.exists()) {
                outFile.delete();
            }
            Writer ow = new OutputStreamWriter(new FileOutputStream(
                    Environment.getExternalStorageDirectory()
                            + "/Subtitle.srt"), "utf-8");
            ow.write(content.toString());
            ow.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials