我在下面的代碼(它列出帶有1個子文件夾,2個.mp4文件和1個.mp3文件的文件夾)中面臨着一個奇怪的Java行爲。我想ASIGN不同的圖標來的視頻和其他文件:Java if-else嵌套在循環中
private void fill(File f)
{
File[]dirs = f.listFiles();
this.setTitle("Current Dir: "+f.getName());
List<Item> dir = new ArrayList<Item>();
List<Item> fls = new ArrayList<Item>();
try{
for(File ff: dirs)
{
Date lastModDate = new Date(ff.lastModified());
DateFormat formater = DateFormat.getDateTimeInstance();
String date_modify = formater.format(lastModDate);
if(ff.isDirectory()){
File[] fbuf = ff.listFiles();
int buf = 0;
if(fbuf != null){
buf = fbuf.length;
}
else buf = 0;
String num_item = String.valueOf(buf);
if(buf == 0) num_item = num_item + " item";
else num_item = num_item + " items";
//String formated = lastModDate.toString();
dir.add(new Item(ff.getName(),num_item,date_modify,ff.getAbsolutePath(),"directory_icon"));
}
else
{
String ext = "";
ext=getFileExtension(ff);
Log.d("Zox","'"+ext+"'");
if(ext!="mp4"){
Log.d("Zox","'"+ext+"'");
Log.d("Zox","FU");
}
else {
fls.add(new Item(ff.getName(), ff.length() + " Byte", date_modify, ff.getAbsolutePath(), "file_icon"));
}
}
}
我得到以下輸出:
08-09 18:54:08.219 11057-11057/com.piandro d/ZOX:「 MP4'
08-09 18:54:08.219 11057-11057/com.piandro d/ZOX: 'MP4'
08-09 18:54:08.219 11057-11057/com.piandro d/ZOX :FU
08-09 18:54:08.219 11057-11057/com.piandro D/ZOX: 'MP4'
08-09 18:54:08.219 11057-11057/com.piandro d/ZOX: 'MP4'
08-09 18:54:08.219 11057-11057/COM。 piandro d/ZOX:FU
08-09 18:54:08.229 11057-11057/com.piandro d/ZOX: 'MP3'
08-09 18:54:08.229 11057-11057/COM。 piandro d/ZOX: 'MP3'
08-09 18:54:08.229 11057-11057/com.piandro d/ZOX:FU
關鍵代碼部分是:
String ext = "";
ext=getFileExtension(ff);
Log.d("Zox","'"+ext+"'");
if(ext!="mp4"){
Log.d("Zox","'"+ext+"'");
Log.d("Zox","FU");
}
else {
fls.add(new Item(ff.getName(), ff.length() + " Byte", date_modify, ff.getAbsolutePath(), "file_icon"));
}
條件和操作都是廢話,只是爲了調試目的。爲什麼Java不能識別字符串匹配?
ext.equals( 「MP4」)解決問題!謝謝你,最好! – Gruiberg