Results 1 to 18 of 18
Thread: Read/edit/Write text file
- 08-19-2010, 05:53 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
Read/edit/Write text file
Hi all,
I have relatively little experience in Java. So to get straight to the point, I have a data set that looks as follows:
Java Code:KKM model E zones bottom Number of histories used for normalizing tallies = 13591000.00 Mesh Tally Number 44 COBALT Mesh tally for zones E28 to E17 This is a neutron mesh tally. Tally bin boundaries: Cylinder origin at 0.00E+00 0.00E+00 7.10E+02, axis in 0.000E+00 0.000E+00 1.000E+00 direction R direction: 0.00 201.60 Z direction: 0.00 26.30 52.60 78.90 105.20 131.50 157.80 184.10 210.40 236.70 263.00 268.00 291.00 Theta direction (revolutions): 0.000 1.000 Energy bin boundaries: 0.00E+00 1.00E+36 R Z Th Result Rel Error 100.800 13.150 0.500 4.96666E-18 6.36428E-01 100.800 39.450 0.500 7.37615E-17 6.26284E-01 100.800 65.750 0.500 3.02569E-13 9.97876E-01 100.800 92.050 0.500 8.20526E-15 3.34284E-01 100.800 118.350 0.500 9.31723E-14 2.17791E-01 100.800 144.650 0.500 9.07843E-13 1.55635E-01 100.800 170.950 0.500 1.01972E-11 9.25421E-02 100.800 197.250 0.500 1.41342E-10 8.25055E-02 100.800 223.550 0.500 1.81634E-09 3.39385E-02 100.800 249.850 0.500 4.28239E-08 6.11518E-03 100.800 265.500 0.500 1.71720E-07 4.59028E-03 100.800 279.500 0.500 2.82212E-06 1.71333E-03 Mesh Tally Number 54 This is a neutron mesh tally. Tally bin boundaries: Cylinder origin at 0.00E+00 0.00E+00 7.10E+02, axis in 0.000E+00 0.000E+00 1.000E+00 direction R direction: 0.00 201.60 Z direction: 0.00 26.30 52.60 78.90 105.20 131.50 157.80 184.10 210.40 236.70 263.00 268.00 291.00 Theta direction (revolutions): 0.000 1.000 Energy bin boundaries: 0.00E+00 1.00E+36 R Z Th Result Rel Error 100.800 13.150 0.500 3.92996E-19 6.46263E-01 100.800 39.450 0.500 5.32324E-18 5.89783E-01 100.800 65.750 0.500 2.39132E-13 9.99794E-01 100.800 92.050 0.500 5.46217E-16 3.68575E-01 100.800 118.350 0.500 6.21691E-15 2.21151E-01 100.800 144.650 0.500 6.15204E-14 1.62555E-01 100.800 170.950 0.500 6.85704E-13 1.02244E-01 100.800 197.250 0.500 9.52404E-12 9.64749E-02 100.800 223.550 0.500 1.29564E-10 2.72903E-02 100.800 249.850 0.500 3.10994E-09 5.51527E-03 100.800 265.500 0.500 1.41452E-08 3.93577E-03 100.800 279.500 0.500 1.96310E-07 1.76984E-03 Mesh Tally Number 64 Mesh tally for zones 28 to E17 This is a neutron mesh tally.
What I need to do is to get it into a format with the "Mesh Tally Number #" as a heading and the tables in a new text file. I have figured out how to parse the text and am currently kind of stuck on writing it to a new file. What I haven't even started yet is to apply some sort of method to extract the necessary data. Excel isn`t any good as it is displayed in a completely useless way and given that the procedure needs to be applied to many of these files (all over 150kb, the sample given above is only 3 kb:( ) manually stripping out the necessary data isn`t feasible.
If I would be very grateful if somebody could help me with the code for reading in the data and writing into a new file with the possibility of applying some sort of algorithm for extracting the relevant data, though that will probably be left to me as the tables do change slightly in format later on.
Thanks for your advice :)Last edited by Graupner1000; 08-27-2010 at 01:22 PM.
- 08-19-2010, 06:26 PM #2
Well if you can change the format and use it with Excel, I'm anticipating you can also change the format of this file completely. If you can, I would make the format more consistent. For example, everything that is NOT going to be parsed, insert '#' in the beginning of the line so it's easier for your program to distinguish what to write and what to omit. I would also remove the indentation from every line.
Reading and writing shouldn't be a problem. If this file is 3kb long, I would recommend using BufferedReader/Writer.
Java Code://Assume the 3kb file is called NuclearPhysics.txt and it is stored in the current //directory. ... BufferedReader fin = new BufferedReader(new FileReader("NuclearPhysics.txt")); BufferedWriter fout = new BufferedWriter(new FileWriter("RefurbishedNucPhysics.txt")); String line; while ((line = fin.readLine()) != null) { //Extracting the data you need will go here //... } fin.close(); fout.close();
Last edited by Lil_Aziz1; 08-19-2010 at 06:29 PM.
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 08-20-2010, 10:51 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
Thanks Lil_Aziz1, It works.
I have now managed to write the contents of one file to a new one. The next thing is trying to devise a method of getting only the relevant data. Unfortunately it is not possible to append any characters to the beginning of the lines I don`t need or remove the indentation(unless I use a method to seek out the relevant lines, which would make it a pointless exercise anyway). The program I am using(mcnp - Monte Carlo N particle) produces the output as in the example given above in a file without extension which can be viewed using the editor.
I am currently trying to find a suitable algorithm but any helpful suggestions would would be much appreciated.
Thanks you:)
- 08-20-2010, 02:27 PM #4not possible to append any characters to the beginning of the lines I don`t need
newLine = "char" + oldLine; // prepend char to line
- 08-20-2010, 03:05 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
Well something like that. I just mean that the method for prepending a character to the beginning of each relevant line would also be used to extract the very same line into a new file, so marking them first would not be ver efficient :D.
- 08-20-2010, 03:17 PM #6
Let me restate the problem: You want to read a file with multiple lines in different formats (example shown above), recognize certain lines as having data you want, extract that data and write the extracted data out to a new file in a new format.
- 08-20-2010, 03:33 PM #7
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
EEEEExactly.
- 08-20-2010, 03:36 PM #8
Then the questions are:
How to recognize the line with the data
How to extract the desired data from the line
How to format the output line with that data
- 08-23-2010, 08:42 AM #9
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
The way I`ve been going about it is extract the data line for line from the file, load each line as an element in an array(just an experiment) and then output them to a new text file. My problem is now filtering out the relevant lines and removing them from the array(Don`t know if this is the most efficient method). Been trying to use regex to do this but am having a few problems getting it to work(generally, not just getting lines).
Do you think this is a good method or am I wasting my time trying to get it to work?
- 08-24-2010, 12:20 AM #10My problem is now filtering out the relevant lines
How do you identify a line with the data to be saved? Regex can be used here as well as some String methods like indexOf.
Are the desired lines identifiable by themselves or are they in the context of other lines?
IE they must follow another line.
How do you extract the data from the line?
- 08-24-2010, 08:52 AM #11
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
Well, As I am trying to extract only the tables and their titles each column in the table has a header which are all in the same line and the same for every table(see example above), however the tables are of variable length. So all relevant data will follow
Java Code:R Z Th Result Rel Error
I am using the BufferedReader class to read the file.
- 08-24-2010, 03:16 PM #12
Read the input file until the line with the contents that you posted above.
You could use a String method like startsWith() or indexOf() to detect that line.
Then read the following lines and extract the data from those lines until you get to the line with ??? what.
Your earlier posting shows more than one section of the file that fits your requirements.
Will all the desired records be merged together in the output file or will each group be by itself some how.
except the Mesh Tally Number #.
Could you edit the input shown in your first post and show what parts of it are to go to the output file and in what format?
- 08-24-2010, 03:28 PM #13
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
Basically what I want is to go from this:
Java Code:KKM model E zones bottom Number of histories used for normalizing tallies = 13591000.00 Mesh Tally Number 44 COBALT Mesh tally for zones E28 to E17 This is a neutron mesh tally. Tally bin boundaries: Cylinder origin at 0.00E+00 0.00E+00 7.10E+02, axis in 0.000E+00 0.000E+00 1.000E+00 direction R direction: 0.00 201.60 Z direction: 0.00 26.30 52.60 78.90 105.20 131.50 157.80 184.10 210.40 236.70 263.00 268.00 291.00 Theta direction (revolutions): 0.000 1.000 Energy bin boundaries: 0.00E+00 1.00E+36 R Z Th Result Rel Error 100.800 13.150 0.500 4.96666E-18 6.36428E-01 100.800 39.450 0.500 7.37615E-17 6.26284E-01 100.800 65.750 0.500 3.02569E-13 9.97876E-01 100.800 92.050 0.500 8.20526E-15 3.34284E-01 100.800 118.350 0.500 9.31723E-14 2.17791E-01 100.800 144.650 0.500 9.07843E-13 1.55635E-01 100.800 170.950 0.500 1.01972E-11 9.25421E-02 100.800 197.250 0.500 1.41342E-10 8.25055E-02 100.800 223.550 0.500 1.81634E-09 3.39385E-02 100.800 249.850 0.500 4.28239E-08 6.11518E-03 100.800 265.500 0.500 1.71720E-07 4.59028E-03 100.800 279.500 0.500 2.82212E-06 1.71333E-03 Mesh Tally Number 54 This is a neutron mesh tally. Tally bin boundaries: Cylinder origin at 0.00E+00 0.00E+00 7.10E+02, axis in 0.000E+00 0.000E+00 1.000E+00 direction R direction: 0.00 201.60 Z direction: 0.00 26.30 52.60 78.90 105.20 131.50 157.80 184.10 210.40 236.70 263.00 268.00 291.00 Theta direction (revolutions): 0.000 1.000 Energy bin boundaries: 0.00E+00 1.00E+36 R Z Th Result Rel Error 100.800 13.150 0.500 3.92996E-19 6.46263E-01 100.800 39.450 0.500 5.32324E-18 5.89783E-01 100.800 65.750 0.500 2.39132E-13 9.99794E-01 100.800 92.050 0.500 5.46217E-16 3.68575E-01 100.800 118.350 0.500 6.21691E-15 2.21151E-01 100.800 144.650 0.500 6.15204E-14 1.62555E-01 100.800 170.950 0.500 6.85704E-13 1.02244E-01 100.800 197.250 0.500 9.52404E-12 9.64749E-02 100.800 223.550 0.500 1.29564E-10 2.72903E-02 100.800 249.850 0.500 3.10994E-09 5.51527E-03 100.800 265.500 0.500 1.41452E-08 3.93577E-03 100.800 279.500 0.500 1.96310E-07 1.76984E-03
Java Code:Mesh Tally Number 44 R Z Th Result Rel Error 100.800 13.150 0.500 4.96666E-18 6.36428E-01 100.800 39.450 0.500 7.37615E-17 6.26284E-01 100.800 65.750 0.500 3.02569E-13 9.97876E-01 100.800 92.050 0.500 8.20526E-15 3.34284E-01 100.800 118.350 0.500 9.31723E-14 2.17791E-01 100.800 144.650 0.500 9.07843E-13 1.55635E-01 100.800 170.950 0.500 1.01972E-11 9.25421E-02 100.800 197.250 0.500 1.41342E-10 8.25055E-02 100.800 223.550 0.500 1.81634E-09 3.39385E-02 100.800 249.850 0.500 4.28239E-08 6.11518E-03 100.800 265.500 0.500 1.71720E-07 4.59028E-03 100.800 279.500 0.500 2.82212E-06 1.71333E-03 Mesh Tally Number 54 R Z Th Result Rel Error 100.800 13.150 0.500 3.92996E-19 6.46263E-01 100.800 39.450 0.500 5.32324E-18 5.89783E-01 100.800 65.750 0.500 2.39132E-13 9.99794E-01 100.800 92.050 0.500 5.46217E-16 3.68575E-01 100.800 118.350 0.500 6.21691E-15 2.21151E-01 100.800 144.650 0.500 6.15204E-14 1.62555E-01 100.800 170.950 0.500 6.85704E-13 1.02244E-01 100.800 197.250 0.500 9.52404E-12 9.64749E-02 100.800 223.550 0.500 1.29564E-10 2.72903E-02 100.800 249.850 0.500 3.10994E-09 5.51527E-03 100.800 265.500 0.500 1.41452E-08 3.93577E-03 100.800 279.500 0.500 1.96310E-07 1.76984E-03
Thanks for your help:)Last edited by Graupner1000; 08-27-2010 at 01:22 PM.
- 08-24-2010, 03:44 PM #14
The logic:
Loop until end of file
Scan to the "Mesh Tally Number" record
copy it to the output file
Scan to the " R Z Th Result Rel Error" record
copy it to the output
Copy the following lines to the output file until reading a blank line.
Write a blank line to the output file
end of loop
- 08-24-2010, 04:33 PM #15
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
Seems logical,
I'll give "codifying" this a go when I get a chance. It seems as though it should be easy enough to find the relevant lines using regex(unless there's a better way)
Thanks
- 08-24-2010, 06:15 PM #16
Yes, there are several ways to find the lines
- 08-25-2010, 08:37 AM #17
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
Could you give me some examples? It would save me a lot of time :D
Thanks
- 08-25-2010, 03:04 PM #18
Similar Threads
-
Using Read/Write From File Using Scanner
By javaisntcoffee123 in forum New To JavaReplies: 4Last Post: 04-15-2010, 04:35 AM -
Edit/Save Text File - Problems
By ryanlbowen in forum New To JavaReplies: 9Last Post: 07-20-2009, 06:23 PM -
Read and edit text file
By VinTiger in forum New To JavaReplies: 5Last Post: 05-14-2009, 02:18 AM -
How to read and write to a file without taking out the comments in the file
By MAGNUM in forum New To JavaReplies: 5Last Post: 02-05-2009, 11:28 AM -
Read and Write file
By mrdestroy in forum New To JavaReplies: 13Last Post: 10-31-2008, 01:11 PM
Bookmarks