file reading
hi, i've got a text file(about 20,000 lines) such as:
1 - NETWORK ENTITY INFORMATION
NE ident Num:00000037226
Extension Lev:
System SW vers:020805010027
NE Type :C
Mother NE:
Total Capacity:
Capacity Part1:
Capacity Part2:
I wanna separate those lines to csv file:value,value,value....
How can i fast as possible read this file and cut it?
I've tried this:
FileReader fr = new FileReader(file_name);
BufferedReader br = new BufferedReader(fr);
while((line=br.readLine())!=null){
String stream = stream+line; //very slow working!@!!!!!!!!!!
}
and then i'm making any operations on stream to cut, but is very slow. Thanks for any advice. Best regards
Load the whole file first.
In fact, you might just want to keep it as a char[] or byte[], depending on what kind of string manipulation you're doing.
here's some code to load a text file quickly
public static String loadTextFile(File f) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(f));
char data[] = new char[(int)f.length()];
int got = 0;
do {
got += br.read(data, got, data.length - got);
}
while (got < data.length);
return new String(data);
}
tjacobs01,
Would your solution also speed-up adding text to a JTextArea? For example would this pseudo-code
...
JTextArea text = new JTextArea
...
public static void addFileText(File f){
BufferedReader br = new BufferedReader(new FileReader(f));
StringBuffer mystring = new StringBuffer();
String s;
while((s=br.readLine())!=null){
mystring.append(s + "\n");
}
text.setText(mystring.toString());
}
work faster than this pseudo-code
...
JTextArea text = new JTextArea
...
public static void addFileText(File f){
BufferedReader br = new BufferedReader(new FileReader(f));
String s;
while((s=br.readLine())!=null){
text.append(s+ "\n");
}
}
I don't know the answer. I have an application that reads text from a file, adds it to a JTextArea, and then adds the JTextArea to a new tab in a JTabbedPane. Unfortunately, the first new tab that I add is very slow. All subsequent tabs are added quickly. Profiling my code indicated that the file read was slowing everything down, which really doesn't make sense. Given that my files load very quickly in all subsequent tabs that are added. It seems that the problem is really with JTabbedPane. Any Ideas?