Search code examples
javanetbeansnas

Move files to a nas with javacode and rename my files


Good morning guys,

I'm french so i dont have a beautiful English ^^ My problem is in my code, I want move my files to a nas with this path("\nas-tps\commun-tps") but it's not correct but Netbeans tells me it is wrong (In my class Parcourir.java with the JFileChooser) How I can do to put this path ?

My class parcourir:

package ged;

import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;

/**
 *
 * @author Evan
 */
public class Parcourir {

    public void Enregistrer() throws IOException

        {
                JFileChooser newdestination = new JFileChooser();
                newdestination.setCurrentDirectory(new File("\\nas-tps\commun-tps"));//Chemin
                newdestination.setAcceptAllFileFilterUsed(false);
                newdestination.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                newdestination.setMultiSelectionEnabled(false);
                newdestination.showOpenDialog(null);
                              
                int dest = newdestination.showOpenDialog(null);
                    if(dest == JFileChooser.APPROVE_OPTION)
                    {
                    File[] fichier02=newdestination.getSelectedFiles();
                        for(int t = 1; t<fichier02.length; ++t)
                        { 
                        fichier02[t].getName();    
                        fichier02[t].getAbsolutePath();
                        }
                    System.out.println("Destination choisie : " + newdestination.getSelectedFile());
                    }
                    else
                    {
                    System.out.println("Aucune destination choisie");
                    }
        }        

}

And in the second time, I want in the final of my code, I want rename my file with the path of my method Enregistrer but it's not correct for Netbeans...

My class GED:

package ged;

import java.awt.Component;
import java.io.File;
import java.util.Scanner;
import javax.swing.JFileChooser;

/**
 *
 * @author Evan
 */
public class GED {
    private static String Nom_Client;
    private static String N_plan;
    private static String ind;
    private static String Reference;
    private static String Typologie;
    
    

    public static void main(String[] args) throws Exception {
        // TODO code application logic here

        System.out.println("****************** TPS Gestionnaire ******************");
        System.out.println(" ");

        System.out.println("Quel fichier choisissez vous ?");

        JFileChooser file = new JFileChooser();
        file.setFileSelectionMode(JFileChooser.FILES_ONLY);
        file.setMultiSelectionEnabled(false);
            int retour = file.showOpenDialog(null);
                if(retour == JFileChooser.APPROVE_OPTION)
                {
                    File[] fichier=file.getSelectedFiles();
                        for( int i = 1; i<fichier.length; ++i)
                        { 
                        fichier[i].getName();    
                        fichier[i].getAbsolutePath();
                        }
                        System.out.println("Fichier choisi : " + file.getSelectedFile().getName());
                }
                else
                {
                    System.out.println("Aucun de fichier choisi");
                }       

        System.out.println (" ");

        System.out.println("Veuillez indiquer le nom du client");
        Scanner name = new Scanner (System.in);
        String Nom_Client = name.nextLine();
        System.out.println("L'entreprise est: " + Nom_Client.toUpperCase());

        System.out.println ("Dans quel sous doussier souhaitez vous mettre votre document ?");
        System.out.println (" ");
        System.out.println ("1." + " PV Contrôle ");
        System.out.println ("2." + " Plan ");

        Scanner sr = new Scanner (System.in);
        int i = sr.nextInt();
        Categorie c = new Categorie (Nom_Client.toUpperCase(),"0","0","0","0");
        if (i==1)
            {
                c.PVControle();
                
            }
        else
            {  
                c.Plan();
                
                
                System.out.println (" Veuillez choisir la nouvelle destination du fichier");
                System.out.println(" ");
                Parcourir dest = new Parcourir();
                dest.Enregistrer();
                File source = file.getSelectedFile(); //Permet de récupérer le chemin du début
                File destination = new File (dest.Enregistrer() + Nom_Client.toUpperCase()+" "+ c.getN_plan()+ " " + "Ind" + " "+ c.getind().toUpperCase() +".pdf"); // Permet d'avoir la nouvelle destination avec le fichier renommé
                source.renameTo(destination); //Pas encore corrigé
                System.out.println(" Votre fichier à été renommé puis déplacé");

            }

    }

}

And my class Categorie:

package ged;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFileChooser;

/**
 *
 * @author Evan
 */
public class Categorie {
    private String Nom_Client;
    private String N_plan;
    private String ind;
    private String Reference;
    private String Typologie;


    public Categorie(String Nom_Client, String N_plan, String ind, String Reference, String Typologie) {
        this.Nom_Client = Nom_Client;
        this.N_plan = N_plan;
        this.ind = ind;
        this.Reference = Reference;
        this.Typologie = Typologie;
    }

    
    public void PVControle () throws IOException{
                   
        int t = 0;
        System.out.println("Vous avez choisit la catégorie PV de contrôle");
        System.out.println("Veuillez indiquer la référence produit");
        Scanner ref = new Scanner (System.in);
        this.Reference = ref.nextLine(); //Permet de demander la reference du plan
        System.out.println("Ainsi que la typologie de production");
        System.out.println("1." + "TÔLES");
        System.out.println("2." + "BOB");
        Scanner typ = new Scanner (System.in);
        this.Typologie = ref.nextLine(); //Permet de demander la typologie
            
        if ( t == 1)
            {
                
                System.out.println (" Vous avez choisit la typologie TÔLES ");
                this.Typologie = "TÔLES";
            }
        
        else 
            {
                System.out.println(" Vous avez choisit la typologie BOB ");
                this.Typologie = "BOB";
            }
        
        System.out.println("Le nom du fichier est: " +Nom_Client + " " + "REF" + " " + Reference.toUpperCase() + " " + "-" + " " + Typologie);    
    }
    
    public void Plan () throws IOException {
        System.out.println("Vous avez choisit la catégorie Plan");        
        System.out.println("Veuillez indiquer le n° Plan");
        Scanner plan = new Scanner (System.in);
        N_plan = plan.nextLine(); //Permet de demander le n°Plan        
        System.out.println ("Ainsi que l'IND");
        Scanner IND = new Scanner (System.in);//Demande de l'IND
        ind = IND.nextLine();        
        System.out.println("Le nom du fichier est: " +Nom_Client + " " + N_plan + " " + "Ind" +" " + ind.toUpperCase());        
        
    }
        
        
    public String getN_plan()
    {
     return N_plan;
    }
    
    public String getind()
    {
        return ind;
    }

}

Can you help me guys ? It's very important for me !

Thanks for your comprehension !


Solution

  • maybe you forgot to escape the middle back-slash?

    newdestination.setCurrentDirectory(new File("\\nas-tps\\commun-tps"));//Chemin
    

    or perhaps you forgot to escape all the back-slashes? (it is not clear if you need double slashes at the start of the path)

    newdestination.setCurrentDirectory(new File("\\\\nas-tps\\commun-tps"));//Chemin
    

    also, as far as I know, Java works fine with forward slash as directory marker, regardless of OS specifics. try the path "/nas-tps/commun-tps" or "//nas-tps/commun-tps"