您的位置:首页 > 移动开发 > Android开发

android 读取SD卡目录下文件

2013-07-29 10:40 543 查看
假如要读取SD卡-ceshi目录下的haha.txt文件中的内容,代码如下所示:

@Override
public void onCreate() {
super.onCreate();
readCatalog("haha.txt");
}

public void readCatalog(String fileName){
String catalogText = "";
File file = new File(Paths.getDataDirectoryPath() + fileName);
try {
FileInputStream input = new FileInputStream(file);
BufferedInputStream buffer=new BufferedInputStream(input);
BufferedReader reader = new BufferedReader(new InputStreamReader(buffer, "utf-8"));
String str = reader.readLine();
while (str != null) {
catalogText = catalogText + str + "\n";
str = reader.readLine();
}
reader.close();
Log.e("读sd卡文件", catalogText);

}catch (Exception e) {
e.printStackTrace();
}
}

public static String getDataDirectoryPath() {
return getDataDirectory().getPath() + File.separatorChar;
}

public static File getDataDirectory() {
File dir = new File(cardDirectory() + "data");
if (!dir.exists()) dir.mkdirs();
return dir;
}

public static String cardDirectory() {
return Environment.getExternalStorageDirectory().getPath() + "/ceshi/";
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android