Results 1 to 9 of 9
Thread: Buffered Writter While Loop
- 05-10-2011, 08:42 PM #1
Member
- Join Date
- May 2011
- Posts
- 9
- Rep Power
- 0
- 05-10-2011, 08:49 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I have a try catch where the catch is looping forever, leading me to believe that the while loop is for some reason not incrementing.
If you want the increment to always happen, put it in the finally part of try/catch/finally.
- 05-10-2011, 08:58 PM #3
Member
- Join Date
- May 2011
- Posts
- 9
- Rep Power
- 0
- 05-10-2011, 09:18 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
You should not increment twice. If you increment as part of the for statement, then all the catch should do is continue.
With so much catching going on it's not completely clear what the problem is exactly. Perhaps you could post the code you are using now and describe the actual behaviour (in objective terms: ie what gets written to out.)
- 05-11-2011, 02:04 AM #5
Member
- Join Date
- May 2011
- Posts
- 9
- Rep Power
- 0
I've been able to fix the majority of my problems on my own, but now I'm having a huge problem. Once I figure it out however, I'm done.
I have several stringer getting fed information in a for loop, that information gets wrote into a text file then it is suppose to go down a line on the text file. However, it only wants to write the last string entered and never go down a line and write there.
The output is suppose to say:Java Code:private void mnu1Save_Click() { try{ rs.last(); rs.moveToCurrentRow(); int numrows = rs.getRow(); rs.first(); rs.moveToCurrentRow(); for(int i = 1; i <= numrows; i++){ try{ rs = stmt.executeQuery("SELECT Last, First, Street, City, State, Zip, Phone FROM MailList"); //Sets the cursor to the first entry rs.absolute(i); //Sets the strings to the information in specific row and column String Last = rs.getString("Last"); String First = rs.getString("First"); String Street = rs.getString("Street"); String City = rs.getString("City"); String State = rs.getString("State"); String Zip = rs.getString("Zip"); String Phone = rs.getString("Phone"); String Total = Last + "," + First + "," + Street + "," + City + "," + State + "," +Zip + "," +Phone; FileWriter fstream = new FileWriter("out.txt"); BufferedWriter out = new BufferedWriter(fstream); out.write( Total ); out.newLine(); out.close(); rs.next(); } catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } } catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } }
Morgan,Harry,123 Lotus Lane,Schenectady,NY,12345,518-445-4554
Trump,Donald,Borgata Room 999,Atlantic City,NJ,54321,305-445-5445
null,null,null,null,null,null,null
Only shows:
null,null,null,null,null,null,null
- 05-11-2011, 03:25 AM #6
That's expected, since you construct a new writer, write each line and then close the writer. Each time you write a line it overwrites the file created for the previous line.
Open the writer before the for loop and close it in the finally block (with a null check).
And fix your indentation if you expect people here to read your code.
Code Conventions for the Java(TM) Programming Language: Contents
db
- 05-11-2011, 03:43 AM #7
Member
- Join Date
- May 2011
- Posts
- 9
- Rep Power
- 0
I apologize. I have fixed my code (and hopefully my indentations)is this what you meant:
I am now getting a Stream Closed Error in the Terminal Window.Java Code:private void mnu1Save_Click() { String[] writeArray = new String[100]; try{ rs.last(); rs.moveToCurrentRow(); int numrows = rs.getRow(); rs.first(); rs.moveToCurrentRow(); FileWriter fstream = new FileWriter("out.txt"); BufferedWriter out = new BufferedWriter(fstream); for(int i = 1; i <= numrows; i++){ try{ rs = stmt.executeQuery("SELECT Last, First, Street, City, State, Zip, Phone FROM MailList"); //Sets the cursor to the first entry rs.absolute(i); //Sets the strings to the information in specific row and column Newstring[0] = rs.getString("Last"); Newstring[1] = rs.getString("First"); Newstring[2] = rs.getString("Street"); Newstring[3] = rs.getString("City"); Newstring[4] = rs.getString("State"); Newstring[5] = rs.getString("Zip"); Newstring[6] = rs.getString("Phone"); String concat = Newstring[0] + "," + Newstring[1]+ + "," +Newstring[2] + "," + Newstring[3] + "," + Newstring[4] + "," + Newstring[5] + "," + Newstring[6] "\n"; writeArray[i] = concat; out.write( writeArray[i] ); out.newLine(); rs.next(); } catch (Exception e){ System.err.println("Error: " + e.getMessage()); } finally { if (out != null) { out.close(); } } } } catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } }Last edited by Sobutai; 05-11-2011 at 04:08 AM.
- 05-11-2011, 04:08 AM #8
You're still closing the writer inside the loop.
db
- 05-11-2011, 04:12 AM #9
Member
- Join Date
- May 2011
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Buffered reader
By franckboy in forum New To JavaReplies: 1Last Post: 02-02-2011, 07:16 PM -
Developing Word Writter and Formatter
By Anchal in forum AWT / SwingReplies: 1Last Post: 04-06-2010, 03:35 AM -
Buffered Reader
By ilovepolluting in forum New To JavaReplies: 2Last Post: 02-04-2010, 09:16 AM -
hey everyone a quick buffered writter question
By xbox_nutter in forum New To JavaReplies: 0Last Post: 03-20-2009, 11:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks