?
Solved

Inputstreamreader reading in more than 1 file

Posted on 2003-03-21
4
Medium Priority
?
190 Views
Last Modified: 2010-03-31
HI im usin java to read in some data files, i can read one file fine, and it prints it out to screen, now when i go to read the second file, it only reads the first line of that file.

---------
this is the first file im reading which works fine and it prints out.
name,ID,numbers
try flowers,zzz,52.00
Balffaled Betty,BB,156.00
Prad,Prad,393.00
pinner,pinner,48.00
Machines,mM,94.50

this code reads it, and this works fine.

   public void load()  throws RemoteException {
    FileInputStream in;
    BufferedReader ds;
    String aLine=null;

    try {

           in = new FileInputStream("stock");
           ds = new BufferedReader(new InputStreamReader(in));

           aLine = ds.readLine();
           StringTokenizer st = new StringTokenizer(aLine,",");
         

                     
          aLine = ds.readLine();
           while(aLine != null) {
             st=new StringTokenizer(aLine,",");
          String sn = st.nextToken();
             String sid = st.nextToken();
          String sp = st.nextToken();
             Stock sk = new Stock (sn,sid,sp); //convert string to float
               
            System.out.println(sk.getID());            

             all.add(sk);                                                                                                                                                                                                                          
           aLine = ds.readLine();
           }
          changed = false;
          in.close();
        }
       
        catch(FileNotFoundException FNFE) {
            System.out.println("Stocklist: no students file");
            System.exit(1);
        } catch(IOException IOE) {
            System.out.println("Stocklist: read failed in load");
            System.exit(1);
        }  catch(Exception e) {
          System.out.println("Stocklist: some other excep");
     }
    }
-----------
now im trying to read in another file.

Name,Password
Steve prat,wolves
Gavin test,writer
joe bloggs,password


this code reads this file but it only prings out the 1st name like steve prat, then it dont do
nothing.

 public void loaduser() throws RemoteException {

   FileInputStream fis;
    BufferedReader br;
    String aLine2=null;                  
    try {

           fis = new FileInputStream("usernames");
           br = new BufferedReader(new InputStreamReader(fis));

           aLine2 = br.readLine();
           StringTokenizer sp = new StringTokenizer(aLine2,",");
           
           aLine2 = br.readLine();
           while(aLine2 != null) {
             sp=new StringTokenizer(aLine2,",");
          String un = sp.nextToken();
             String up = sp.nextToken();
       
             Login log = new Login (un,up);
               
            System.out.println(log.getUsername());            

             all2.add(log);                                                                                                                                                                                                       aLine2 = br.readLine();
           }
          changed = false;
          fis.close();
        }
       
        catch(FileNotFoundException FNFE) {
            System.out.println("Loginlist: no students file");
            System.exit(1);
        } catch(IOException IOE) {
            System.out.println("Loginlist: read failed in load");
            System.exit(1);
        }  catch(Exception e) {
          System.out.println("Loginlist: some other excep");
     }
    }

----------------
kinda stuck, i dont see what i am doing wrong.
0
Comment
Question by:mkrashid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 8181311
You need to be tokenizing in a loop like this:

 static void getLogons(String fileName){
      BufferedReader in = null;
      String fileLine = null;
      try {
        in = new BufferedReader(new FileReader(fileName));
        while ((fileLine = in.readLine()) != null){
            StringTokenizer st = new StringTokenizer(fileLine,",");
            while (st.hasMoreTokens()){
              // Do something
            }
          }
        }
      }
      catch (IOException e){
        e.printStackTrace();
      }
      finally {
        try { in.close(); } catch(IOException e) { e.printStackTrace(); }
      }
  }
0
 

Expert Comment

by:CleanupPing
ID: 9058556
mkrashid:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9730639
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept CEHJ's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question