如何从外部存储目录输出没有扩展名的文件?

时间:2015-04-29 03:07:09

标签: java android

我在堆栈上查看了类似的问题,并且找不到路上我特定碰撞的答案是不成功的。我会向任何可以给我解决方案的人捐款。

我试图在没有扩展名的情况下从外部存储目录显示内容(.txt文件)。我已导入(apache.commons.io)。

ArrayAdapter mArrayAdapter;
ListView listView;
String fileNames[];
ArrayList<String> nameArray;



protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    this.setContentView(R.layout.activity_read_note_menu);

    listView = (ListView) this.findViewById(R.id.readListView);
    fileNames = new File(String.valueOf(Environment.getExternalStorageDirectory().getAbsolutePath()) + "/Notes").list();

    nameArray = new ArrayList<>();

         for (String name: fileNames){
            name = name.substring(0, name.indexOf("."));
            nameArray.add(name);
            Log.d("Files", "FileName:" + name);

    }

    mArrayAdapter = new ArrayAdapter<>(this, R.layout.list_item_1, nameArray);
    mArrayAdapter.sort(new Comparator<String>() {

        public int compare(String li1, String li2) {

            return li1.compareTo(li2);
        }

    });

    listView.setAdapter(mArrayAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView adapterView, View view, int i, long l) {
            String string = (String) listView.getItemAtPosition(i);
            Intent intent = new Intent(ReadNoteMenu.this, ReadNote.class);
            intent.putExtra(EXTRA_MESSAGE, string);
            ReadNoteMenu.this.startActivity(intent);

            finish();

        }

    });

1 个答案:

答案 0 :(得分:1)

确保从以下代码中获取所有文件列表:

String fileNames[] = new  File(String.valueOf(Environment.getExternalStorageDirectory().getAbsolutePath()) + "/Notes").list();

获得所有文件列表后,从文件名中删除扩展名并将该名称存储在数组中。

ArrayList<String> nameArray = new ArrayList<String>();

for (String name: fileNames){
   name = name.substring(0, name.indexOf("."));
   nameArray.add(name);
   Log.d("Files", "FileName:" + name);
}

请通过适配器中的nameArray完成工作,如下所示:

mArrayAdapter = new ArrayAdapter(this, R.layout.list_item_1, nameArray);