Java File類的操作
阿新 • • 發佈:2019-03-07
sdi NPU catch temp sub ace substr void tput
題目描述
A文件夾下面有多個子文件夾,然後子文件下後面有一些.jpg文件,要求把這些.jpg文件完全拷貝復制到B文件夾。
實現思路
先遍歷循環A文件夾下的文件,然後找到符合.jpg的文件,放到一個列表中,然後再把列表中的jpg文件放到B文件夾上。
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.file.Files; import java.util.LinkedList; import java.util.List; import java.util.Queue; public class Picture { private static LinkedList<File> fileQueue= new LinkedList<>(); private final String aPath="C:\\Users\\m088163\\Pictures"; private final String bPath="D:\\temp"; /** * 將源文件夾下所有的.jpg文件找到,並存在fileQueue 列表中。 * @param path 需要查找文件的路徑名 * @param subStr 匹配的文件類型 */ public void FindJpg(String path,String subStr){ File file=new File(path); File[] files=file.listFiles(); for(int i=0;i<files.length;i++){ if(files[i].isDirectory()){ FindJpg(files[i].getAbsolutePath(),subStr); } if(files[i].getAbsolutePath().contains(subStr)){ fileQueue.add(files[i]); } } } /** * 將fileQeueue中的jpg文件存在目標文件夾。 * @param path * @throws Exception */ public void moveJpg(String path) throws Exception{ String myPath=""; File newFile; for(File files:fileQueue){ myPath=path+"\\"; myPath+=files.getName(); System.out.println(files.getName()); newFile=new File(myPath); if(newFile.exists()){ System.out.println("創建文件失敗"+newFile+"失敗,目標文件已經存在"); } if(!newFile.getParentFile().exists()){ System.out.println("文件的根目錄不存在,創建根目錄"); newFile.getParentFile().mkdir(); } copyfile(files.getAbsoluteFile(),newFile); } } /** * 寫入文件操作 * @param fromDest * @param toDest * @throws Exception */ public void copyfile(File fromDest,File toDest ) throws Exception{ FileInputStream is =new FileInputStream(fromDest); FileOutputStream os =new FileOutputStream(toDest); byte[] b =new byte[1024]; int temp=0; while((temp=is.read(b))!=-1){ os.write(b,0,temp); } is.close(); os.close(); } public static void main(String[] args) { Picture picture = new Picture(); picture.FindJpg(picture.aPath, ".jpg"); try { picture.moveJpg(picture.bPath); } catch (Exception e) { e.printStackTrace(); } } } import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.file.Files; import java.util.LinkedList; import java.util.List; import java.util.Queue; public class Picture { private static LinkedList<File> fileQueue= new LinkedList<>(); private final String aPath="C:\\Users\\m088163\\Pictures"; private final String bPath="D:\\temp"; /** * 將源文件夾下所有的.jpg文件找到,並存在fileQueue 列表中。 * @param path 需要查找文件的路徑名 * @param subStr 匹配的文件類型 */ public void FindJpg(String path,String subStr){ File file=new File(path); File[] files=file.listFiles(); for(int i=0;i<files.length;i++){ if(files[i].isDirectory()){ FindJpg(files[i].getAbsolutePath(),subStr); } if(files[i].getAbsolutePath().contains(subStr)){ fileQueue.add(files[i]); } } } /** * 將fileQeueue中的jpg文件存在目標文件夾。 * @param path * @throws Exception */ public void moveJpg(String path) throws Exception{ String myPath=""; File newFile; for(File files:fileQueue){ myPath=path+"\\"; myPath+=files.getName(); System.out.println(files.getName()); newFile=new File(myPath); if(newFile.exists()){ System.out.println("創建文件失敗"+newFile+"失敗,目標文件已經存在"); } if(!newFile.getParentFile().exists()){ System.out.println("文件的根目錄不存在,創建根目錄"); newFile.getParentFile().mkdir(); } copyfile(files.getAbsoluteFile(),newFile); } } /** * 寫入文件操作 * @param fromDest * @param toDest * @throws Exception */ public void copyfile(File fromDest,File toDest ) throws Exception{ FileInputStream is =new FileInputStream(fromDest); FileOutputStream os =new FileOutputStream(toDest); byte[] b =new byte[1024]; int temp=0; while((temp=is.read(b))!=-1){ os.write(b,0,temp); } is.close(); os.close(); } public static void main(String[] args) { Picture picture = new Picture(); picture.FindJpg(picture.aPath, ".jpg"); try { picture.moveJpg(picture.bPath); } catch (Exception e) { e.printStackTrace(); } } }
Java File類的操作