Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading multiple "columns" from text file

Status
Not open for further replies.

Wrathchild

Technical User
Aug 24, 2001
303
US
Hi, I've read many postings & docs on doing this, but am still confused...help would be mucho appreciated

Sample data in text file:
app1name, c:\my data\app1name.mde
app2name, c:\my data\app2name.mde
app3name, c:\my data\app3name.mde

Now I would like to read this data into an array, hashmap, or whatever so I can use it in my application without hardcoding it. Below is what I currently have:
Code:
    public void initPathData() {
        apps.add(new ApplicationsHolder("app1name", "c:\my data\app1name.mde"));
        apps.add(new ApplicationsHolder("app2name", "c:\my data\app2name.mde"));
        apps.add(new ApplicationsHolder("app3name", "c:\my data\app3name.mde));
}

thanks!
 
Hi,

Read the text file line by line, as the line delimiter is "," use the StringTokenize or String.slpit() and store them in HashMap. After reading the file send the HashMap to initPathData();

Some thing like this
Code:
 BufferedReader inputStream = new BufferedReader(new FileReader("textFile.txt"));
String inLine = null;
HashMap map = new HashMap();
   while ((inLine = inputStream.readLine()) != null) {
        String[] s = inLine.split(",");
	map.put(s[0],s[1]);
    }
 inputStream.close();
 // pass the map to you method

Cheers
Venu
 
thanks, I'm getting the following errors:

ApplicationList.java [111:1] unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
BufferedReader inputStream = new BufferedReader(new FileReader("textFile.txt"));

ApplicationList.java [114:1] unreported exception java.io.IOException; must be caught or declared to be thrown
while ((inLine = inputStream.readLine()) != null) {

ApplicationList.java [118:1] unreported exception java.io.IOException; must be caught or declared to be thrown
inputStream.close();

3 errors
 
Hi,

Catch the IOException while reding the file. And the exception is that its not able to read the file "textFile.txt"

Code:
HashMap map = new HashMap();
       try {
           BufferedReader inputStream = new BufferedReader(new FileReader("textFile.txt"));
           String inLine = null;
           while ((inLine = inputStream.readLine()) != null) {
               String[] s = inLine.split(",");
               map.put(s[0], s[1]);
           }
           inputStream.close();
       } catch (IOException e) {
           System.out.println(e.toString());

       }

Cheers
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top