IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Documents Java Discussion :

Exception si le fichier est vide


Sujet :

Documents Java

  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    350
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 350
    Par défaut Exception si le fichier est vide
    Bonjour tout le monde ;
    j'ai une méthode qui me retourne le contenue d'un fichier sous format d'un string.voici le code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    public String scGetContentOfFlux(InputStream in) {
    		LOG.debug("Appel de la méthode GererRecensementDiseImpl.scGetContentOfFlux : Lecture des valeurs \n");
    		String txtGenerique = null;
    		try {
    			// Lecture par segment de 0.5Mo 
    			byte buffer[]=new byte[512*1024];
    			int nbLecture;
     
    			// lecture du fichier d'origine et dump dans un String
    			ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    			while( (nbLecture = in.read(buffer)) != -1 ) {                	
    				bytes.write(buffer, 0, nbLecture);
    			}
    			txtGenerique = bytes.toString();			
    			txtGenerique = txtGenerique.replaceAll("\r\n",";"); // remplacer les retour à la ligne par des ;
    			txtGenerique = txtGenerique.substring(0, txtGenerique.length()-1); // supprimer le dernier ;
     
    		}
    		catch( java.io.IOException e ) {
     
    		} finally {
    			// Quoi qu'il arrive, on ferme les flux
    			try {
    				in.close();
    			} catch(Exception e) { }
    		}
    		return txtGenerique;
    	}
    Cette fonction marche très bien si le fichier (qui est convertit en input stream contient des caractère ou donnée),mais si j'ai un fichier vide, apr exemple fichier texte vide, j'obtient l'exception suivante :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    	at java.lang.String.substring(II)Ljava/lang/String;(Unknown Source)
    	at com.francetelecom.webvpn.services.recensement.GererRecensementDiseImpl.scGetContentOfFlux(Ljava/io/InputStream;)Ljava/lang/String;(GererRecensementDiseImpl.java:1465)
    	at jrockit.reflect.NativeMethodInvoker.invoke0(Ljava/lang/Object;ILjava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;(Unknown Source)
    	at jrockit.reflect.NativeMethodInvoker.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;(Unknown Source)
    	at jrockit.reflect.VirtualNativeMethodInvoker.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;(Unknown Source)
    	at java.lang.reflect.Method.invoke(Ljava/lang/Object;[Ljava/lang/Object;I)Ljava/lang/Object;(Unknown Source)
    	at com.francetelecom.webvpn.service.ServiceManager.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;(ServiceManager.java:142)
    	at $Proxy3.scGetContentOfFlux(Ljava/io/InputStream;)Ljava/lang/String;(Unknown Source)
    	at com.francetelecom.webvpn.struts.RecensementDISEAction.importerValeursExcel(Lorg/apache/struts/action/ActionMapping;Lorg/apache/struts/action/ActionForm;Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)Lorg/apache/struts/action/ActionForward;(RecensementDISEAction.java:957)
    	at jrockit.reflect.NativeMethodInvoker.invoke0(Ljava/lang/Object;ILjava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;(Unknown Source)
    	at jrockit.reflect.NativeMethodInvoker.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;(Unknown Source)
    	at jrockit.reflect.VirtualNativeMethodInvoker.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;(Unknown Source)
    	at java.lang.reflect.Method.invoke(Ljava/lang/Object;[Ljava/lang/Object;I)Ljava/lang/Object;(Unknown Source)
    	at org.apache.struts.actions.DispatchAction.dispatchMethod(Lorg/apache/struts/action/ActionMapping;Lorg/apache/struts/action/ActionForm;Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljava/lang/String;)Lorg/apache/struts/action/ActionForward;(DispatchAction.java:276)
    	at org.apache.struts.actions.DispatchAction.execute(Lorg/apache/struts/action/ActionMapping;Lorg/apache/struts/action/ActionForm;Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)Lorg/apache/struts/action/ActionForward;(DispatchAction.java:196)
    	at com.francetelecom.webvpn.struts.WebVPNDispatchAction.execute(Lorg/apache/struts/action/ActionMapping;Lorg/apache/struts/action/ActionForm;Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)Lorg/apache/struts/action/ActionForward;(WebVPNDispatchAction.java:241)
    	at org.apache.struts.action.RequestProcessor.processActionPerform(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Lorg/apache/struts/action/Action;Lorg/apache/struts/action/ActionForm;Lorg/apache/struts/action/ActionMapping;)Lorg/apache/struts/action/ActionForward;(RequestProcessor.java:421)
    	at org.apache.struts.action.RequestProcessor.process(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V(RequestProcessor.java:226)
    	at com.francetelecom.clara.security.struts.action.SecuredRequestProcessor.process(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V(SecuredRequestProcessor.java:165)
    	at org.apache.struts.action.ActionServlet.process(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V(ActionServlet.java:1164)
    	at org.apache.struts.action.ActionServlet.doPost(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V(ActionServlet.java:415)
    	at javax.servlet.http.HttpServlet.service(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V(HttpServlet.java:763)
    	at javax.servlet.http.HttpServlet.service(Ljavax/servlet/ServletRequest;Ljavax/servlet/ServletResponse;)V(HttpServlet.java:856)
    	at jrockit.reflect.NativeMethodInvoker.invoke0(Ljava/lang/Object;ILjava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;(Unknown Source)
    	at jrockit.reflect.NativeMethodInvoker.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;(Unknown Source)
    	at jrockit.reflect.VirtualNativeMethodInvoker.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;(Unknown Source)
    	at java.lang.reflect.Method.invoke(Ljava/lang/Object;[Ljava/lang/Object;I)Ljava/lang/Object;(Unknown Source)
    	at org.apache.catalina.security.SecurityUtil$1.run()Ljava/lang/Object;(SecurityUtil.java:243)
    	at jrockit.vm.AccessController.do_privileged_exc(Ljava/security/PrivilegedExceptionAction;Ljava/security/AccessControlContext;I)Ljava/lang/Object;(Unknown Source)
    	at jrockit.vm.AccessController.doPrivileged(Ljava/security/PrivilegedExceptionAction;Ljava/security/AccessControlContext;)Ljava/lang/Object;(Unknown Source)
    	at javax.security.auth.Subject.doAsPrivileged(Ljavax/security/auth/Subject;Ljava/security/PrivilegedExceptionAction;Ljava/security/AccessControlContext;)Ljava/lang/Object;(Subject.java:500)
    	at org.apache.catalina.security.SecurityUtil.execute(Ljava/lang/reflect/Method;Ljava/lang/Object;[Ljava/lang/Object;Ljava/security/Principal;)V(SecurityUtil.java:272)
    Caused by =

    est-ce que c'est car le fichier est vide??Merci pour votre aide.

  2. #2
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    txtGenerique = txtGenerique.substring(0, txtGenerique.length()-1); // supprimer le dernier ;
    Tu présuppose que ta string finira par ";" or ce n'est le cas que si ton fichier finissait par un retour à la ligne. C'est la ton erreur.

Discussions similaires

  1. [Shell] Tester si un fichier est vide
    Par Spitfire378 dans le forum Linux
    Réponses: 3
    Dernier message: 10/02/2008, 12h18
  2. [VB 6.0] Tester si un fichier est vide
    Par maxou75 dans le forum VB 6 et antérieur
    Réponses: 4
    Dernier message: 19/12/2006, 18h06
  3. Savoir si un fichier est vide?..
    Par babarpapa dans le forum Langage
    Réponses: 3
    Dernier message: 22/08/2006, 11h55
  4. Tester si un fichier est vide ?
    Par stepd dans le forum Réseau
    Réponses: 15
    Dernier message: 27/07/2006, 19h04
  5. Ksh - tester si un fichier est vide ou pas?
    Par mzt.insat dans le forum Linux
    Réponses: 1
    Dernier message: 25/04/2006, 14h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo