subSequence方法和subString方法的差別
subSequence方法和subString方法的差別
根據JDK的文檔說明,String.subSequence隻是為了實作CharSequence接口上的同名方法而放在那裡的,其行為與String.subString一樣 傳回類型不一樣,subString傳回的是String,subSequence傳回的是實作了CharSequence接口的類,也就是說使用subSequence得到的結果, 隻能使用CharSequence接口中的方法。不過在String類中已經重寫了subSequence,調用subSequence方法,可以直接下轉為String對象。
ArrayList<File> list = new ArrayList<File>();
String path = "/mnt/sdcard/Movies/";
getFiles(list, path);
for (int i = 0; i < list.size(); i++) {
String mPicName = list.get(i).getName();
System.out.println("全明星: " + mPicName);
// 擷取字尾名與非字尾名
String substring = mPicName.substring(mPicName.lastIndexOf(".")+1);
System.out.println("字尾名: " + substring);
String string = mPicName.substring(0, mPicName.lastIndexOf("."));
Log.e("eee","檔案名 : " + string);
CharSequence sequence = mPicName.subSequence(0, mPicName.lastIndexOf("."));
Log.e("eee","檔案名 : " + sequence);
// boolean kz = mPicName.endsWith(".jpeg");
// if (kz) { //判斷是否為 JPEG圖檔
// Log.e("eee","檔案名 : " + mPicName);
// }
}
// 周遊檔案
private void getFiles(ArrayList<File> list, String filePath) {
File[] allFiles = new File(filePath).listFiles();
if (allFiles != null) { // 若檔案不為空,則周遊檔案長度
for (int i = 0; i < allFiles.length; i++) {
File file = allFiles[i];
if (file.isFile()) {
list.add(file);
}
}
}
}
2、
getFileName("/mnt/sdcard/Movies/");
public static Vector<String> getFileName(String filepath) {
Vector<String> vecFile = new Vector<String>();
File file = new File(filepath);
File[] subFile = file.listFiles();
for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
// 判斷是否為檔案夾
if (!subFile[iFileLength].isDirectory()) {
String filename = subFile[iFileLength].getName();
Log.e("eee","檔案名 : " + filename);
}
}
return vecFile;
}