package com.jx.udiskdemo; java
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import com.jx.udiskdemo.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
android
//該類用顯示加載SDK上的文件
public class FileActivity extends Activity {
private String path;
private ListView lv;
ArrayList name;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.file);
lv = (ListView) findViewById(R.id.list);
name = new ArrayList();
// if (Environment.getExternalStorageState().equals(
// Environment.MEDIA_MOUNTED)) {
// File path = Environment.getExternalStorageDirectory();// 得到SD卡路徑
File path = new File("mnd/sta/樂航/");
File[] files = path.listFiles();// 讀取
getFileName(files);
// }
SimpleAdapter adapter = new SimpleAdapter(this, name, R.layout.file,
new String[] { "Name" }, new int[] { R.id.list });
lv.setAdapter(adapter);
for (int i = 0; i < name.size(); i++) {
Log.i("zeng", "list. name: " + name.get(i));
}
}
private void getFileName(File[] files) {
if (files != null) {// 先判斷目錄是否爲空,不然會報空指針
for (File file : files) {
if (file.isDirectory()) {
Log.i("zeng", "如果文件目錄。繼續讀1" + file.getName().toString()
+ file.getPath().toString());
getFileName(file.listFiles());
Log.i("zeng", "如果文件目錄。繼續讀2" + file.getName().toString()
+ file.getPath().toString());
} else {
String fileName = file.getName();
if (fileName.endsWith(".txt")) {
HashMap map = new HashMap();
String s = fileName.substring(0,
fileName.lastIndexOf(".")).toString();
Log.i("zeng", "文件名txt:: " + s);
map.put("Name", fileName.substring(0,
fileName.lastIndexOf(".")));
name.add(map);
}
}
}
}
}
} app
大俠們 這是一個讀取SDCard上的txt文件,路徑換成U盤的爲何就不能夠,大俠們指點指點
ide