Java文件操作大全(绝对的经典,值得收藏!)

Java文件操作

 //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 folderList = new LinkedList(); 
	folderList.add(str1); 
	while (folderList.size() > 0) { 
		File file = new File(folderList.peek()); 
		folderList.removeLast(); 
		File[] files = file.listFiles(); 
		ArrayList fileList = new ArrayList(); 
		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 folderList = new LinkedList(); 
	folderList.add(str1); 
	LinkedList folderList2 = new LinkedList(); 
	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;kfolderList=new ArrayList(); 
			folderList.add(copyfoldersList[k].getPath()); 
			ArrayListfolderList2=new ArrayList(); 
			folderList2.add(str2+"/"+copyfoldersList[k].getName()); 
			for(int j=0;j folderList = new LinkedList(); 
	folderList.add(str1); 
	LinkedList folderList2 = new LinkedList(); 
	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;kfolderList=new ArrayList(); 
			folderList.add(movefoldersList[k].getPath()); 
			ArrayListfolderList2=new ArrayList(); 
			folderList2.add(str2+"/"+movefoldersList[k].getName()); 
			for(int j=0;jfolderList=new ArrayList(); 
	folderList.add(str1); 
	ArrayListfolderList2=new ArrayList(); 
	folderList2.add(str2); 
	for(int j=0;j



from: http://uujava0322.iteye.com/blog/1045876

你可能感兴趣的:(Java,文件操作,大全,经典,Java)