您的位置:首页 > 理论基础 > 计算机网络

Android 网络字幕文件保证编码一致的文件读写

2016-06-18 15:10 423 查看
这是一段读入网络字幕文件并写出在本地的code。

<span style="font-size:24px;">    private String writeToLocalPath(String url){
String tmpPath = null;
String subtitleLocalPath = null;
if (Tools.isSambaPlaybackUrl(url)) {
Log.i(TAG,"HttpBean.setmSmbUrl url:"+url);
HttpBean.setmSmbUrl(url);
Log.i(TAG,"before convertSambaToHttpUrl's imagePath:"+url);
String sambaPath = HttpBean.convertSambaToHttpUrl(url);
Log.i(TAG,"convertSambaToHttpUrl's result sambaPath:"+sambaPath);
tmpPath = sambaPath;
} else {
tmpPath = url;
}
final String realPath  = tmpPath;
if (Tools.isNetPlayback(realPath)) {
try {
is = new URL(realPath).openStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
File mFile = new File(realPath);
is = new FileInputStream(mFile);
} catch (IOException e) {
e.printStackTrace();
}

}
if (is == null) {
Log.i(TAG,"is is null");
return null;
}
String charset = GetCharset(realPath);
try {
mInputStreamReader = new InputStreamReader(is,charset);
}catch (Exception e) {
e.printStackTrace();
}
// One line read

try {
mBufferedReader = new BufferedReader(mInputStreamReader);
char byteArray [] = new char[1024*8+1];
int readNum = 0;
int offset = 0;

int index = realPath.lastIndexOf(".");
String postFix = realPath.substring(index,realPath.length());
subtitleLocalPath = "/data/subtitle"+postFix;
File outPutLocalSubtitleFile = new File(subtitleLocalPath);
mOutputStreamWriter = new OutputStreamWriter(new FileOutputStream(outPutLocalSubtitleFile),charset);
mBufferedWriter = new BufferedWriter(mOutputStreamWriter);
while ((readNum = mBufferedReader.read(byteArray,0,1024*8))>0){
mBufferedWriter.write(byteArray,0,readNum);
offset += readNum;
Log.i(TAG,"andrew_read_write:"+readNum+","+offset);
}

mBufferedReader.close();
mBufferedWriter.close();
mInputStreamReader.close();
mOutputStreamWriter.close();
if (is!=null) {
is.close();
}
if (isForGetCharset!=null) {
isForGetCharset.close();
}
} catch (FileNotFoundException e){
e.printStackTrace();

} catch (IOException e){
e.printStackTrace();
}
try {
String command = "chmod 777 " + subtitleLocalPath;
Log.i(TAG, "command = " + command);
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command);
} catch (IOException e) {
Log.i(TAG,"chmod fail!!!!");
e.printStackTrace();
}
return subtitleLocalPath;
}</span>


通过

<span style="font-size:24px;">    public String GetCharset(final String filePath) {
String charset = "GBK";
byte[] first3Bytes = new byte[3];
try {
boolean checked = false;
if (bis != null) {
bis.close();
}
if (Tools.isNetPlayback(filePath)) {
try {
isForGetCharset = new URL(filePath).openStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (isForGetCharset == null) {
Log.i(TAG,"isForGetCharset is null");
return null;
}
bis = new BufferedInputStream(isForGetCharset);
} else {
File file = new File(filePath);
bis = new BufferedInputStream(new FileInputStream(file));
}
// bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1)
return charset;
if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
charset = "UTF-16LE";
checked = true;
} else if (first3Bytes[0] == (byte) 0xFE
&& first3Bytes[1] == (byte) 0xFF) {
charset = "UTF-16BE";
checked = true;
} else if (first3Bytes[0] == (byte) 0xEF
&& first3Bytes[1] == (byte) 0xBB
&& first3Bytes[2] == (byte) 0xBF) {
charset = "UTF-8";
checked = true;
} else if (first3Bytes[0] == (byte) 0xFF
&& first3Bytes[1] == (byte) 0xFE) {
charset = "unicode";
checked = true;
} else if (first3Bytes[0] == (byte) 0x5B
&& first3Bytes[1] == (byte) 0x30) {
charset = "ISO8859-1";
checked = true;
} else {
charset = "GBK";
checked = true;
}
// bis.reset();
if (!checked) {
while ((read = bis.read()) != -1) {
if (read >= 0xF0)
break;
if (0x80 <= read && read <= 0xBF)
break;
if (0xC0 <= read && read <= 0xDF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF)
continue;
else
break;
} else if (0xE0 <= read && read <= 0xEF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
charset = "UTF-8";
break;
} else
break;
} else
break;
}
}
}
bis.close();
} catch (Exception e) {
e.printStackTrace();
try {
bis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
return charset;
}</span>


(1)首先获取网络文件的文件流
主要是通过OpenStream获取到InputStream对象

        if (Tools.isNetPlayback(realPath)) {

            try {

                is = new URL(realPath).openStream();

            } catch (MalformedURLException e) {

                e.printStackTrace();

            } catch (IOException e) {

                e.printStackTrace();

            }

        } else {

            try {

                File mFile = new File(realPath);

                is = new FileInputStream(mFile);

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

(2)为了加快读写速度使用字符读写器,需要使用字节流转字符流的转化类:InputStreamReader

使用获取到文件的编码来初始化对象,这样才能保证按照文件的真正编码方式读出来。

try {

        mInputStreamReader = new InputStreamReader(is,charset);

      }    catch (Exception e) {

                e.printStackTrace();

      }

(3)用InputStreamReader对象初始化一个字符读入类对象

mBufferedReader = new BufferedReader(mInputStreamReader);

(4)创建一个字符写出类对象

先根据本地路径创建文件,然后用文件创建字节写出类对象,然后使用读写文件时缓存下来的编码方式

以此来初始化字符写出类对象

    subtitleLocalPath = "/data/subtitle"+postFix;

            File outPutLocalSubtitleFile = new File(subtitleLocalPath);

            mOutputStreamWriter = new OutputStreamWriter(new FileOutputStream(outPutLocalSubtitleFile),charset);

(5)然后就可以边读边写文件了

读入多少字符,就写出多少字符。

while ((readNum = mBufferedReader.read(byteArray,0,1024*8))>0){

                mBufferedWriter.write(byteArray,0,readNum);

                offset += readNum;

                Log.i(TAG,"andrew_read_write:"+readNum+","+offset);

            }

(6)最后close掉字符流,字节流

mBufferedReader.close();

            mBufferedWriter.close();

            mInputStreamReader.close();

            mOutputStreamWriter.close();

            if (is!=null) {

                is.close();

            }

            if (isForGetCharset!=null) {

                isForGetCharset.close();

            }

(7)再说下怎么识别文件编码:

主要是根据文件的头三个字符进行分类判断。

不过网上应该有很多这样的例子,随便抄QAQ

结尾附上一篇总结的不错的文章:http://blog.csdn.net/lanhuzi9999/article/details/31389963
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 文件读写