1.判断路径是文件还是目录
File subDir=new File("c:\\mp3"); if(subDir.isDirectory()){ // 是目录 } File mp3=new File("c:\\mp3\\avemaria.mp3"); if(mp3.isFile()){ // 是文件 }
2.列出目录下的文件和子目录
File dir = new File(fromDir); String[] children = dir.list(); for (int i=0; i<children.length; i++) { String filename = children[i]; ... }
3.文件拷贝
public static void copyFile(File sourceFile, File targetFile) throws IOException { BufferedInputStream inBuff = null; BufferedOutputStream outBuff = null; try { // 新建文件输入流并对它进行缓冲 inBuff = new BufferedInputStream(new FileInputStream(sourceFile)); // 新建文件输出流并对它进行缓冲 outBuff = new BufferedOutputStream(new FileOutputStream(targetFile)); // 缓冲数组 byte[] b = new byte[1024 * 5]; int len; while ((len = inBuff.read(b)) != -1) { outBuff.write(b, 0, len); } // 刷新此缓冲的输出流 outBuff.flush(); } finally { // 关闭流 if (inBuff != null) inBuff.close(); if (outBuff != null) outBuff.close(); } }
4.取得操作系统的临时目录
String folder=System.getProperty("java.io.tmpdir");
时间: 2024-11-05 11:27:59