android读取sd卡上文件中的数据
从sd卡上的文件中读取数据
第1种方法:
public static String readFileMsg(String filePath) {
if (TextUtils.isEmpty(filePath)) {
return "";
}
BufferedReader reader = null;
try {
File file = new File(filePath);
if (!file.exists()) {
return "";
}
reader = new BufferedReader(new FileReader(file));
String line;
StringBuilder content = new StringBuilder();
while((line = reader.readLine()) != null) {
content.append(line);
}
return content.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "";
}
第2种方法:
public static String readFileMsg(String filePath) {
InputStream inStream = null;
BufferedReader reader = null;
try {
StringBuilder content = new StringBuilder();
File file = new File(filePath);
if (!file.exists()) {
return "";
}
inStream = new FileInputStream(file);
if (inStream != null) {
InputStreamReader inputReader = new InputStreamReader(inStream);
reader = new BufferedReader(inputReader);
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
}
reader.close();
return content.toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(inStream != null) {
try {
inStream.close();
} catch(IOException e) {
}
}
}
return "";
}