Alright guys, so the title explains exactly my goal. The truth is I'm going to be reading in a lot of data from an xml file. The file is too large and there's too much data to store in arraylists without running out of memory, so I'm reading and as I'm reading I'm going to write to a file.
This is the thing though, I already can do this and have it done, but I want to modify the program so you can choose what data you want to take out. To do this I would set up the text file to show something like this:
Where one, two, etc. are data names and the numbers are data separated by commas as you see.
Here's my problem. I will not run into all the data at one time. Every second is another piece of information, so when I get to that element in the xml I want to open the file, locate the correct data (what line), then replace that line, so it could look like this afterwards:
Now, I wrote something using RandomAccessFil e in java, here's all my code (it's a test program):
[code=java]
import java.util.*;
import java.io.*;
public class testwriteMoreTo File {
private RandomAccessFil e raf;
private static String fileToBe;
public void writeMoreToFile (String tagName, String newData) {
try {
try {
raf = new RandomAccessFil e( new File(fileToBe), "rw");
} catch (FileNotFoundEx ception e) {
raf = new RandomAccessFil e(fileToBe, "rw");
}
StringBuffer contents = new StringBuffer();
String line = null;
long prevFilePointer = 0;
while ((line = raf.readLine()) != null) {
if (line.substring (0,tagName.leng th()).equals(ta gName)) {
contents.append (line).append(n ewData).append( ",").append(Sys tem.getProperty ("line.separato r"));
raf.seek(prevFi lePointer);
raf.writeChars( contents.toStri ng());
break;
}
prevFilePointer = raf.getFilePoin ter();
}
} catch (IOException e2) {
e2.printStackTr ace();
} finally {
try {
raf.close();
} catch (IOException e2) {
e2.printStackTr ace();
}
}
}
public static void main(String args[]) {
String[] tagNames = new String[]{ "one","two","th ree","four","fi ve" };
String line = new String("1,2,3,4 ,5,");
String[] lines = new String[5];
fileToBe = "Z:\\test.t xt";
PrintWriter out = null;
for(int i = 0; i < lines.length; i++) {
lines[i] = tagNames[i]+":"+line;
}
try {
out = new PrintWriter(fil eToBe);
for (int i = 0; i < lines.length; i++) {
out.println(lin es[i]);
}
} catch (IOException e2) {
e2.printStackTr ace();
} finally {
out.close();
}
testwriteMoreTo File test = new testwriteMoreTo File();
test.writeMoreT oFile(tagNames[3], "6");
}
}
[/code]
I predicted this problem before I tried it, but thought maybe it could work so I'll try it anyway.
The output looks like this in the text file:
After I add the line (and it doesn't even seem to add correctly, why the spaces?) bytes after that probably don't read correctly..
Any ideas guys, just ideas, what do you think? Any better concepts?
I've got this one that I read somewhere:
Two files:
Copy first line of file 1 to file 2
repeat until you get to line you want to change
change the line, put it in file 2
continue till end copying to file 2
Problem is if I do this sooooo many times because of all the data I"ll need to do it with will this take a very long time? Even so, what, 300 copies of a file? I should delete the old one right, I believe something was mentioned about a kill method... Can I delete the old one and then change the new file name to be the old file name?
Thanks for all the help guys...
-blazed
This is the thing though, I already can do this and have it done, but I want to modify the program so you can choose what data you want to take out. To do this I would set up the text file to show something like this:
Code:
one:1,2,3,4,5, two:1,2,3,4,5, three:1,2,3,4,5, four:1,2,3,4,5, five:1,2,3,4,5,
Here's my problem. I will not run into all the data at one time. Every second is another piece of information, so when I get to that element in the xml I want to open the file, locate the correct data (what line), then replace that line, so it could look like this afterwards:
Code:
one:1,2,3,4,5, two:1,2,3,4,5, three:1,2,3,4,5, four:1,2,3,4,5,6, five:1,2,3,4,5,
[code=java]
import java.util.*;
import java.io.*;
public class testwriteMoreTo File {
private RandomAccessFil e raf;
private static String fileToBe;
public void writeMoreToFile (String tagName, String newData) {
try {
try {
raf = new RandomAccessFil e( new File(fileToBe), "rw");
} catch (FileNotFoundEx ception e) {
raf = new RandomAccessFil e(fileToBe, "rw");
}
StringBuffer contents = new StringBuffer();
String line = null;
long prevFilePointer = 0;
while ((line = raf.readLine()) != null) {
if (line.substring (0,tagName.leng th()).equals(ta gName)) {
contents.append (line).append(n ewData).append( ",").append(Sys tem.getProperty ("line.separato r"));
raf.seek(prevFi lePointer);
raf.writeChars( contents.toStri ng());
break;
}
prevFilePointer = raf.getFilePoin ter();
}
} catch (IOException e2) {
e2.printStackTr ace();
} finally {
try {
raf.close();
} catch (IOException e2) {
e2.printStackTr ace();
}
}
}
public static void main(String args[]) {
String[] tagNames = new String[]{ "one","two","th ree","four","fi ve" };
String line = new String("1,2,3,4 ,5,");
String[] lines = new String[5];
fileToBe = "Z:\\test.t xt";
PrintWriter out = null;
for(int i = 0; i < lines.length; i++) {
lines[i] = tagNames[i]+":"+line;
}
try {
out = new PrintWriter(fil eToBe);
for (int i = 0; i < lines.length; i++) {
out.println(lin es[i]);
}
} catch (IOException e2) {
e2.printStackTr ace();
} finally {
out.close();
}
testwriteMoreTo File test = new testwriteMoreTo File();
test.writeMoreT oFile(tagNames[3], "6");
}
}
[/code]
I predicted this problem before I tried it, but thought maybe it could work so I'll try it anyway.
The output looks like this in the text file:
Code:
one:1,2,3,4,5, two:1,2,3,4,5, three:1,2,3,4,5, f o u r : 1 , 2 , 3 , 4 , 5 , 6 ,
Any ideas guys, just ideas, what do you think? Any better concepts?
I've got this one that I read somewhere:
Two files:
Copy first line of file 1 to file 2
repeat until you get to line you want to change
change the line, put it in file 2
continue till end copying to file 2
Problem is if I do this sooooo many times because of all the data I"ll need to do it with will this take a very long time? Even so, what, 300 copies of a file? I should delete the old one right, I believe something was mentioned about a kill method... Can I delete the old one and then change the new file name to be the old file name?
Thanks for all the help guys...
-blazed
Comment