#Java檔案操作大全
阿新 • • 發佈:2019-02-09
//1.建立資料夾 //import java.io.*; File myFolderPath = new File(str1); try { if (!myFolderPath.exists()) { myFolderPath.mkdir(); } } catch (Exception e) { System.out.println("新建目錄操作出錯"); e.printStackTrace(); } //2.建立檔案 //import java.io.*; File myFilePath = new File(str1); try { if (!myFilePath.exists()) { myFilePath.createNewFile(); } FileWriter resultFile = new FileWriter(myFilePath); PrintWriter myFile = new PrintWriter(resultFile); myFile.println(str2); resultFile.close(); } catch (Exception e) { System.out.println("新建檔案操作出錯"); e.printStackTrace(); } //3.刪除檔案 //import java.io.*; File myDelFile = new File(str1); try { myDelFile.delete(); } catch (Exception e) { System.out.println("刪除檔案操作出錯"); e.printStackTrace(); } //4.刪除資料夾 //import java.io.*; File delFolderPath = new File(str1); try { delFolderPath.delete(); //刪除空資料夾 } catch (Exception e) { System.out.println("刪除資料夾操作出錯"); e.printStackTrace(); } //5.刪除一個檔案下夾所有的資料夾 //import java.io.*; File delfile=new File(str1); File[] files=delfile.listFiles(); for(int i=0;i<files.length;i++){ if(files[i].isDirectory()){ files[i].delete(); } } //6.清空資料夾 //import java.io.*; File delfilefolder=new File(str1); try { if (!delfilefolder.exists()) { delfilefolder.delete(); } delfilefolder.mkdir(); } catch (Exception e) { System.out.println("清空目錄操作出錯"); e.printStackTrace(); } //7.讀取檔案 //import java.io.*; // 逐行讀取資料 FileReader fr = new FileReader(str1); BufferedReader br = new BufferedReader(fr); String str2 = br.readLine(); while (str2 != null) { str3 str2 = br.readLine(); } br.close(); fr.close(); //8.寫入檔案 //import java.io.*; // 將資料寫入檔案 try { FileWriter fw = new FileWriter(str1); fw.write(str2); fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } //9.寫入隨機檔案 //import java.io.*; try { RandomAcessFile logFile=new RandomAcessFile(str1,"rw"); long lg=logFile.length(); logFile.seek(str2); logFile.writeByte(str3); }catch(IOException ioe){ System.out.println("無法寫入檔案:"+ioe.getMessage()); } //10.讀取檔案屬性 //import java.io.*; // 檔案屬性的取得 File f = new File(str1); if (af.exists()) { System.out.println(f.getName() + "的屬性如下: 檔案長度為:" + 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()){ str2 } if(f.canWrite()){ str3 } //11.寫入屬性 //import java.io.*; File filereadonly=new File(str1); try { boolean b=filereadonly.setReadOnly(); } catch (Exception e) { System.out.println("拒絕寫訪問:"+e.printStackTrace()); } //12.列舉一個資料夾中的所有檔案 //import java.io.*; //import java.util.*; LinkedList<String> folderList = new LinkedList<String>(); folderList.add(str1); while (folderList.size() > 0) { File file = new File(folderList.peek()); folderList.removeLast(); 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) { str2=f.getAbsoluteFile(); str3 } } //13.複製資料夾 //import java.io.*; //import java.util.*; LinkedList<String> folderList = new LinkedList<String>(); folderList.add(str1); LinkedList<String> folderList2 = new LinkedList<String>(); folderList2.add(str2+ str1.substring(str1.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("複製整個資料夾內容操作出錯"); e.printStackTrace(); } folderList.removeFirst(); folderList2.removeFirst(); } //14.複製一個資料夾下所有的資料夾到另一個資料夾下 //import java.io.*; //import java.util.*; File copyfolders=new File(str1); File[] copyfoldersList=copyfolders.listFiles(); for(int k=0;k<copyfoldersList.length;k++){ if(copyfoldersList[k].isDirectory()){ ArrayList<String>folderList=new ArrayList<String>(); folderList.add(copyfoldersList[k].getPath()); ArrayList<String>folderList2=new ArrayList<String>(); folderList2.add(str2+"/"+copyfoldersList[k].getName()); for(int j=0;j<folderList.length;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[i]); } else { temp=new File(folderList.get(j)+"/"+File.separator+file[i]); } FileInputStream input = new FileInputStream(temp); if(temp.isFile()){ FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream(folderList2.get(j) + "/" + (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("複製整個資料夾內容操作出錯"); e.printStackTrace(); } } } } //15.移動資料夾 //import java.io.*; //import java.util.*; LinkedList<String> folderList = new LinkedList<String>(); folderList.add(str1); LinkedList<String> folderList2 = new LinkedList<String>(); folderList2.add(str2 + str1.substring(str1.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.delete()) System.out.println("刪除單個檔案操作出錯!"); } 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("複製整個資料夾內容操作出錯"); e.printStackTrace(); } folderList.removeFirst(); folderList2.removeFirst(); } File f = new File(str1); if (!f.delete()) { for (File file : f.listFiles()) { if (file.list().length == 0) { System.out.println(file.getPath()); file.delete(); } } } //16.移動一個資料夾下所有的資料夾到另一個目錄下 //import java.io.*; //import java.util.*; File movefolders=new File(str1); File[] movefoldersList=movefolders.listFiles(); for(int k=0;k<movefoldersList.length;k++){ if(movefoldersList[k].isDirectory()){ ArrayList<String>folderList=new ArrayList<String>(); folderList.add(movefoldersList[k].getPath()); ArrayList<String>folderList2=new ArrayList<String>(); folderList2.add(str2+"/"+movefoldersList[k].getName()); for(int j=0;j<folderList.length;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[i]); } else{ temp=new File(folderList.get(j)+"/"+File.separator+file[i]); } FileInputStream input = new FileInputStream(temp); if(temp.isFile()){ FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream(folderList2.get(j) + "/" + (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(); temp.delete(); } if(temp.isDirectory()){//如果是子資料夾 folderList.add(folderList.get(j)+"/"+file[i]); folderList2.add(folderList2.get(j)+"/"+file[i]); } } } catch (Exception e) { System.out.println("複製整個資料夾內容操作出錯"); e.printStackTrace(); } } movefoldersList[k].delete(); } } //17.以一個資料夾的框架在另一個目錄建立資料夾和空檔案 //import java.io.*; //import java.util.*; boolean b=false;//不建立空檔案 ArrayList<String>folderList=new ArrayList<String>(); folderList.add(str1); ArrayList<String>folderList2=new ArrayList<String>(); folderList2.add(str2); for(int j=0;j<folderList.length;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[i]); } else{ temp=new File(folderList.get(j)+"/"+File.separator+file[i]); } FileInputStream input = new FileInputStream(temp); if(temp.isFile()){ if (b) temp.createNewFile(); } if(temp.isDirectory()){//如果是子資料夾 folderList.add(folderList.get(j)+"/"+file[i]); folderList2.add(folderList2.get(j)+"/"+file[i]); } } } catch (Exception e) { System.out.println("複製整個資料夾內容操作出錯"); e.printStackTrace(); } } //18.複製檔案 //import java.io.*; int bytesum = 0; int byteread = 0; File oldfile = new File(str1); try { if (oldfile.exists()) { //檔案存在時 FileInputStream inStream = new FileInputStream(oldfile); //讀入原檔案 FileOutputStream fs = new FileOutputStream(new File(str2,oldfile.getName())); byte[] buffer = new byte[5120]; int length; while ( (byteread = inStream.read(buffer)) != -1) { bytesum += byteread; //位元組數 檔案大小 System.out.println(bytesum); fs.write(buffer, 0, byteread); } inStream.close(); } } catch (Exception e) { System.out.println("複製單個檔案操作出錯"); e.printStackTrace(); } //19.複製一個資料夾下所有的檔案到另一個目錄 //import java.io.*; File copyfiles=new File(str1); File[] files=copyfiles.listFiles(); for(int i=0;i<files.length;i++){ if(!files[i].isDirectory()){ int bytesum = 0; int byteread = 0; try { InputStream inStream = new FileInputStream(files[i]); //讀入原檔案 FileOutputStream fs = new FileOutputStream(new File(str2,files[i].getName()); byte[] buffer = new byte[5120]; int length; while ( (byteread = inStream.read(buffer)) != -1) { bytesum += byteread; //位元組數 檔案大小 System.out.println(bytesum); fs.write(buffer, 0, byteread); } inStream.close(); } catch (Exception e) { System.out.println("複製單個檔案操作出錯"); e.printStackTrace(); } } } //提取副檔名 String str2=str1.substring(str1.lastIndexOf(".")+1);