我正在使用Java!我用两个“Ö”标记的部分出现“无返回语句”错误,而在我用“// <----ÄÄÄ”标记的部分出现“无法访问的语句”错误。这部分代码应该删除一个包含文件的目录/文件夹,并且它本身就可以工作。但我似乎无法让它与我的其余代码一起使用!帮助将不胜感激!
通常,这是一个检查目录大小、删除目录及其内容并创建具有相同名称的新目录的程序。
$package delete.file.or.dir;
import java.io.*;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class DeleteFileOrDir{
public static void main (String [] args){
// The directory in question
File file = new File("C://123");
long fileSize = file.length();
System.out.println("File size in bytes is: " + fileSize);
System.out.println("File size in KB is : " + (double)fileSize/1024);
System.out.println("File size in MB is :" + (double)fileSize/(1024*1024));
//Bad yes no statement
JOptionPane pane = new JOptionPane(
"Y=Yes\nN=No");
Object[] options = new String[] { "Y", "N" };
pane.setOptions(options);
JDialog dialog = pane.createDialog(new JFrame(), "Dilaog");
dialog.show();
Object obj = pane.getValue();
int result = -1;
for (int k = 0; k < options.length; k++)
if (options[k].equals(obj))
result = k;
System.out.println("User's choice: " + result);
//Delete Directory with files
deleteDirectory(new File("C://123"));
}
//The first line of text just below is the first problem Ö
static public boolean deleteDirectory(File path) {
if( path.exists() ) {
File[] files = path.listFiles();
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
files[i].delete();
}
}
}
return( path.delete() );
File dir= new File("C://123"); // This gives me "unreachable statament Ä"
boolean isDirectoryCreated = dir.mkdir();
if(isDirectoryCreated)
System.out.println("Reset");
else
System.out.println("Error");
}
}