现在的位置: 首页 > 综合 > 正文

java 文件与文件夹拷贝、重命名、文件搜索

2013年10月02日 ⁄ 综合 ⁄ 共 955字 ⁄ 字号 评论关闭
java文件搜索
public void getDir(String strPath) throws Exception
	{
	    try
	    {
	        File f=new File(strPath);
	        if(f.isDirectory())
	        {
	            File[] fList=f.listFiles();
	            for(int j=0;j<fList.length;j++)
	            {
	                if(fList[j].isDirectory())
	                {
	                    System.out.println(fList[j].getPath());
	                    getDir(fList[j].getPath()); //在getDir函数里面又调用了getDir函数本身 
	                }
	            }
	            for(int j=0;j<fList.length;j++)
	            {
	                
	                if(fList[j].isFile())
	                {
	                    System.out.println(fList[j].getPath());//把这话换成你要处理的语句.比如 fList[j].substring(fList[j].length-3,3)=="txt"
	                }
	                
	            }
	        }
	    }
	    catch(Exception e)
	    {
	        e.printStackTrace();
	    }
	}
java 文件与文件夹拷贝
File fileOld = new File(pathOld);
		File fileNew = new File(pathNew);
		if(fileOld.exists()){
			try {
				FileInputStream fis = new FileInputStream(fileOld);
				FileOutputStream fos = new FileOutputStream(fileNew);
				int read = 0;
				while ((read = fis.read()) != -1) {
					fos.write(read);
					fos.flush();
				}
				fos.close();
				fis.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

java文件重命名
File file = new File("D:\\123.txt");
		if(file.exists()) {
		    file.renameTo(new File("D:\\1234.txt"));
		}

抱歉!评论已关闭.