Androidメモリとsdカードのtxtファイルを読み込む
1535 ワード
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
readFromMemery("myFile.txt");
String name = android.os.Environment.getExternalStorageDirectory()
+ "/myFile.txt";
readFormSDcard(name);
}
/**
* @description sd txt
* @param path
*
*/
private void readFormSDcard(String path) {
if (!new File(path).exists()) {
Log.i("tag", " ");
System.out.println(" ");
return;
} else {
try {
FileInputStream fis = new FileInputStream(path);
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(new InputStreamReader(
fis));
StringBuilder sb = new StringBuilder("");
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
Log.i("tag", sb.toString());
System.out.println(sb.toString());
} catch (Exception e) {
Log.i("tag", " !");
System.out.println(e);
}
}
}
/**
* @description txt
* @param path
*
*/
private void readFromMemery(String path) {
try {
InputStream is = openFileInput(path);
byte[] buffer = new byte[100];
int byteCount = is.read(buffer);
String str = new String(buffer, 0, byteCount, "utf-8");
Log.i("tag", str);
} catch (Exception e) {
Log.i("tag", " !");
}
}