Jump to content

Read text file


sean04

Recommended Posts

Hey!

 

So I have an app that stores records to a text file. An example of one of the records in the text file is as follows:

 

10/08/2010|Sean|M|7852546225|7852546225|Dr. Something

 

Each field is separated by a "|".

 

How can I read that text file and each record and put it in a table like this:

 

App Date      First Name    Last Name    Home Number    Other Number    Doctors Name

10/08/2010  Sean              M                  7852546225        7852546225      Dr. Something

 

I currently have this:

 

public synchronized static Vector readRecords(String file) throws IOException{
        Vector users = new Vector();
        BufferedReader in = new BufferedReader(
                            new FileReader(file));
        String line = in.readLine();
        while (line != null){
            StringTokenizer t = new StringTokenizer(line, "|");
            String appDate = t.nextToken();
            String firstName = t.nextToken();
            String lastName = t.nextToken();
            String homeNumber = t.nextToken();
            String otherNumber = t.nextToken();
            String doctorsName = t.nextToken();
            User user = new User(appDate, firstName, lastName, homeNumber, otherNumber, doctorsName);
            users.add(user);
            line = in.readLine();
        }
        in.close();
        return users;
    }


 

And where I want the stuff to show up I have:

 



<%! String filename = "C:/Users/Sean/Desktop/other.txt"; %>
<% java.util.Vector users = Booking.UserIO.readRecords("C:/Users/Sean/Desktop/other.txt");
   session.setAttribute("users", users);
%>

<table border="1" cellspacing="5" cellpadding="5">
    <tr>
        <td>Date</td>
        <th>Client First Name</th>
        <th>Client Last Name</th>
        <th>Client Phone Numbers</th>
        <th>Doctor</th>
    </tr>

    <tr valign="top">
        <td><p><%= pageContext.getAttribute("appDate") %></td>
        <td><p><%= pageContext.getAttribute("firstName") %></td>
        <td><p><%= pageContext.getAttribute("lastName") %></td>
        <td><p><%= pageContext.getAttribute("homeNumber") %><br><%= pageContext.getAttribute("otherNumber") %></td>
        <td><p><%= pageContext.getAttribute("doctorsName") %></td>

    </tr>


</table>

 

The only thing showing up in the table is "null".

Hopefully theres an easier way!

 

Thanks in advance,

Sean

Link to comment
https://forums.phpfreaks.com/topic/204409-read-text-file/
Share on other sites

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.