Results 1 to 18 of 18
  1. #1
    Graupner1000 is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default 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.
    Note: This is only a (very) small extract. Exactly what it is, is an entirely different(and long) story:D.

    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 12:22 PM.

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    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();
    You could alternatively look into DataInputStream.
    Last edited by Lil_Aziz1; 08-19-2010 at 05: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)

  3. #3
    Graupner1000 is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default

    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:)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    not possible to append any characters to the beginning of the lines I don`t need
    Do you mean not allowed here? Its certainly possible to prepend a character to a line
    newLine = "char" + oldLine; // prepend char to line

  5. #5
    Graupner1000 is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default

    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.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    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.

  7. #7
    Graupner1000 is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default

    EEEEExactly.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    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

  9. #9
    Graupner1000 is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default

    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?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    My problem is now filtering out the relevant lines
    A waste of time reading and saving all the lines if you only need the data from selected 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?

  11. #11
    Graupner1000 is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default

    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
    except the Mesh Tally Number #.
    I am using the BufferedReader class to read the file.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    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 #.
    Where does that fit in?

    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?

  13. #13
    Graupner1000 is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default

    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
    To this:

    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
    Preferably with the new data all in the same text file and in this format(As I`ve said there is allot more than shown above).

    Thanks for your help:)
    Last edited by Graupner1000; 08-27-2010 at 12:22 PM.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    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

  15. #15
    Graupner1000 is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default

    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

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    Yes, there are several ways to find the lines

  17. #17
    Graupner1000 is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default

    Could you give me some examples? It would save me a lot of time :D

    Thanks

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    Using indexOf would be something like this:
    String line = input.readLine(); // get next line from file into line
    if(line.indexOf( "Mesh Tally Number") >= 0) { // test if this line has desired contents
    .. save this line ...
    }

Similar Threads

  1. Using Read/Write From File Using Scanner
    By javaisntcoffee123 in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 03:35 AM
  2. Edit/Save Text File - Problems
    By ryanlbowen in forum New To Java
    Replies: 9
    Last Post: 07-20-2009, 05:23 PM
  3. Read and edit text file
    By VinTiger in forum New To Java
    Replies: 5
    Last Post: 05-14-2009, 01:18 AM
  4. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  5. Read and Write file
    By mrdestroy in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 12:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •