android 读取sd卡的txt并去除乱码

需要从sd卡读取txt
首先得添加权限 在AndroidManifest.xml
<!-- 允许程序访问外部存储设备 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 允许创建和删除外部存储设备的文件 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />


参考很多其他的读取操作,有时会有乱码的情况发生,所以这里需要一个自己判断字符,再读取

创建一个读写类 FileHelper
package cn.test.Manager;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.R.integer;
import android.content.Context;
import android.os.Environment;

public class FileHelper {

	private Context context;

	/** SD卡是否存在 **/

	private boolean hasSD = false;

	/** SD卡的路径 **/

	private String SDPATH;

	/** 当前程序包的路径 **/

	private String FILESPATH;

	public FileHelper(Context context) {

		this.context = context;

		hasSD = Environment.getExternalStorageState().equals(

		android.os.Environment.MEDIA_MOUNTED);

		SDPATH = Environment.getExternalStorageDirectory().getPath();

		FILESPATH = this.context.getFilesDir().getPath();

	}

	/**
	 * 
	 * 在SD卡上创建文件
	 * 
	 * 
	 * 
	 * @throws IOException
	 */

	public File createSDFile(String fileName) throws IOException {

		File file = new File(SDPATH + "//" + fileName);

		if (!file.exists()) {

			file.createNewFile();

		}

		return file;

	}

	/**
	 * 
	 * 删除SD卡上的文件
	 * 
	 * 
	 * 
	 * @param fileName
	 */

	public boolean deleteSDFile(String fileName) {

		File file = new File(SDPATH + "//" + fileName);

		if (file == null || !file.exists() || file.isDirectory())

			return false;

		return file.delete();

	}

	/**
	 * 写入内容到SD卡中的txt文本中 str为内容
	 */

	public void writeSDFile(String str, String fileName) {
		try {
			FileWriter fw = new FileWriter(SDPATH + "//" + fileName);
			File f = new File(SDPATH + "//" + fileName);
			fw.write(str);

			FileOutputStream os = new FileOutputStream(f);
			DataOutputStream out = new DataOutputStream(os);
			out.writeShort(2);
			out.writeUTF("");
			System.out.println(out);
			fw.flush();
			fw.close();
			System.out.println(fw);
		} catch (Exception e) {
		}
	}

	/**
	 * 
	 * 读取SD卡中文本文件
	 * 
	 * 
	 * 
	 * @param fileName
	 * 
	 * @return
	 */

	public String readSDFile(String fileName) {

		File file = new File(SDPATH + "//" + fileName);
		BufferedReader reader;
		String text = "";
		try {
			// FileReader f_reader = new FileReader(file);
			// BufferedReader reader = new BufferedReader(f_reader);
			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();
			int index = 0;
			int line  =0;
			List< Question> questions = new ArrayList< Question>();
			while (str != null) {
				while (line<3) {
					text = text + str + "\r\n";
					str = reader.readLine();
					line++;
				}
				line = 0; 
				Question question = new Question();
				String []num = text.split("\r\n");
				for (int i = 0; i < num.length; i++) {
					
					if (i % 3 == 0) {
						question.setId(index);
					}
					if (i % 3 == 2) {
						question.setQuestion(num[i]);
					}
					if (i % 3 == 1) {
						question.setAnwer(num[i]);
					}
					
				}
				questions.add(question);
				index++;
			}
			reader.close();
			
			for (int i = 0; i < questions.size(); i++) {
				System.out.println("---index---"+i +"----"+questions.get(i).getQuestion() +"anwer--"+questions.get(i).getAnwer());
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return text;

	}

	public String getFILESPATH() {

		return FILESPATH;

	}

	public String getSDPATH() {

		return SDPATH;

	}

	public boolean hasSD() {

		return hasSD;

	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值