java中如何删除本地文件夹以及文件

Python012

java中如何删除本地文件夹以及文件,第1张

删除文件(前提:文件夹为空以及InputStream和OutputStream等一些数据文件流关掉【close()】,否则文件无法删除)

//删除文件夹

public static void delFolder(String folderPath) {

try {

delAllFile(folderPath) //删除完里面所有内容

String filePath = folderPath

filePath = filePath.toString()

java.io.File myFilePath = new java.io.File(filePath)

myFilePath.delete() //删除空文件夹

} catch (Exception e) {

e.printStackTrace()

}

}

删除指定文件夹下的所有文件

public static boolean delAllFile(String path) {

boolean flag = false

File file = new File(path)

if (!file.exists()) {

return flag

}

if (!file.isDirectory()) {

return flag

}

String[] tempList = file.list()

File temp = null

for (int i = 0 i < tempList.length i++) {

if (path.endsWith(File.separator)) {

temp = new File(path + tempList[i])

} else {

temp = new File(path + File.separator + tempList[i])

}

if (temp.isFile()) {

temp.delete()

}

if (temp.isDirectory()) {

delAllFile(path + "/" + tempList[i])//先删除文件夹里面的文件

delFolder(path + "/" + tempList[i])//再删除空文件夹

flag = true

}

}

return flag

}

}

写了一个读取本地文件的方法, File file = new File(htmlFile)FileReader fr = new FileReader(file)BufferedReader br = new BufferedReader(fr)while((s=br.readLine())!=null){ al.add(s)} 在当前类写了main方法测试了一下是可行的, 但是页面某方法想调用该方法,不能实现。 总结问题是:只有放在static方法中可行,在其他地方调用都显示找不到指定文件。 文件结构: 把本地文件放在了web-inf的classes下了,相对路径写的(“/file.txt”)求教为啥static方法可以,其他地方调用不行,这个函数本身不是静态的啊。

对于音乐文件,文件名,标题,歌手名之类的信息是在音乐文件结构中固定位置存放,所以,按照结构读取就可以了。

例如mp3的。

mp3File = new MP3File("D:\\test.mp3")//封装好的类

MP3AudioHeader header = mp3File.getMP3AudioHeader()

System.out.println("时长: " + header.getTrackLength())//获得时长

System.out.println("比特率: " + header.getBitRate())//获得比特率

System.out.println("音轨长度: " + header.getTrackLength())//音轨长度

System.out.println("格式: " + header.getFormat())//格式,例 MPEG-1

System.out.println("声道: " + header.getChannels())//声道

System.out.println("采样率: " + header.getSampleRate())//采样率

System.out.println("MPEG: " + header.getMpegLayer())//MPEG

System.out.println("MP3起始字节: " + header.getMp3StartByte())//MP3起始字节