views:

67

answers:

4

What's the best way to do it? Should I use the File class and scanner? I've never done it before and cant seem to find a solid guide for it online so I figured I would ask here.

Thanks!

+1  A: 

If you are free to say what the syntax / structure of the text file is, then consider making it a Java properties file. Then you can load and save the file with minimal programming effort using the java.util.Properties class.

Stephen C
it is in 3 columns, with sample Name, ID, and birthrate variables. I'll take a look at the properties class on the api but I think my Lab is encouraging me to use File and scanner and Print Writer.
Cody
@Cody - properties file format is not applicable to your use case.
Stephen C
+2  A: 

The easiest way depends on the format of the text file. From your other comment, it sounds like the lines are tab separated values. As a beginner you will probably find it simplest to use Scanner. Specifically, Scanner.nextLine(). Couple that with using String.split("\t") to split the data into an array (assuming the format is tab-separated-values).

Tim Bender
Thanks, I will see what I can come up with.
Cody
http://pastie.org/1051615 Here is where I am at.I am having problems getting the "new person added to file" sysout.PeopleMap is my treeMap, and names is my arrayList. They already contain some values, that part works fine.
Cody
@Cody, sorry. Your comment is not really StackOverflow friendly. If you have additional questions or need to post clarifications (such as the input format or a code snippet) you should edit the original question.
Tim Bender
+1  A: 

Simply depends on the format of the text file.

  1. If its simple name value pair then you can use java.util.Properties. for example a.properties could look like:

    name=john city=san jose date=12 july 2010

then you can load this as:

Properties props = new Properties();
props.load(new FileInputStream("a.properties"));
  1. If format is different than what is supported by java.util.Properties.load() then using java.util.Scanner would be helpful to process it line by line:

    File file = new File("data.txt"); try {

    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {
       String line = scanner.nextLine();
       //Process each line seperately
       processLine(line);
    }
    

    } catch (FileNotFoundException e) { e.printStackTrace(); }

Suhas Phartale
http://pastie.org/1051615 Here is my code so far, I am having troubles getting it to print the new entry t console msg. Any ideas? I used File.
Cody
A: 

This is what I like to do in that situation:

Scanner s = new Scanner(file);
Scanner line;
String name;
String date;
int id;
while(s.hasNext()){
   line = new Scanner(s.nextLine());
   id = line.nextInt();
   name = line.next/*String*/();
   date = line.next/*String*/();
   /* Do something with id, name and date */
}

Maybe there is some exception handling or something like that

(Anyone want to comment on the efficiency of creating many new Scanners?)

sixtyfootersdude
Well... creating the extra Scanner is entirely unnecessary since you could simply call `nextInt(), next(), next()` on the original. Also, what if someone had a two-part name like "Jim Bob"? Using `next()` doesn't really handle that situation.
Tim Bender
Yeah, good point.
sixtyfootersdude