最近在做个MP3播放器,出现中文乱码问题,在网上找了很多解决办法,我整理了出现乱码的点和解决方案,拿出来和大家共享一下

1.读取中文文件乱码解决方法

package com.apj.conv;

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.widget.TextView;

public class ConverActivity extends Activity {

private TextView textview ;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

textview = (TextView) findViewById(R.id.lrctext);

System.out.println("==============convertCodeAndGetText begin============== ");

///获得SDCard中文件的路径

String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator ;

String tochinese = convertCodeAndGetText(path+"a.txt");

System.out.println(tochinese);

System.out.println("==============cconvertCodeAndGetText end==============");

textview.setText(tochinese);

}

public String convertCodeAndGetText(String str_filepath) {//转变编码

File file = new File(str_filepath);

BufferedReader reader;

String text = "";

try {

FileInputStream fis = new FileInputStream(file);

BufferedInputStream in = new BufferedInputStream(fis);

in.mark(4);

byte[] first3bytes = new byte[3];

in.read(first3bytes);

in.reset();

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"));

}

String str = reader.readLine();

while (str != null) {

text = text + str + "\n";

str = reader.readLine();

}

reader.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return text;

}

}

package com.apj.conv;

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.widget.TextView;

public class ConverActivity extends Activity {

private TextView textview ;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

textview = (TextView) findViewById(R.id.lrctext);

System.out.println("===================convertCodeAndGetText begin=================== ");

///获得SDCard中文件的路径

String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator ;

String tochinese = convertCodeAndGetText(path+"a.txt");

System.out.println(tochinese);

System.out.println("===================cconvertCodeAndGetText end===================");

textview.setText(tochinese);

}

public String convertCodeAndGetText(String str_filepath) {// ת��

File file = new File(str_filepath);

BufferedReader reader;

String text = "";

try {

FileInputStream fis = new FileInputStream(file);

BufferedInputStream in = new BufferedInputStream(fis);

in.mark(4);

byte[] first3bytes = new byte[3];

in.read(first3bytes);

in.reset();

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"));

}

String str = reader.readLine();

while (str != null) {

text = text + str + "\n";

str = reader.readLine();

}

reader.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return text;

}

}

2. 连接网络读取文件内容中文乱码解决办法

URL myFileUrl = null;

myFileUrl = new URL(url);

HttpURLConnection conn;

conn = (HttpURLConnection) myFileUrl.openConnection();

conn.setDoInput(true);

conn.connect();

InputStream is = conn.getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(is,  "GB2312"));

sb = new StringBuffer();

String data = "";

while ((data = br.readLine()) != null) {

sb.append(data+"\n");

}

String result = sb.toString();

URL myFileUrl = null;

myFileUrl = new URL(url);

HttpURLConnection conn;

conn = (HttpURLConnection) myFileUrl.openConnection();

conn.setDoInput(true);

conn.connect();

InputStream is = conn.getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(is,

"GB2312"));

sb = new StringBuffer();

String data = "";

while ((data = br.readLine()) != null) {

sb.append(data+"\n");

}

String result = sb.toString();

3.读取网络文件中文名下载乱码解决办法 
1.先在设置服务器编码:找到Tomcat安装目录下的server.xml文件(Tomcat 6.0\conf\server.xml)。设置编码为UTF-8

<Connector port="8080" URIEncoding="UTF-8" redirectPort="8443" connectionTimeout="20000" protocol="HTTP/1.1"/>

2. android 中代码为:

try {

lrcUrl = "http://192.168.0.214/vote/mp3/" + URLEncoder.encode("中文.mp3","UTF-8");

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

int result1 = downFile(lrcUrl, "mp3/", "中文.mp3");

**

* 该函数返回整型( -1:代表下载文件出错 ;0:代表下载成功;1:代表文件已存在)

**/

public int downFile(String urlStr, String path, String fileName) {

InputStream inputStream = null;

try {

FileUtils fileUtils = new FileUtils();

if (fileUtils.isFileExist( fileName,path )) {

return 1;

} else {

inputStream = getInputStreamFromUrl(urlStr);

File resultFile = fileUtils.write2SDFromInput(path, fileName, inputStream);

if (resultFile == null) {

return -1;

}

}

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

return -1;

} finally {

try {

inputStream.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

return 0;

}

/**

* 根据URL得到输入流

*

* @param urlStr

* @return

* @throws MalformedURLException

* @throws IOException

*/

public InputStream getInputStreamFromUrl(String urlStr)

throws MalformedURLException, IOException {

url = new URL(urlStr);

HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();

InputStream inputStream = urlConn.getInputStream();

return inputStream;

}

Android读取中文文件乱码解决方法相关推荐

  1. 取出Cookie中的中文显示乱码解决方法。经验证第三种方法有效。

    取出Cookie中的中文显示乱码解决方法 HttpUtility.UrlEncode(Response.Cookies[ "newcookie "].Values);  HttpU ...

  2. Jenkins控制台中文输出乱码解决方法

    Jenkins控制台中文输出乱码解决方法 参考文章: (1)Jenkins控制台中文输出乱码解决方法 (2)https://www.cnblogs.com/dreamer-fish/p/1181204 ...

  3. vim 编辑器 打开GB2312、GBK文件乱码解决方法

    vim 编辑器 打开GB2312.GBK文件乱码解决方法 参考文章: (1)vim 编辑器 打开GB2312.GBK文件乱码解决方法 (2)https://www.cnblogs.com/suibbe ...

  4. 通过swagger下载的文件乱码解决方法,求解

    通过swagger下载的文件乱码解决方法,求解 参考文章: (1)通过swagger下载的文件乱码解决方法,求解 (2)https://www.cnblogs.com/shuiqian/p/10568 ...

  5. .sql导入中文显示乱码解决方法

    .sql导入中文显示乱码解决方法 参考文章: (1).sql导入中文显示乱码解决方法 (2)https://www.cnblogs.com/fnote/p/8051682.html 备忘一下.

  6. Excel 打开 CSV 文件乱码解决方法

    Excel 打开 CSV 文件乱码解决方法 - 膨胀的面包 https://blog.wangtwothree.com/code/157.html 经常接触数据的朋友,可能时不时会遇到这种情况,一份 ...

  7. requests.get()爬去中文网页乱码解决方法

    requests.get()爬去中文网页乱码解决方法 当我们使用requests.get()爬取百度首页时会发现,返回的html代码中的中文发生乱码. import requestsheaders = ...

  8. 从服务上下载下的日志文件,用UltraEdit打开时中文是乱码解决方法

    我刚才从服务器上下载来的日志文件,后缀是.log,用UltraEdit打开时中文全是乱码,解决方法: 1.高级--->配置---->常规 里的 "自动检测到UTF-8文件&quo ...

  9. python怎么读取中文文件-Python3 解决读取中文文件txt编码的问题

    问题描述 尝试用Python写一个Wordcloud的时候,出现了编码问题. 照着网上某些博客的说法添添改改后,结果是变成了"UnicodeDecodeError: "utf-8' ...

最新文章

  1. ifconfig没有命令 kali_新装的Linux系统没有ifconfig命令?教你一键找回
  2. JavaFx 实用小工具超 60+ 大集合
  3. 历届试题 错误票据(multiset)
  4. 开源 免费 java CMS - FreeCMS1.7 栏目管理
  5. Getting the right Exception Context from a Memory dump Fixed
  6. 小白能读懂的 《手把手教你学DSP(TMS320X281X)》第四章 2020-12-29 完整工程
  7. 国军标 软件测评 静态分析常见问题总结
  8. chrome 清理缓存
  9. 图像处理保研面试_北航信息与通信工程方向保研面试题集
  10. Noi 十连测 Zbox loves meizi
  11. python爬取腾讯新闻_Python采集腾讯新闻实例
  12. vue打包之后浏览器图标设置直接没有输出,vue打包之后浏览器图标不显示
  13. C语言笔记:进制转换与32位二进制IP地址转换十进制问题
  14. 区块链中的记账权问题
  15. 管理学中的 Expectancy Theory - 期望理论
  16. 基因表达半衰期 | mRNA Half-Life
  17. Web前端Table中的tr和td按百分比设定宽度和高度,当内容超出时Table变型解决
  18. 模拟退火——算法思想与实例
  19. (最详细)VueApp项目实战2 - - 首页制作
  20. 禁止电脑自动安装软件的方法

热门文章

  1. 总结篇--String、StringBuffer、StringBuilder
  2. CDH6.3.2环境搭建
  3. Android开发手册
  4. matlab工具箱使用手册,matlab机器人工具箱使用手册.pdf
  5. Arcgis经纬度到平面坐标转换
  6. opencv 的pyramid down函数verilog实现
  7. MTF模块 PX4 光流模块详细配置
  8. 三、HTML5之文本元素
  9. lattepanda安装linux系统,lattepanda alpha Ubuntu18.04下配置串口登录
  10. 耦合式是什么意思_什么是耦合、解耦