您好我正在制作一个语音转换器应用程序,我必须在sdcard中删除一些添加的pcm文件。保存歌曲的格式为: name.pcm
File f = new File(filepath + "/" + dir.getName() , SelectedFileOfListView + ".pcm");
f.delete();
其中SelectedFileOfListView是所选文件的名称。以下列出了目录中的文件。
dir = new File(filepath,"Recordings");
if(!dir.exists()){
dir.mkdirs();
}
File tempFile = new File(filepath,"hi");
if(tempFile.exists())
tempFile.delete();
file = new File(filepath + "/" + dir.getName() , "test.pcm");
// check list of files in the directory
files = dir.list();
if (files == null) {
list.add("No Recordings saved yet.");
} else {
list.clear();
for (int i = 0; i < files.length; ++i) {
list.add(files[i]);
}
}
答案 0 :(得分:3)
因此继承了所选文件删除的代码
File f = new File(PATH_TO_DIRECTORY + "/" + SELECTED_FILE);
if (f!=null && f.exists()){
//delete it
f.delete();
}
答案 1 :(得分:1)
如果要删除名为扩展名为.PCM
的文件你应该使用带有filenamefilter接口的list函数
像这样public static String[] getListOfFiles(String directory) {
String path = directory;
File f = new File(path);
String[] list_names;
// ArrayAdapter<String> adapter=new
// ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice,android.R.id.text1,list_names);
list_names = f.list(new FilenameFilter() {
public boolean accept(File fileDescriptor, String filename) {
// TODO Auto-generated method stub
if (filename.endsWith(".pcm"))
return true;
return false;
}
});
return list_names;
}
如果list_names!= null表示存在该文件,那么你将其删除
否则 什么都不做或只是出现祝酒
希望它可以帮到你。