Mise à jours nom du fichier

Bonjour,

J'ai écrit le code suivant pour lire un fichier texte et pour extraire les fichier.Mais le problème que le nom du fichier change chaque jour (par exemple:nom du fichier="10032016.BDE".J'ai pas su comment changer la destination automatiquement

Code:
package houssem;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
 
 
public class ReadWithScanner {
 
 
public static void main(String... aArgs) throws IOException, InterruptedException {
 int j =0;
 Boolean c=true;
   do{ int a=count(); 
   String b=null;
   int ki=0;
 
 
 
  ReadWithScanner parser = new ReadWithScanner("C:\\ERROR\\05122015.BDE");
 
         try (Scanner scanner =  new Scanner(parser.fFilePath)){
         if(c){
                     while (scanner.hasNextLine()) {ki++;b=scanner.nextLine();
                     if(ki>20)
                     {processLine(b);
                     }
 
                     j++;c=false;
                     }}
                     }
 
                       if(j<a)  {Scanner scanner1 = new Scanner(new File("C:\\ERROR\\05122015.BDE"));
                         String line = null;
 
                       while (scanner1.hasNextLine()) {
                           line = scanner1.nextLine();}
                          int k=countwords(line);
                          if(k>=9){ processLine(line);j++;}
                            scanner1.close();}
 
 
         Thread.sleep(1000);
         }while(true);
         }
  public static int count() throws IOException{
   int j = 0;
   ReadWithScanner parser = new ReadWithScanner("C:\\ERROR\\05122015.BDE");
     try (Scanner scanner =  new Scanner(parser.fFilePath)){
 
                while (scanner.hasNextLine()) { j++;
   scanner.nextLine();   }   
            }   
     return(j);
  }
 
  public static int countwords(String line) throws IOException{
   int j=0;
   Scanner s=new Scanner(line);
   while(s.hasNext()){ j++;s.next();} 
     return(j);                                      }
 
  public ReadWithScanner(String aFileName){
    fFilePath = Paths.get(aFileName);
  }
 
 
 
  protected static void processLine(String aLine){
 
    Scanner scanner = new Scanner(aLine);
    scanner.useDelimiter(" ");
    if (scanner.hasNext()){
      String value = scanner.next();
         String name = scanner.next();
            String value2 = scanner.next();
            String value3 = scanner.next();
           String value4 = scanner.next();
      System.out.println( quote(value.trim())+"       "+ quote(name.trim())+"       "+quote(value2.trim())+quote(value3.trim())+quote(value4.trim()));
    }
    else {
      System.out.println("invalid line");
    }
    scanner.close();
  }
 
 
  private  Path fFilePath;  
 
  private static String quote(String aText){
        return aText;} 
}

1 réponse

  1. Salut ? Tu n'as qu'un seul fichier BDE ? Si oui tu peux lister le contenu de ton répertoire et retenir le nom du fichier avec l'extension BDE
    0
    1. chaque jour la machine a un nouveau fichier crée automatiquement dont le nom de fichier est la date du jour
      0
    2. Ha bah dans ce cas il te suffit avant de calculer le nom du fichier.

      Exemple pour l'avoir au format que tu souhaites :
      import java.util.Calendar;
      
      public class DateDuJour {
      
       public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        int day = c.get(Calendar.DAY_OF_MONTH);
        int month = c.get(Calendar.MONTH) + 1;
        int year = c.get(Calendar.YEAR);
        String date = "";
        if (day < 10){
         date += "0" + Integer.toString(day);
        } else {
         date += Integer.toString(day);
        }
      
        if (month < 10){
         date += "0" + Integer.toString(month);
        } else {
         date += Integer.toString(month);
        }
        date += Integer.toString(year);
        System.out.println(date);
       }
      
      }
      0
    3. comment le mettre ce nom de fichier pour le lire avec scanner. C ça le gros problème pour moi
      0
    4. Tu remplaces tes nom de fichiers par la ta date :

      package houssem;
      import java.io.File;
      import java.io.IOException;
      import java.nio.file.Path;
      import java.nio.file.Paths;
      import java.util.Calendar;
      import java.util.Scanner;
      
      
      public class ReadWithScanner {
      
      	public static final String fileName = "C:/ERROR/"+ReadWithScanner.getDate()+".BDE";
      	
      	public static String getDate(){
      		Calendar c = Calendar.getInstance();
      		int day = c.get(Calendar.DAY_OF_MONTH);
      		int month = c.get(Calendar.MONTH) + 1;
      		int year = c.get(Calendar.YEAR);
      		String date = "";
      		if (day < 10){
      			date += "0" + Integer.toString(day);
      		} else {
      			date += Integer.toString(day);
      		}
      
      		if (month < 10){
      			date += "0" + Integer.toString(month);
      		} else {
      			date += Integer.toString(month);
      		}
      		date += Integer.toString(year);
      		return date;
      	}
      
      
      public static void main(String... aArgs) throws IOException, InterruptedException {	
      	int j =0;
      	Boolean c=true;
      	do{ int a=count(); 
      	String b=null;
      	int ki=0;
      
      	ReadWithScanner parser = new ReadWithScanner(fileName);
      
      	try (Scanner scanner =  new Scanner(parser.fFilePath)){
      		if(c){
      			while (scanner.hasNextLine()) {ki++;b=scanner.nextLine();
      			if(ki>20)
      			{processLine(b);
      			}
      
      			j++;c=false;
      			}}
      	}
      
      	if(j<a)  {Scanner scanner1 = new Scanner(new File(fileName));
      	String line = null;
      
      	while (scanner1.hasNextLine()) {
      		line = scanner1.nextLine();}
      	int k=countwords(line);
      	if(k>=9){ processLine(line);j++;}
      	scanner1.close();}
      
      
      	Thread.sleep(1000);
      	}while(true);
      }
      public static int count() throws IOException{
      	int j = 0;
      	ReadWithScanner parser = new ReadWithScanner(fileName);
      	try (Scanner scanner =  new Scanner(parser.fFilePath)){
      
      		while (scanner.hasNextLine()) { j++;
      		scanner.nextLine();   }   
      	}   
      	return(j);
      }
      
      public static int countwords(String line) throws IOException{
      	int j=0;
      	Scanner s=new Scanner(line);
      	while(s.hasNext()){ j++;s.next();} 
      	return(j);                                      }
      
      public ReadWithScanner(String aFileName){
      	fFilePath = Paths.get(aFileName);
      }
      
      
      
      protected static void processLine(String aLine){
      
      	Scanner scanner = new Scanner(aLine);
      	scanner.useDelimiter(" ");
      	if (scanner.hasNext()){
      		String value = scanner.next();
      		String name = scanner.next();
      		String value2 = scanner.next();
      		String value3 = scanner.next();
      		String value4 = scanner.next();
      		System.out.println( quote(value.trim())+"       "+ quote(name.trim())+"       "+quote(value2.trim())+quote(value3.trim())+quote(value4.trim()));
      	}
      	else {
      		System.out.println("invalid line");
      	}
      	scanner.close();
      }
      
      
      private  Path fFilePath;  
      
      private static String quote(String aText){
      	return aText;} 
      }
      0
    5. merci :D
      une autre chose j'ai sauvegardé les résultats dans une base de données. Le problème est lorsque l'un des résultats contient un apostrophe,la requete n'est pas inseré
      0