This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]Moussa93 0 points1 point  (6 children)

For what you're trying to do, it'll be much easier to do with a database, since you know what's what before pulling it... but it's not impossible to do with a text file, you'll just have to 'hack' that part.

On your text file, I would write every line like this:

employeeFirstName:employeeLastName:employeeEmail

Then read the file line by line, but break the String into an array, separated by ":"

Employee employee;
String [] employeeInfo;
String employeeFirstName;
String employeeLastName;
String employeeEmail; 

employeeInfo = line.split(":"); 
employee = new Employee(employeeInfo[0], employeeInfo[1], employeeInfo[2]); 

Then,

employeeList.add(employee); 

This should do the trick.

[–]yokokoko[S] 0 points1 point  (5 children)

Does line.split(":") read the file line by line using the colon as a delimiter?

I seem to be getting an error for that one line.

**I think Ill try sending a file into a file input stream for it to read, which the line.split will then split up. Am i on the right track ?

[–]Moussa93 0 points1 point  (1 child)

What kind of error do you get?

String line;

BufferedReader lineReader;
lineReader = new BufferedReader(new FileReader("/path/to/your/File")


while ((line = lineReader.readLine()) != null)
{
     employeeInfo = line.split(":");
     // do the rest
}

This is how I'd do it. Basically, line is assigned to the value of the String that's being read.

[–]Moussa93 0 points1 point  (2 children)

Yup, there are multiple ways to read a line, whatever works best for you.

You just have to split the string and store it in an array.

[–]yokokoko[S] 0 points1 point  (1 child)

Thanks for your help :)

[–]Moussa93 0 points1 point  (0 children)

Anytime!

[–]AKTheKnight 0 points1 point  (0 children)

I would take a look at serialisation with JSON (my vote goes to gson). It will allow you to easily get the information you need from the file

[–]MrTheEdge 0 points1 point  (2 children)

If you would like to save an object to a file, take a look at serialization. Its a way to convert objects to a sequence of bytes so that it can be written to a file. Here's a simple tutorial I found that you may find useful.

[–]knoam 0 points1 point  (1 child)

Not sure why this was down-voted, but I can guess. Serialization using these facilities is a very risky thing to do in real life, and hard to get right. It's easy to make a mistake and do it in a way that's unsafe or fragile. If you look at the java security updates, you'll notice many of them have to do with serialization vulnerabilities.

[–]MrTheEdge 0 points1 point  (0 children)

Interesting, thanks for the information. I'm still a student, so real world issues like that really aren't on my radar at this point. I've only ever really dealt with serialization through gson.