2015-09-21 42 views
0

早上好,大家好,将文件移动到同一个javacode NAS和重命名我的文件

我是法国人,所以我不有一个美丽的英国^^ 我的问题是在我的代码,我想我的文件移动到nas这个路径(“\ nas-tps \ commun-tps”),但它不正确,但是Netbeans告诉我这是错误的(在我的类中使用JFileChooser的Parcourir.java) 我该怎么做才能放这个路径?

我班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"); 
 
        } 
 
     }   
 

 
}

而在第二次,我想在决赛中我的代码,我想重新命名我的文件,我的方法Enregistrer的路径但它不是纠正Netbeans的...

我班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é"); 
 

 
      } 
 

 
    } 
 

 
}

我的班种类:

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; 
 
    } 
 

 
}

你能帮助我的家伙? 这对我来说非常重要!

感谢您的理解!

+0

只是一个建设性的评论:这是不必要的,不推荐张贴的所有项目的代码,如果它不直接releted的问题 –

回答

0

也许你忘了逃避中间的反斜杠?

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

或者你忘了逃避所有的反斜线? (目前尚不清楚,如果你在路径的开始需要双斜杠)

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

也,据我所知,Java的正常工作与正斜杠作为目录标志,不管OS细节的。尝试的路径"/nas-tps/commun-tps""//nas-tps/commun-tps"

+0

感谢您的帮助,但是这是什么不起作用 –

+0

不行 ?你遇到了什么错误? NAS路径从命令行看起来如何? –

+0

你在运行什么操作系统?它是虚拟机还是本机? –

相关问题