2015-01-20 6 views
3

현재와 파일에 대한 자산 폴더에서 파일 이름 가져 오기 : 나에게 자산 폴더에있는 모든 파일 이름의 ArrayList를 제공만이 코드가 확장자가 .txt

ArrayList<String> items = new ArrayList<String>(); 
       AssetManager assetManager = getApplicationContext().getAssets(); 
       try { 
        items.addAll(Arrays.asList(assetManager.list(""))); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

합니다. 그러나 arraylist는 확장명이 .txt 인 파일의 파일 이름 만 가지고 각 항목 이름에서 .txt를 제거 할 수 있도록 필터링해야합니다.

그래서 현재 코드가 될 것이다 :

test.txt 
pi.txt 
sounds 
hippo.png 
square.xml 
seven.txt 

을하지만 ArrayList의 내용으로해야하는 경우 다음과 같습니다

test 
pi 
seven 

답변

8

그래서 당신이 할 필요가

ArrayList<String> items = new ArrayList<String>(); 
AssetManager assetManager = getApplicationContext().getAssets(); 
for (String file : assetManager.list("")) { 
    if (file.endsWith(".txt")) 
     items.add(file); 
} 

파일 이름에서 .txt 확장자를 제거하려면 다음을 수행하십시오.

... 
    items.add(file.replaceAll(".txt$", "")); 
...