Android音乐播放器读取歌词.lrc文件乱码问题解决方法
2016-10-12 19:13
996 查看
最近在做Android音乐播放器,在读取本地歌词的时候,老是乱码,使我很郁闷:今天在网上看到一个文件转码的文章,彻底解决了我的乱码问题,这样我就不用自己去手动的转码lrc歌词文件你的编码了,现在与大家分享一下这一文章:
[java] view
plain copy
package com.qgmobile.utils;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 转换文件的编码格式
* @author yangchuxi
*
*/
public class ConvertFileCode {
public String converfile(String filepath){
System.out.println("ConvertFileCode--------->"+filepath);
File file=new File(filepath);
FileInputStream fis=null;
BufferedInputStream bis=null;
BufferedReader reader=null;
String text="";
try {
fis=new FileInputStream(file);
bis=new BufferedInputStream(fis);
bis.mark(4);
byte[] first3bytes=new byte[3];
// System.out.println("");
//找到文档的前三个字节并自动判断文档类型。
bis.read(first3bytes);
bis.reset();
if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB
&& first3bytes[2] == (byte) 0xBF) {// utf-8
reader = new BufferedReader(new InputStreamReader(bis, "utf-8"));
} else if (first3bytes[0] == (byte) 0xFF
&& first3bytes[1] == (byte) 0xFE) {
reader = new BufferedReader(
new InputStreamReader(bis, "unicode"));
} else if (first3bytes[0] == (byte) 0xFE
&& first3bytes[1] == (byte) 0xFF) {
reader = new BufferedReader(new InputStreamReader(bis,
"utf-16be"));
} else if (first3bytes[0] == (byte) 0xFF
&& first3bytes[1] == (byte) 0xFF) {
reader = new BufferedReader(new InputStreamReader(bis,
"utf-16le"));
} else {
reader = new BufferedReader(new InputStreamReader(bis, "GBK"));
}
String str = reader.readLine();
while (str != null) {
text = text + str + "/n";
str = reader.readLine();
}
System.out.println("text"+text);
} catch (Exception e) {
e.printStackTrace();
}finally{
if (fis!=null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return text;
}
}
[java] view
plain copy
package com.qgmobile.utils;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 转换文件的编码格式
* @author yangchuxi
*
*/
public class ConvertFileCode {
public String converfile(String filepath){
System.out.println("ConvertFileCode--------->"+filepath);
File file=new File(filepath);
FileInputStream fis=null;
BufferedInputStream bis=null;
BufferedReader reader=null;
String text="";
try {
fis=new FileInputStream(file);
bis=new BufferedInputStream(fis);
bis.mark(4);
byte[] first3bytes=new byte[3];
// System.out.println("");
//找到文档的前三个字节并自动判断文档类型。
bis.read(first3bytes);
bis.reset();
if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB
&& first3bytes[2] == (byte) 0xBF) {// utf-8
reader = new BufferedReader(new InputStreamReader(bis, "utf-8"));
} else if (first3bytes[0] == (byte) 0xFF
&& first3bytes[1] == (byte) 0xFE) {
reader = new BufferedReader(
new InputStreamReader(bis, "unicode"));
} else if (first3bytes[0] == (byte) 0xFE
&& first3bytes[1] == (byte) 0xFF) {
reader = new BufferedReader(new InputStreamReader(bis,
"utf-16be"));
} else if (first3bytes[0] == (byte) 0xFF
&& first3bytes[1] == (byte) 0xFF) {
reader = new BufferedReader(new InputStreamReader(bis,
"utf-16le"));
} else {
reader = new BufferedReader(new InputStreamReader(bis, "GBK"));
}
String str = reader.readLine();
while (str != null) {
text = text + str + "/n";
str = reader.readLine();
}
System.out.println("text"+text);
} catch (Exception e) {
e.printStackTrace();
}finally{
if (fis!=null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return text;
}
}
相关文章推荐
- IO 流读取文件时候出现乱码 文件编码格式问题 怎么转换解决方法
- java中读取excel文件中字符串乱码问题解决方法
- IO 流读取文件时候出现乱码 文件编码格式问题 怎么转换解决方法
- 安卓(JAVA)读取Mp3文件中的歌曲信息(及readLine()出现乱码问题的解决方法)
- Python实现的json文件读取及中文乱码显示问题解决方法
- Java:InputStream读取文本文件内容出现乱码问题的解决方法
- IO 流读取文件时候出现乱码 文件编码格式问题 怎么转换解决方法
- java中读取excel文件中字符串乱码问题解决方法
- Android读取本地json文件的方法(解决显示乱码问题)
- Java读取UTF-8格式txt文件第一行出现乱码——问号“?”及解决;Java读带有BOM的UTF-8文件乱码原因及解决方法
- JavaScript读取中文cookie时的乱码问题的解决方法
- 读取页面后输出到文件,解决乱码问题
- php读取淘宝数据包csv文件 unicode ucs-2 utf-16 中文乱码问题解决
- 关于iPod touch 4音乐歌词乱码的问题解决方法
- bash 中 while读取文件并通过 ssh执行命令出现的问题及解决方法
- Java解析DBF文件,错误Failed to parse Number: For input string: "-.---" 或读取不出数据或乱码问题都在这里解决
- ResourceBundle (读取properties文件及中文乱码解决方法)
- Java读取properties文件中文乱码问题的解决
- Andoird - 解决读取txt文件中文乱码问题
- java读取ANSI编码或者UTF-8编码文件乱码问题解决