java文件将文件从文件夹1交换到文件夹2 [英] java files swap files from folder 1 to folder 2

查看:86
本文介绍了java文件将文件从文件夹1交换到文件夹2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码listes目录1和目录2的图像现在我需要一个函数来将目录1中的concrate图像交换到目录2中的另一个图像或反向



我的代码是:



My code listes the images of directory 1 and directory 2 now i need a function to swap a concrate image from directory 1 to another image from directory 2 or at reverse

the code i have is this:

public class Art_replace {

    /**
     * @param args the command line arguments
     */
    public static String ext;
    public static int MAX_FILE=30;
    
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        String c_projecte,c_imatges;
        String dades[]= new String[2];
        String arr_img_prj[] = new String [MAX_FILE];
        String arr_img_mod[] = new String [MAX_FILE];
        
        demanar_dades (dades);
        c_projecte=dades[0];
        c_imatges=dades[1];
        
        File fp = new File(c_projecte);
        recorrer_dir_rec (fp,arr_img_prj);
        //System.out.println (Arrays.toString(arr_img_prj));
        File fi = new File(c_imatges);
        recorrer_dir_rec (fi,arr_img_mod);
        //System.out.println (Arrays.toString(arr_img_mod));
        Mostrar_informacio (arr_img_prj,arr_img_mod);
    }
    
    private static void Mostrar_informacio(String arr_img_proj[],String arr_img_mod[]){
        int i ;
        String espais= " ";
    System.out.println ("Imatges del projecte                               Imatges de substitució");
    System.out.println ("====================                               ======================");           
    for (i = 0;i<arr_img_proj.length;i++){>
        espais = calcula_espais(arr_img_proj[i]!=null?arr_img_proj[i].length():4);
        System.out.printf  ("%d.-%s %s %d.-%s\n",i,arr_img_proj[i],espais,i,arr_img_mod[i]);
        espais = " ";
        if (arr_img_proj[i]==null && arr_img_mod[i]==null)
            break;
        }
    }
    
    private static String calcula_espais (int num){
            int i;
            String espai = " ";
            for (i =num;i<=23;i++)
                espai = espai + " ";
            return espai;
    }
    private static void demanar_dades (String dades[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Introdueix la carpeta del projecte: ");
        dades[0] = sc.nextLine();
        System.out.print("Introdueix la carpeta d'imatges: ");
        dades[1] = sc.nextLine();
        System.out.print("Introdueix la extensió de les imatges: ");
        ext = sc.nextLine();
    }
    
    private static void recorrer_dir_rec (File f, String arr_img[]) throws IOException{
        File[] fitxers = f.listFiles();
        for (int x=0;x<fitxers.length;x++){>
            if (fitxers[x].isFile())
                guardar_imatges_dir(fitxers[x],arr_img);
            else if (fitxers[x].isDirectory())
                recorrer_dir_rec(fitxers[x],arr_img);
            }              
    }    
   
    private static void guardar_imatges_dir (File f, String arr_img[]){
        if (f.getName().indexOf("." + ext)!=-1){
            int i=0;
            while (arr_img[i]!=null)
                i++;
            arr_img[i]=f.getPath();
        }
    }
    private static void guardar_imatges_fitx (File f, String arr_img[]) throws FileNotFoundException, IOException{
        int i=0;
        while (arr_img[i]!=null)
            i++; 
        BufferedReader rli = new BufferedReader(new FileReader(f));
        String linia = rli.readLine();
        while(linia != null){
            if (linia.contains("."+ext)){
                arr_img[i]= recupera_nom_fitxer (linia);
                i++;
            }
            linia = rli.readLine();
        }
    }

    private static String recupera_nom_fitxer(String linia) {
        int index=0;
        String nom_file="";
        index = linia.indexOf("." + ext,index);
            while (linia.charAt(index)!=' ' && linia.charAt(index)!='\\'&& linia.charAt(index)!='/'){
                    nom_file= linia.charAt(index) + nom_file;
                    index = index-1;
            }
            return nom_file + ext;
    }
}

推荐答案

参见 https://www.google.com/search?q=java%20rename%20file [ ^ ]。


这篇关于java文件将文件从文件夹1交换到文件夹2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆