java修改文件名
import java.io.*; public class test1 { public static void main(String[] args) { File file=new File("D:/gai.jpg"); //指定文件名及路徑 String name="123"; String filename=file.getAbsolutePath(); if(filename.indexOf(".")>=0) { filename = filename.substring(0,filename.lastIndexOf(".")); } file.renameTo(new File(name+".jpg")); //改名 }
}
1.創(chuàng)建文件夾 //import java.io.*; File myFolderPath = new File(%%1); try { if (!myFolderPath.exists()) { myFolderPath.mkdir(); } } catch (Exception e) { System.out.println("新建目錄操作出錯(cuò)"); e.printStackTrace(); }
2.創(chuàng)建文件 //import java.io.*; File myFilePath = new File(%%1); try { if (!myFilePath.exists()) { myFilePath.createNewFile(); } FileWriter resultFile = new FileWriter(myFilePath); PrintWriter myFile = new PrintWriter(resultFile); myFile.println(%%2); myFile.flush(); resultFile.close(); } catch (Exception e) { System.out.println("新建文件操作出錯(cuò)"); e.printStackTrace(); }
3.刪除文件 //import java.io.*; File myDelFile = new File(%%1); try { myDelFile.delete(); } catch (Exception e) { System.out.println("刪除文件操作出錯(cuò)"); e.printStackTrace(); }
4.刪除文件夾 /* import java.io.*; import java.util.*; */ LinkedList folderList = new LinkedList<String>(); folderList.add(%%1); while (folderList.size() > 0) { File file = new File(folderList.poll()); File[] files = file.listFiles(); ArrayList<File> fileList = new ArrayList<File>(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { folderList.add(files[i].getPath()); } else { fileList.add(files[i]); } } for (File f : fileList) { f.delete(); } } folderList = new LinkedList<String>(); folderList.add(%%1); while (folderList.size() > 0) { File file = new File(folderList.getLast()); if (file.delete()) { folderList.removeLast(); } else { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { folderList.add(files[i].getPath()); } } }
5.刪除一個(gè)文件下夾所有的文件夾 /* import java.io.*; private static LinkedList<String> folderList=null; */ File delfile=new File(%%1); File[] files=delfile.listFiles(); for(int i=0;i<files.length;i++){ if(files[i].isDirectory()){ if(!files[i].delete()){ folderList = new LinkedList<String>(); folderList.add(files[i]); while (folderList.size() > 0) {
|
· 2009-10-23 21:56
· 回復(fù)
|
href="##"
|
2樓
File file = new File(folderList.poll()); File[] files = file.listFiles(); ArrayList<File> fileList = new ArrayList<File>(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { folderList.add(files[i].getPath()); } else { fileList.add(files[i]); } } for (File f : fileList) { f.delete(); } } folderList = new LinkedList<String>(); folderList.add(files[i]); while (folderList.size() > 0) { File file = new File(folderList.getLast()); if (file.delete()) { folderList.removeLast(); } else { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { folderList.add(files[i].getPath()); } } } } } }
6.清空文件夾 //import java.io.*; File delfilefolder=new File(%%1); try { if (!delfilefolder.exists() && !delfilefolder.delete()){ LinkedList folderList = new LinkedList<String>(); folderList.add(delfilefolder); while (folderList.size() > 0) { File file = new File(folderList.poll()); File[] files = file.listFiles(); ArrayList<File> fileList = new ArrayList<File>(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { folderList.add(files[i].getPath()); } else { fileList.add(files[i]); } } for (File f : fileList) { f.delete(); } } folderList = new LinkedList<String>(); folderList.add(delfilefolder); while (folderList.size() > 0) { File file = new File(folderList.getLast()); if (file.delete()) { folderList.removeLast(); } else { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { folderList.add(files[i].getPath()); } } } } delfilefolder.mkdir(); } catch (Exception e) { System.out.println("清空目錄操作出錯(cuò)"); e.printStackTrace(); }
7.讀取文件 //import java.io.*; // 逐行讀取數(shù)據(jù) FileReader fr = new FileReader(%%1); BufferedReader br = new BufferedReader(fr); String %%2 = br.readLine(); while (%%2 != null) { %%3 %%2 = br.readLine(); } br.close(); fr.close();
|
· 2009-10-23 21:56
· 回復(fù)
|
·
· 無尾獸零號(hào)機(jī)
· 4位粉絲
·
|
3樓
9.寫入隨機(jī)文件 //import java.io.*; try { RandomAcessFile logFile=new RandomAcessFile(%%1,"rw"); long lg=logFile.length(); logFile.seek(%%2); logFile.writeByte(%%3); }catch(IOException ioe){ System.out.println("無法寫入文件:"+ioe.getMessage()); }
10.讀取文件屬性 //import java.io.*; // 文件屬性的取得 File af = new File(%%1); if (af.exists()) { System.out.println(f.getName() + "的屬性如下: 文件長(zhǎng)度為:" + f.length()); System.out.println(f.isFile() ? "是文件" : "不是文件"); System.out.println(f.isDirectory() ? "是目錄" : "不是目錄"); System.out.println(f.canRead() ? "可讀取" : "不"); System.out.println(f.canWrite() ? "是隱藏文件" : ""); System.out.println("文件夾的最后修改日期為:" + new Date(f.lastModified())); } else { System.out.println(f.getName() + "的屬性如下:"); System.out.println(f.isFile() ? "是文件" : "不是文件"); System.out.println(f.isDirectory() ? "是目錄" : "不是目錄"); System.out.println(f.canRead() ? "可讀取" : "不"); System.out.println(f.canWrite() ? "是隱藏文件" : ""); System.out.println("文件的最后修改日期為:" + new Date(f.lastModified())); } if(f.canRead()){ %%2 } if(f.canWrite()){ %%3 }
11.寫入屬性 //import java.io.*; File filereadonly=new File(%%1); try { boolean b=filereadonly.setReadOnly(); } catch (Exception e) { System.out.println("拒絕寫訪問:"+e.printStackTrace()); }
|
· 2009-10-24 15:19
· 回復(fù)
|
·
· 無尾獸零號(hào)機(jī)
· 4位粉絲
·
|
4樓
8.寫入文件 //import java.io.*; // 將數(shù)據(jù)寫入文件 try { FileWriter fw = new FileWriter(%%1); fw.write(%%2); fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); }
|
· 2009-10-24 15:19
· 回復(fù)
|
·
· 無尾獸零號(hào)機(jī)
· 4位粉絲
·
|
5樓
12.枚舉一個(gè)文件夾中的所有文件 /* import java.io.*; import java.util.*; */ LinkedList<String> folderList = new LinkedList<String>(); folderList.add(%%1); while (folderList.size() > 0) { File file = new File(folderList.poll()); File[] files = file.listFiles(); List<File> fileList = new ArrayList<File>(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { folderList.add(files[i].getPath()); } else { fileList.add(files[i]); } } for (File f : fileList) { %%2=f.getAbsoluteFile(); %%3 } }
13.復(fù)制文件夾 /* import java.io.*; import java.util.*; */ LinkedList<String> folderList = new LinkedList<String>(); folderList.add(%%1); LinkedList<String> folderList2 = new LinkedList<String>(); folderList2.add(%%2+ %%1.substring(%%1.lastIndexOf("\\"))); while (folderList.size() > 0) { (new File(folderList2.peek())).mkdirs(); // 如果文件夾不存在 則建立新文件夾 File folders = new File(folderList.peek()); String[] file = folders.list(); File temp = null; try { for (int i = 0; i < file.length; i++) { if (folderList.peek().endsWith(File.separator)) { temp = new File(folderList.peek() + File.separator + file[i]); } else { temp = new File(folderList.peek() + File.separator + file[i]); } if (temp.isFile()) { FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream( folderList2.peek() + File.separator + (temp.getName()).toString()); byte[] b = new byte[5120]; int len; while ((len = input.read(b)) != -1) { output.write(b, 0, len); } output.flush(); output.close(); input.close(); } if (temp.isDirectory()) {// 如果是子文件夾 for (File f : temp.listFiles()) { if (f.isDirectory()) { folderList.add(f.getPath()); folderList2.add(folderList2.peek() + File.separator + f.getName()); } } } } } catch (Exception e) { //System.out.println("復(fù)制整個(gè)文件夾內(nèi)容操作出錯(cuò)"); e.printStackTrace(); } folderList.removeFirst(); folderList2.removeFirst(); }
|
· 2009-10-24 15:19
· 回復(fù)
|
·
· 無尾獸零號(hào)機(jī)
· 4位粉絲
·
|
6樓
14.復(fù)制一個(gè)目錄下所有的文件夾到另一個(gè)文件夾下 /* import java.io.*; import java.util.*; */ File copyfolders=new File(%%1); File[] copyfoldersList=copyfolders.listFiles(); for(int k=0;k<copyfoldersList.length;k++){ if(copyfoldersList[k].isDirectory()){ List<String>folderList=new ArrayList<String>(); folderList.add(copyfoldersList[k].getPath()); List<String>folderList2=new ArrayList<String>(); folderList2.add(%%2+"/"+copyfoldersList[k].getName()); for(int j=0;j<folderList.size();j++){ (new File(folderList2.get(j))).mkdirs(); //如果文件夾不存在 則建立新文件夾 File folders=new File(folderList.get(j)); String[] file=folders.list(); File temp=null; try { for (int i = 0; i < file.length; i++) { if(folderList.get(j).endsWith(File.separator)){ temp=new File(folderList.get(j)+File.separator+file[i]); } else{ temp=new File(folderList.get(j)+File.separator+file[i]); } FileInputStream input = new FileInputStream(temp); if(temp.isFile()){ FileOutputStream output = new FileOutputStream(folderList2.get(j) + File.separator + (temp.getName()).toString()); byte[] b = new byte[5120]; int len; while ( (len = input.read(b)) != -1) { output.write(b, 0, len); } output.flush(); output.close(); input.close(); } if(temp.isDirectory()){//如果是子文件夾 folderList.add(folderList.get(j)+"/"+file[i]); folderList2.add(folderList2.get(j)+"/"+file[i]); } } } catch (Exception e) { System.out.println("復(fù)制整個(gè)文件夾內(nèi)容操作出錯(cuò)"); e.printStackTrace(); } } } }
|
· 2009-10-24 15:20
· 回復(fù)
|
·
· 無尾獸零號(hào)機(jī)
· 4位粉絲
·
|
7樓
43.ZIP壓縮文件夾 /* import java.io.*; import java.util.*; import java.util.zip.*; */ public static String zipFileProcess(ArrayList outputZipFileNameList, String outputZipNameAndPath) { ArrayList fileNames = new ArrayList(); ArrayList files = new ArrayList(); FileOutputStream fileOut = null; ZipOutputStream outputStream = null; FileInputStream fileIn = null; StringBuffer sb = new StringBuffer(outputZipNameAndPath); // FileInputStream fileIn =null; try { if (outputZipNameAndPath.indexOf(".zip") != -1) { outputZipNameAndPath = outputZipNameAndPath; } else { sb.append(".zip"); outputZipNameAndPath = sb.toString(); } fileOut = new FileOutputStream(outputZipNameAndPath); outputStream = new ZipOutputStream(fileOut); int outputZipFileNameListSize = 0; if (outputZipFileNameList != null) { outputZipFileNameListSize = outputZipFileNameList.size(); } for (int i = 0; i < outputZipFileNameListSize; i++) { File rootFile = new File(outputZipFileNameList.get(i).toString()); listFile(rootFile, fileNames, files, ""); } for (int loop = 0; loop < files.size(); loop++) { fileIn = new FileInputStream((File) files.get(loop)); outputStream.putNextEntry(new ZipEntry((String) fileNames.get(loop))); byte[] buffer = new byte[1024]; while (fileIn.read(buffer) != -1) { outputStream.write(buffer); } outputStream.closeEntry(); fileIn.close(); } return outputZipNameAndPath; } catch (IOException ioe) { return null; } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { } } if (fileIn != null) { try { fileIn.close(); } catch (IOException e) { } } } } private static void listFile(File parentFile, List nameList, List fileList, String directoryName) { if (parentFile.isDirectory()) { File[] files = parentFile.listFiles(); for (int loop = 0; loop < files.length; loop++) { listFile(files[loop], nameList, fileList, directoryName + parentFile.getName() + "/"); } } else { fileList.add(parentFile); nameList.add(directoryName + parentFile.getName()); } } ArrayList outputZipFileName=new ArrayList(); String savePath=""; int argSize = 0; savePath = %%1; outputZipFileName.add(%%2); ZipFileOther instance=new ZipFileOther(); instance.zipFileProcess(outputZipFileName,savePath);
|
import java.io.*; public class FileOperate { public FileOperate() { } /** * 新建目錄 * @param folderPath String 如 c:/fqf * @return boolean */ public void newFolder(String folderPath) { try { String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); if (!myFilePath.exists()) { myFilePath.mkdir(); } } catch (Exception e) { System.out.println("新建目錄操作出錯(cuò)"); e.printStackTrace(); } } /** * 新建文件 * @param filePathAndName String 文件路徑及名稱 如c:/fqf.txt * @param fileContent String 文件內(nèi)容 * @return boolean */ public void newFile(String filePathAndName, String fileContent) { try { String filePath = filePathAndName; filePath = filePath.toString(); File myFilePath = new File(filePath); if (!myFilePath.exists()) { myFilePath.createNewFile(); } FileWriter resultFile = new FileWriter(myFilePath); PrintWriter myFile = new PrintWriter(resultFile); String strContent = fileContent; myFile.println(strContent); resultFile.close(); } catch (Exception e) { System.out.println("新建目錄操作出錯(cuò)"); e.printStackTrace(); } } /** * 刪除文件 * @param filePathAndName String 文件路徑及名稱 如c:/fqf.txt * @param fileContent String * @return boolean */ public void delFile(String filePathAndName) { try { String filePath = filePathAndName; filePath = filePath.toString(); java.io.File myDelFile = new java.io.File(filePath); myDelFile.delete(); } catch (Exception e) { System.out.println("刪除文件操作出錯(cuò)"); e.printStackTrace(); } } /** * 刪除文件夾 * @param filePathAndName String 文件夾路徑及名稱 如c:/fqf * @param fileContent String * @return boolean */ public void delFolder(String folderPath) { try { delAllFile(folderPath); //刪除完里面所有內(nèi)容 String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); myFilePath.delete(); //刪除空文件夾 } catch (Exception e) { System.out.println("刪除文件夾操作出錯(cuò)"); e.printStackTrace(); } } /** * 刪除文件夾里面的所有文件 * @param path String 文件夾路徑 如 c:/fqf */ public void delAllFile(String path) { File file = new File(path); if (!file.exists()) { return; } if (!file.isDirectory()) { return; } 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]);//再刪除空文件夾 } } } /** * 復(fù)制單個(gè)文件 * @param oldPath String 原文件路徑 如:c:/fqf.txt * @param newPath String 復(fù)制后路徑 如:f:/fqf.txt * @return boolean */ public void copyFile(String oldPath, String newPath) { try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); if (oldfile.exists()) { //文件存在時(shí) InputStream inStream = new FileInputStream(oldPath); //讀入原文件 FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; int length; while ( (byteread = inStream.read(buffer)) != -1) { bytesum += byteread; //字節(jié)數(shù) 文件大小 System.out.println(bytesum); fs.write(buffer, 0, byteread); } inStream.close(); } } catch (Exception e) { System.out.println("復(fù)制單個(gè)文件操作出錯(cuò)"); e.printStackTrace(); } } /** * 復(fù)制整個(gè)文件夾內(nèi)容 * @param oldPath String 原文件路徑 如:c:/fqf * @param newPath String 復(fù)制后路徑 如:f:/fqf/ff * @return boolean */ public void copyFolder(String oldPath, String newPath) { try { (new File(newPath)).mkdirs(); //如果文件夾不存在 則建立新文件夾 File a=new File(oldPath); String[] file=a.list(); File temp=null; for (int i = 0; i < file.length; i++) { if(oldPath.endsWith(File.separator)){ temp=new File(oldPath+file[i]); } else{ temp=new File(oldPath+File.separator+file[i]); } if(temp.isFile()){ FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString()); byte[] b = new byte[1024 * 5]; int len; while ( (len = input.read(b)) != -1) { output.write(b, 0, len); } output.flush(); output.close(); input.close(); } if(temp.isDirectory()){//如果是子文件夾 copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]); } } } catch (Exception e) { System.out.println("復(fù)制整個(gè)文件夾內(nèi)容操作出錯(cuò)"); e.printStackTrace(); } } /** * 移動(dòng)文件到指定目錄 * @param oldPath String 如:c:/fqf.txt * @param newPath String 如:d:/fqf.txt */ public void moveFile(String oldPath, String newPath) { copyFile(oldPath, newPath); delFile(oldPath); } /** * 移動(dòng)文件到指定目錄 * @param oldPath String 如:c:/fqf.txt * @param newPath String 如:d:/fqf.txt */ public void moveFolder(String oldPath, String newPath) { copyFolder(oldPath, newPath); delFolder(oldPath); } }
|