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

来源:互联网 发布:从零开始学android编程 编辑:程序博客网 时间:2024/06/03 15:26
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"));}

原创粉丝点击