Results 1 to 15 of 15
  1. #1
    Zrob is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default [SOLVED] Advice on current code and a new direction...

    Hi everyone, here is some code that is half working, but in the if statement I am trying to print the output from one file to another new one called xyNew.txt though its being created it is not getting any data. So Ideas needed here?

    Also, should I take this in another direction? My goal is to take out all the "Print" statements in the original file and replace with "GOTO/" then to split each line at the first "x2" and put it on a new line also adding the "GOTO/" so it looks like this:

    GOTO/ x1=23.7905 y1=32.4554 z1=0.0000
    GOTO/ x2=24.3722 y2=31.9628 z2=0.0000

    So if you can just guide me in the right direction as to the correct Java "tools" to use that would be a big help.

    Here is what I have now, and thanks in advance!

    Rob

    PHP Code:
    package apt_v2.APT_v2;
    import java.io.*;
    
    class FileReaderDemo {
    public static void main(String args[]) throws Exception {
        
    FileWriter fw;
    FileReader fr = new FileReader("C:/temp/xy.txt");
    BufferedReader br = new BufferedReader(fr);
    
    try {fw = new FileWriter("C:/temp/xyNew.txt");}
    catch(IOException exc){System.out.println("Cannot open file.");
    return;}
    
    String s;
    while((s = br.readLine()) != null) {
    //fw.append(s);
    System.out.println(s);
    //fw.write(s);
    }
    String str;
    if((str = br.readLine()) != null) {
    str = br.readLine();
    String goto_ = new String("GOTO/");
    System.out.println(goto_);
    fw.write(str);
    } 
    
    
    try {fr.close();}
    catch(IOException exc){System.out.println("Error Closing file.");}
    
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    it is not getting any data
    Where do you close the output file?
    String goto_ = new String("GOTO/");
    System.out.println(goto_);
    What are those statement supposed to do? Why not:
    System.out.println("GOTO/");

    Your code reads two lines one after the other without using the first one for anything:
    if((str = br.readLine()) != null) {
    str = br.readLine();
    To copy one file to another you need a loop that will continue reading/writing until the end of the input file. You program might copy one line, the second one.

    What does your program output to the console?

  3. #3
    Zrob is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Where do you close the output file?
    At the end of the program.

    Quote Originally Posted by Norm View Post
    What are those statement supposed to do? Why not:
    System.out.println("GOTO/");
    Good point!

    Quote Originally Posted by Norm View Post
    Your code reads two lines one after the other without using the first one for anything:


    To copy one file to another you need a loop that will continue reading/writing until the end of the input file. You program might copy one line, the second one.

    What does your program output to the console?
    Ok I will try the loop,

    Thanks for the advice!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    At the end of the program.
    Is there more to the program than you posted? I didn't see the fw.close();

  5. #5
    Zrob is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    You are right, but after I put the File Close statement in its still not working. Strange .......not even the screen display is working now......


    LOL Oh well

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And also you can do all close operation in a finally block. No need to use another try block within the code.

    Java Code:
    class FileReaderDemo {
    
        public static void main(String args[]) throws Exception {
            FileWriter fw = null;
            FileReader fr = null;
            BufferedReader br = null;
    
            try {
                fr = new FileReader("C:/xy.txt");
                fw = new FileWriter("C:/xyNew.txt");
                br = new BufferedReader(fr);
    
                String s;
                while((s = br.readLine()) != null) {
                    //fw.append(s);
                    System.out.println(s);
                    fw.write(s);
                }
    
                String str;
                if((str = br.readLine()) != null) {
                    str = br.readLine();
                    String goto_ = new String("GOTO/");
                    System.out.println(goto_);
                    fw.write(str);
                }             
            }
            catch(IOException exc){
                System.out.println("Cannot open file.");
                return;
            }
            finally {
                fr.close();
                fw.close();
            }
        }
    }

  7. #7
    Zrob is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    And also you can do all close operation in a finally block. No need to use another try block within the code.
    I like the way you arranged the code in your example very neat! So I went back and cleaned mine up as well. I am now back on track, but I need some advice. My next step is to copy this output into the "xyNew" file and edit that new file, I have to replace words, and break up lines. So any advice will be great. I will also post the file I am working with in a code window bellow.

    Thanks for the help!

    Java Code:
    package apt_v3;
    import java.io.*;
    
    class FileReaderDemo {
        public static void main(String args[]) throws Exception {
            FileWriter fw = null;
            FileReader fr = null;
            BufferedReader br = null;
            
            fr = new FileReader("C:/temp/xy.txt");
            br = new BufferedReader(fr);
    
        try {
            fw = new FileWriter("C:/temp/xyNew.txt");
        
            String s;
            while((s = br.readLine()) != null) {
            System.out.println(s);
            }
        }
    catch(IOException exc){
        System.out.println("Cannot open file.");
        return;
    }
    finally {
    fr.close();
    fw.close();
        }
      }
    }
    This is my xy.txt file:
    Java Code:
    ============================================================
    Information listing created by :  Zrob
    Date                           :  9/10/2008 5:14:54 PM
    Current work part              :  E:\APT File Catia\APT_H4244101-117-No-Arcs.prt
    Node name                      :  ae9
    ============================================================
    line x1=23.7905 y1=32.4554 z1=0.0000 x2=24.3722 y2=31.9628 z2=0.0000
    line x1=24.3722 y1=31.9628 z1=0.0000 x2=24.2825 y2=1.7603 z2=0.0000
    line x1=24.2825 y1=1.7603 z1=0.0000 x2=24.0935 y2=1.5272 z2=0.0000
    line x1=24.0935 y1=1.5272 z1=0.0000 x2=24.0212 y2=1.2361 z2=0.0000
    line x1=24.0212 y1=1.2361 z1=0.0000 x2=23.8130 y2=1.0260 z2=0.0000
    line x1=23.8130 y1=1.0260 z1=0.0000 x2=23.6411 y2=0.9364 z2=0.0000
    line x1=23.6411 y1=0.9364 z1=0.0000 x2=17.4299 y2=0.9224 z2=0.0000
    line x1=12.5295 y1=-0.0598 z1=0.0000 x2=16.8089 y2=0.8560 z2=0.0000
    line x1=11.8936 y1=-0.0316 z1=0.0000 x2=0.4170 y2=-0.0183 z2=0.0000
    line x1=-0.0381 y1=0.9120 z1=0.0000 x2=-0.0074 y2=7.5939 z2=0.0000
    line x1=-0.0074 y1=7.5939 z1=0.0000 x2=-0.0529 y2=31.9031 z2=0.0000
    line x1=-0.0529 y1=31.9031 z1=0.0000 x2=0.1935 y2=32.1614 z2=0.0000
    line x1=0.1935 y1=32.1614 z1=0.0000 x2=0.5160 y2=32.3941 z2=0.0000
    line x1=0.5160 y1=32.3941 z1=0.0000 x2=5.7424 y2=32.3931 z2=0.0000
    line x1=5.7424 y1=32.3931 z1=0.0000 x2=23.7905 y2=32.4554 z2=0.0000
    line x1=0.0275 y1=0.6069 z1=0.0000 x2=-0.0513 y2=0.7226 z2=0.0000
    line x1=-0.0381 y1=0.9120 z1=0.0000 x2=-0.0513 y2=0.7226 z2=0.0000
    line x1=0.4170 y1=-0.0183 z1=0.0000 x2=0.0275 y2=0.6069 z2=0.0000
    line x1=12.5295 y1=-0.0598 z1=0.0000 x2=11.8936 y2=-0.0316 z2=0.0000
    line x1=17.4299 y1=0.9224 z1=0.0000 x2=16.8089 y2=0.8560 z2=0.0000
    My Goal is this, delete the top portion, add "GOTO/" and split the line in half to a new line, like this:

    Java Code:
    GOTO/ x1=23.7905 y1=32.4554 z1=0.0000 
    GOTO/ x2=24.3722 y2=31.9628 z2=0.0000
    GOTO/ x1=24.3722 y1=31.9628 z1=0.0000 
    GOTO/ x2=24.2825 y2=1.7603 z2=0.0000

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Here is the much simpler way to do it.

    Java Code:
    import java.io.*;
    
    class FileReaderDemo {
    
        public static void main(String args[]) throws Exception {
            FileWriter fw = null;
            FileReader fr = null;
            BufferedReader br = null;
    
            try {
                fr = new FileReader("C:/xy.txt");
                fw = new FileWriter("C:/xyNew.txt");
                br = new BufferedReader(fr);
    
                String s;
                while((s = br.readLine()) != null) {
                    if(s.substring(0, 4).equals("line"))
                    {
                        String temp = s.substring(5);
                        String[] ar = null;
                        ar = temp.split(" ", -1);
                        
                        for(int i = 0; i < ar.length - 2; i = i + 3) {
                            fw.write("GOTO/ " + ar[i] + " " + ar[i + 1] + " " + ar[i + 2]);
                            fw.write(System.getProperty("line.separator"));
                        }
                    }
                }
            }
            catch(IOException ex){
                System.out.println("Cannot open file.");
                return;
            }
            finally {
                fr.close();
                fw.close();
            }
        }
    }

  9. #9
    Zrob is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default Sanity checking today!

    Quote Originally Posted by Eranga View Post
    Here is the much simpler way to do it.

    :cool:

    WOW!
    Thanks a bunch! I love it!!
    I have been studying this all day, and this is exactly what I needed to do. Notice I put sanity checks in.......LOL

    Java Code:
    if(s.substring(0, 4).equals("line"))
                {
                    String temp = s.substring(5);
    System.out.println("variable test temp = " +temp);//sanity check
                    String[] ar = null;
                    ar = temp.split(" ", -1);
    
    System.out.println("variable test ar = " +ar[0]);//sanity check
    System.out.println("variable test ar = " +ar[0]+ar[1]);//sanity check
    System.out.println("variable test ar = " +ar[0]+ar[1]+ar[2]);//sanity check
    
                    for(int i = 0; i < ar.length - 2; i = i + 3) {
                        fw.write("GOTO/ " + ar[i] + " " + ar[i + 1] + " " + ar[i + 2]);
                        fw.write(System.getProperty("line.separator"));
    System.out.println("test1: GOTO/ " + ar[i]);//sanity check
    System.out.println("test2: GOTO/ " + ar[i] + " ");//sanity check
    System.out.println("test3: GOTO/ " + ar[i] + " " + ar[i + 1]);//sanity check
    System.out.println("test4: GOTO/ " + ar[i] + " " + ar[i + 1] + " ");//sanity check
    System.out.println("test5: GOTO/ " + ar[i] + " " + ar[i + 1] + " " + ar[i + 2]);//sanity check
    
                        
                        
                    }
                }

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  11. #11
    Zrob is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default Trouble adding a header and footer...

    Quote Originally Posted by Eranga View Post
    I hope you can easily identify what my code done with text files.

    Well, I can't say it was easy for me to understand it all, but I do have a much better idea now. I have made some changes and I will post the code and also the new output.

    But I do need some new advice on adding a header and footer to this. I am thinking that I need to change the data from an array back to a string, in order to insert a header and append the footer to it, am I on the right track here?

    Here is the small code changes that I did:

    Java Code:
    package apt_v5;
    
    import java.io.*;
    
    class FileReaderDemo {
        public static void main(String args[]) throws Exception {
            FileWriter fw = null;
            FileReader fr = null;
            BufferedReader br = null;
            
            fr = new FileReader("C:/temp/xy.txt");
            br = new BufferedReader(fr);
    
        try {
            fw = new FileWriter("C:/temp/xyNew.txt");
        
            String s;
            while((s = br.readLine()) != null) {
                s = s.replace("x1=", " "); // Remove unwanted Items
                s = s.replace("y1=", "  ");
                s = s.replace("z1=", "  ");
                s = s.replace("x2=", "  "); 
                s = s.replace("y2=", "  ");
                s = s.replace("z2=", "  ");
            if(s.substring(0, 4).equals("line"))
                {
                    String temp = s.substring(5);
                    String[] ar = null;
                    ar = temp.split("  ");//was 1 space and -1 (added a space)
    
                    for(int i = 0; i < ar.length - 2; i = i + 3) {//-2 +3
                        fw.write("GOTO/ " + ar[i] + ", " + ar[i + 1] + ", " + ar[i + 2]);
                        fw.write(System.getProperty("line.separator"));
                        
                    }
                }
             }
         }
    catch(IOException exc){
        System.out.println("Cannot open file.");
        return;
    }
    finally {
    fr.close();
    fw.close();
        }
      }
    }

    Example of the new output, just need a header and footer now.

    Java Code:
    GOTO/  24.2825,  1.7603,  0.0000
    GOTO/  24.0935,  1.5272,  0.0000
    GOTO/  24.0935,  1.5272,  0.0000
    GOTO/  24.0212,  1.2361,  0.0000
    GOTO/  24.0212,  1.2361,  0.0000
    GOTO/  23.8130,  1.0260,  0.0000
    GOTO/  23.8130,  1.0260,  0.0000
    GOTO/  23.6411,  0.9364,  0.0000
    GOTO/  23.6411,  0.9364,  0.0000
    GOTO/  17.4299,  0.9224,  0.0000

  12. #12
    Zrob is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default Footer added, but I am still having trouble with inserting a header?

    Well, I have made some progress on the footer, BUT I'll be darned if I can not figure out on how to insert a header?? :eek: or just some text, say two lines; to an existig txt document or within this code, any ideas....

    Help Please....:D

    Java Code:
    package apt_v5;
    
    import java.io.*;
    
    class FileReaderDemo {
        public static void main(String args[]) throws Exception {
            FileWriter fw = null;
            FileReader fr = null;
            BufferedReader br = null;
            
            fr = new FileReader("C:/temp/xy.txt");
            br = new BufferedReader(fr);
    
        try {
            fw = new FileWriter("C:/temp/xyNew.txt");
        
            String s;
            while((s = br.readLine()) != null) {
                s = s.replace("x1=", " "); // Remove unwanted Items
                s = s.replace("y1=", "  ");
                s = s.replace("z1=", "  ");
                s = s.replace("x2=", "  "); 
                s = s.replace("y2=", "  ");
                s = s.replace("z2=", "  ");
            if(s.substring(0, 4).equals("line"))
                {
                    String temp = s.substring(5);
                    String[] ar = null;
                    ar = temp.split("  ");//was 1 space and -1 (added a space)
    
                    for(int i = 0; i < ar.length - 2; i = i + 3) {//-2 +3
                        fw.write("GOTO/ " + ar[i] + ", " + ar[i + 1] + ", " + ar[i + 2]);
                        fw.write(System.getProperty("line.separator"));
                  }
               }
            }
         }
    catch(IOException exc){
        System.out.println("Cannot open file.");
        return;
    }
    finally {
    fr.close();
    fw.close();
        }
    FileWriter fw2 = null;
    try {
            String filename = "C:/temp/xyNew.txt";
            boolean append = true;
            fw2 = new FileWriter(filename,append);
    
            fw2.write("INSERT/ HEAD/OFF\n");//appends the string to the file
            fw2.close();
        }
    
        catch(IOException ioe){
        System.err.println("IOException: " + ioe.getMessage());
        }
    
      }
    }

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Do all alignments as you wish.

    Java Code:
    import java.io.*;
    
    class FileReaderDemo {
        
        private static final String newLine = System.getProperty("line.separator");
    
        public static void main(String args[]) throws Exception {
            FileWriter fw = null;
            FileReader fr = null;
            BufferedReader br = null;
    
            try {
                fr = new FileReader("C:/xy.txt");
                br = new BufferedReader(fr);
                fw = new FileWriter("C:/xyNew.txt");
    
                String s;
                fw.write("\tINSERT\tHEAD\tOFF" + newLine + newLine);
                while((s = br.readLine()) != null) {
                    s = s.replace("x1=", " "); // Remove unwanted Items
                    s = s.replace("y1=", "  ");
                    s = s.replace("z1=", "  ");
                    s = s.replace("x2=", "  "); 
                    s = s.replace("y2=", "  ");
                    s = s.replace("z2=", "  ");
                    if(s.substring(0, 4).equals("line")) {
                        String temp = s.substring(5);
                        String[] ar = null;
                        ar = temp.split("  ");//was 1 space and -1 (added a space)
    
                        for(int i = 0; i < ar.length - 2; i = i + 3) {//-2 +3
                            fw.write("GOTO/ " + ar[i] + ", " + ar[i + 1] + ", " + ar[i + 2]);
                            fw.write(newLine);
                        }
                    }
                }
            }
            catch(IOException exc){
                System.out.println("Cannot open file.");
                return;
            }
            finally {
                fr.close();
                fw.close();
            }
        }
    }

  14. #14
    Zrob is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    Thank you Eranga!

    I could not have done this without your help! Like I said before "hats off" to people like yourself that can write code!

    :)

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I like help others, I like to spend my knowledge on others. :) Actually I give codes to you, because you have tried something to do on your question before ask help here. I really like that. Keep going pal.

Similar Threads

  1. Help with dice game...student seeking advice
    By waparson in forum New To Java
    Replies: 3
    Last Post: 07-21-2008, 03:31 PM
  2. Advice on best method for....
    By shaungoater in forum Java 2D
    Replies: 1
    Last Post: 06-23-2008, 07:36 PM
  3. I need some advice and tips about my code...
    By sukatoa in forum Advanced Java
    Replies: 3
    Last Post: 06-23-2008, 07:29 PM
  4. Replies: 2
    Last Post: 03-03-2008, 08:05 PM
  5. Prerequisite advice needed
    By Hatrabbit in forum New To Java
    Replies: 2
    Last Post: 11-30-2007, 04:38 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
  •