Results 1 to 15 of 15
- 09-14-2008, 12:49 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
[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.");} } }
- 09-14-2008, 01:33 AM #2it is not getting any data
String goto_ = new String("GOTO/");
System.out.println(goto_);
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();
What does your program output to the console?
- 09-14-2008, 02:23 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
- 09-14-2008, 03:31 AM #4At the end of the program.
- 09-14-2008, 03:50 AM #5
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
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
- 09-14-2008, 06:31 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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(); } } }
- 09-14-2008, 08:58 AM #7
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
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(); } } }
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
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
- 09-14-2008, 10:15 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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(); } } }
- 09-14-2008, 08:52 PM #9
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
Sanity checking today!
: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 } }
- 09-15-2008, 05:01 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
I hope you can easily identify what my code done with text files.
- 09-15-2008, 06:12 AM #11
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
Trouble adding a header and footer...
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
- 09-17-2008, 06:45 AM #12
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
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()); } } }
- 09-17-2008, 07:26 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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(); } } }
- 09-18-2008, 05:35 AM #14
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
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!
:)
- 09-18-2008, 05:41 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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
-
Help with dice game...student seeking advice
By waparson in forum New To JavaReplies: 3Last Post: 07-21-2008, 04:31 PM -
Advice on best method for....
By shaungoater in forum Java 2DReplies: 1Last Post: 06-23-2008, 08:36 PM -
I need some advice and tips about my code...
By sukatoa in forum Advanced JavaReplies: 3Last Post: 06-23-2008, 08:29 PM -
Want Some Expert Advice? JAX India 2008 Might Be the Right Place
By james in forum Java SoftwareReplies: 2Last Post: 03-03-2008, 09:05 PM -
Prerequisite advice needed
By Hatrabbit in forum New To JavaReplies: 2Last Post: 11-30-2007, 05:38 PM
Bookmarks