reading a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    reading a file

    why

    while((strLine = br.readLine()) != null)

    reads the file 3 times ?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by oll3i
    why

    while((strLine = br.readLine()) != null)

    reads the file 3 times ?
    Who knows? Maybe you open, read and close the file in a loop you didn't show us?

    kind regards,

    Jos

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      Code:
       private List<String> getKlienci(){
      	  
      		try{
          	    
          	    
          	    BufferedReader br = new BufferedReader(new FileReader("klienci.txt"));
          		       
          	    
          	    
          	    
          	
          	    
          	    String strLine ;
          	  
          	    String nazwa;
          	    String[] get_nazwa;
          	 
          	  while((strLine = br.readLine()) != null)   {
          		  System.out.println(strLine);
          		  get_nazwa=strLine.split(":");
          	       if(get_nazwa[0].equals("nazwa")){		    			     nazwa=get_nazwa[1];
          	              klienci_z_pliku.add(nazwa); }   
          	  }
          	  
          	
          	
          	  
          	    br.close();
          	  }catch (FileNotFoundException e) {
          	 System.err.println("File not found!");
          	  }catch (IOException e) {
          	  e.printStackTrace(); 
          	  }catch (Exception e)  {
                e.printStackTrace();  }	
      	  
      	 return klienci_z_pliku; 
        }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by oll3i
        Code:
         private List<String> getKlienci(){
        	  
        		try{
            	    
            	    
            	    BufferedReader br = new BufferedReader(new FileReader("klienci.txt"));
            		       
            	    
            	    
            	    
            	
            	    
            	    String strLine ;
            	  
            	    String nazwa;
            	    String[] get_nazwa;
            	 
            	  while((strLine = br.readLine()) != null)   {
            		  System.out.println(strLine);
            		  get_nazwa=strLine.split(":");
            	       if(get_nazwa[0].equals("nazwa")){		    			nazwa=get_nazwa[1];
            	              klienci_z_pliku.add(nazwa); }   
            	  }
            	  
            	
            	
            	  
            	    br.close();
            	  }catch (FileNotFoundException e) {
            	 System.err.println("File not found!");
            	  }catch (IOException e) {
            	  e.printStackTrace(); 
            	  }catch (Exception e)  {
                  e.printStackTrace();  }	
        	  
        	 return klienci_z_pliku; 
          }
        I still can't tell what the cause might be; are you calling that method three
        times from different places? 'klience_z_plik u' is a member variable so calling
        this method three times keeps on adding Strings three times to the same list.
        Try to clear that list first before you start reading from that file and see what
        happens ...

        kind regards,

        Jos

        Comment

        • oll3i
          Contributor
          • Mar 2007
          • 679

          #5
          i call that method only once
          and it still reads the file now 2 times

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Add a klienci_z_pliku .clear() near the top of the method that reads from the
            file and see what happens then.

            kind regards,

            Jos

            Comment

            • oll3i
              Contributor
              • Mar 2007
              • 679

              #7
              i added clear before i read from the file and add to the list but it doesnt help

              Comment

              • oll3i
                Contributor
                • Mar 2007
                • 679

                #8
                i solved it :) thank you

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by oll3i
                  i solved it :) thank you
                  What was the bug then?

                  kind regards,

                  Jos

                  Comment

                  Working...