I have a series of textfiles that I want to split up
the textfiles look like this
I have a class called result and it has following fields:
String name (the name of the pilot, ex: mika hakkinen )
String team (the name of the team, ex: mclaren-mercedes)
String time (the time of the pilot, ex: 5m 11.783s or +3.423s)
I want to add object of this class to an ArrayList, reading it from the file
loop{
new result(name,team,time);
}
what methods and functions for splitting and reading the file would be best to use?
splitting up a textfile
Started by Shaddix, Jun 13 2009 05:55 AM
10 replies to this topic
#1
Posted 13 June 2009 - 05:55 AM
|
|
|
#2
Posted 13 June 2009 - 06:19 AM
Given that it looks like you have fixed field widths in your source file, you should be able to use the substring and trim methods to extract the data you want.
#3
Posted 14 June 2009 - 12:53 AM
Hello,
do you plan to use the text-file for something else aswell?
When i created a little game with highscores i simply wrote the highscore to a file. Note that it was a binary file and thus can't be edited by opening it in notepad or something. So you could make an arrayList of results. And then simply write the whole arrayList with 2 lines of code. So you won't be adding it to the files line by line.
Here 'highscores' is the name of the arrayList, 'highscore.dat' is the name of the (binary) file. I never tested if it would work with txt-files. But why wouldn't it? ^^
Only "problem" you may have is. Since it writes it away as an object you can't manually edit the file in notepad.
So you must make it manually in java.
Back to the code. Once it's written away you can read it back like this:
the 2 lines in the try{} are most important.
note: instead of
It's also possible by reading the Strings of the txt file, line by line, trimming them etc. But this is waaay easier :p
do you plan to use the text-file for something else aswell?
When i created a little game with highscores i simply wrote the highscore to a file. Note that it was a binary file and thus can't be edited by opening it in notepad or something. So you could make an arrayList of results. And then simply write the whole arrayList with 2 lines of code. So you won't be adding it to the files line by line.
outputStream = new ObjectOutputStream(new FileOutputStream("highscore.dat"));
outputStream.writeObject(highscores);
(you'll need to add a try{} and some catches...)Here 'highscores' is the name of the arrayList, 'highscore.dat' is the name of the (binary) file. I never tested if it would work with txt-files. But why wouldn't it? ^^
Only "problem" you may have is. Since it writes it away as an object you can't manually edit the file in notepad.
So you must make it manually in java.
Back to the code. Once it's written away you can read it back like this:
try
{
inputStream = new ObjectInputStream(new FileInputStream("highscore.dat"));
highscores = (ArrayList<String>) inputStream.readObject();
}
catch (FileNotFoundException e)
{
resetHighScore();
}
catch (IOException e)
{
System.err.println ("Fout bij het lezen");
resetHighScore();
}
catch(ClassNotFoundException e){
resetHighScore();
System.err.println ("Kan file nietlezen");
}
finally {
try {
if (inputStream != null) {
inputStream.close();
}
}
catch (IOException e)
{
System.err.println ("Kan file niet openen / lezen");
}
}
(error println is in dutch^^)the 2 lines in the try{} are most important.
note: instead of
highscores = (ArrayList<String>) inputStream.readObject();you will have to cast it into ArrayList<Result> instead of String. (you can probably reduce the number of catches by using catch (Exception e) and remove the others.
It's also possible by reading the Strings of the txt file, line by line, trimming them etc. But this is waaay easier :p
#4
Posted 14 June 2009 - 05:25 AM
I did a highscoresystem ones that is very similar to yours, so that's not a problem
my problem is splitting my file up so I can use it and make some statistics of all files I put into the program (there are multiple textfiles like the one I gave as an example)
I have succeeded to load my file into a string, but now the trim function isn't working :s
this is my code:
when I run it on the file I used in the first post I get:
but I want it to be
my problem is splitting my file up so I can use it and make some statistics of all files I put into the program (there are multiple textfiles like the one I gave as an example)
I have succeeded to load my file into a string, but now the trim function isn't working :s
this is my code:
package resultmanagement;
import javax.swing.*;
import java.io.*;
public class ResultProcessor extends JFrame {
private String location = "C:\\Games\\Grand Prix 4\\TEXT.txt";
private JFileChooser chooser;
public ResultProcessor() {
chooser = new JFileChooser();
split();
}
public void split(){
String text = openFile();
String gpname = "";
gpname = text.substring(254, 334);
gpname.replaceAll(" ", "");
System.out.println("gpname: " + gpname);
}
private String openFile() {
int returnVal = chooser.showOpenDialog(this);
if (returnVal != JFileChooser.APPROVE_OPTION) {
return "fail"; // cancelled
}
File bestand = chooser.getSelectedFile();
BufferedReader reader = null;
String text = "";
try {
reader = new BufferedReader(new FileReader(bestand));
String line = reader.readLine();
while (line != null) {
text += line;
line = reader.readLine();
}
} catch (FileNotFoundException fne) {
System.out.println("Kan de file " + bestand.getName() + "niet openen: " + fne.getMessage());
} catch (IOException ioe) {
System.out.println("Problemen bij het lezen van de file: " + ioe.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ioe) {
System.out.println("Problemen bij het sluiten van de file: " + ioe.getMessage());
}
}
}
return text;
}
}
when I run it on the file I used in the first post I get:
gpname: Australia - Albert Park(with spaces)
but I want it to be
gpname: Australia - Albert Park(without spaces)
#5
Posted 14 June 2009 - 05:47 AM
You have to break the string into two pieces, then trim, then reassemble. Alternatively, you could repeatedly replace " " with " ".
#6
Posted 14 June 2009 - 06:08 AM
I don't realy get it, normaly the trim should delete all spaces in front and afther the characters
"gpname: " is only added in the printline so it doesn't affect the trimming
also the spaces afther "Australia - Albert Park" are still present, but the forum seems to delete them
"gpname: " is only added in the printline so it doesn't affect the trimming
also the spaces afther "Australia - Albert Park" are still present, but the forum seems to delete them
#7
Posted 14 June 2009 - 06:10 AM
I don't see a call to trim anywhere in your code.
#8
Posted 14 June 2009 - 06:32 AM
oh sorry, my fault, I have tried trim, but since that didn't work I used:
but that also didn't work
gpname.replaceAll(" ", "");
but that also didn't work
#9
Posted 14 June 2009 - 07:48 AM
Probably because you have to make
because replaceAll will return a string, and you have to save that returned value.
gpname = gpname.replaceAll(" ","");
because replaceAll will return a string, and you have to save that returned value.
#10
Posted 14 June 2009 - 08:10 AM
you where correct, noobish mistake from me
thanx for helping!
thanx for helping!
#11
Posted 14 June 2009 - 08:12 AM
No problem.. that happened to me too :) when I was started learning Java


Sign In
Create Account


Back to top









