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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android 读取sd卡的txt并去除乱码
共38个文件
class:13个
java:7个
png:6个
5星 · 超过95%的资源 需积分: 19 95 下载量 196 浏览量
2015-05-21
13:37:13
上传
评论
收藏 78KB ZIP 举报
温馨提示
android 读取sd卡的txt并去除乱码 参考很多其他的读取操作,有时会有乱码的情况发生,所以这里需要一个自己判断字符,再读取 原文地址:http://blog.csdn.net/z13580130369/article/details/45890525
资源推荐
资源详情
资源评论
收起资源包目录
SDCardReadAndWrite.zip (38个子文件)
AboutSDCardReadAndWrite
.project 859B
project.properties 648B
src
cn
test
Manager
FileHelper.java 5KB
DataManager.java 1KB
Question.java 472B
data
DateManagerTest.java 2KB
AboutDateActivity.java 2KB
AndroidManifest.xml 1KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 3KB
layout
main.xml 2KB
drawable-xhdpi
values
strings.xml 476B
.settings
org.eclipse.jdt.core.prefs 177B
org.eclipse.core.resources.prefs 88B
assets
gen
cn
lee
data
BuildConfig.java 153B
R.java 1KB
.classpath 475B
bin
resources.ap_ 12KB
classes
cn
test
Manager
DataManager.class 1KB
Question.class 921B
FileHelper.class 5KB
data
AboutDateActivity$1.class 1KB
DateManagerTest.class 1KB
AboutDateActivity.class 2KB
lee
data
R$layout.class 373B
BuildConfig.class 331B
R$drawable.class 379B
R$attr.class 319B
R$id.class 636B
R.class 482B
R$string.class 605B
AndroidManifest.xml 1KB
dexedLibs
res
crunch
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 2KB
AboutSDCardReadAndWrite.apk 19KB
classes.dex 11KB
共 38 条
- 1
资源评论
- shuaizai1002015-11-18还行,就是稍有点复杂了
- liuyaoliuy2015-09-02不错,适应读取各种编码格式
mczhang13
- 粉丝: 12
- 资源: 34
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功