Results 1 to 15 of 15
Thread: Creating Ouput Text File
- 05-23-2010, 11:32 AM #1
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
Creating Ouput Text File
I'm having trouble with how to generate 10 random numbers between 50 and 100 and write them to a text file.
this is what i got so far..i'm stuck at the part: output to a text file
Please help me, give me some ideas or advice on how to fix it or doing it.
Thanks in advance.
Moderator Edit: Code tags addedJava Code:import java.io.*; import java.util.Random; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class Project7PartB { public static void main(String args[]) { FileOutputStream out; // declare a file output object try { // Create a new file output stream // connected to "myfile.txt" out = new FileOutputStream("myfile.txt"); // Connect print stream to the output stream Rand = new PrintStream( out ); Random randomnumber = new Random(); int yes[] = new int[10]; for(int i=0; i < 10; i++) { yes[i] = randomnumber.nextInt(51) + 50; System.out.println(yes[i]); } } catch (Exception e){ System.err.println ("Error writing to file"); } } }Last edited by Fubarable; 05-23-2010 at 01:47 PM. Reason: Moderator Edit: Code tags added
-
Hello, and welcome to the forum. I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.
To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Best of luck, and again, welcome!Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
-
Have a look at the Sun input and output tutorials which you can find here: Lesson: Basic I/O
In particular, check out the sections on character output.
Much luck!
- 05-23-2010, 02:28 PM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 277
- Rep Power
- 4
Java Code:import java.util.Random; import java.io.FileWriter; import java.io.BufferedWriter; import java.io.IOException; public class Project7 { static final String fileName = "myfile.txt"; public static void main(String[] args) { try { BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); Random randomNumber = new Random(); int[] yes = new int[10]; String value; for(int i = 0; i < 10; i++) { yes[i] = randomNumber.nextInt(51) + 50; value = Integer.toString(yes[i]); writer.write(value + " "); } writer.close(); } catch(IOException e) { e.printStackTrace(); System.exit(1); } } }
- 05-23-2010, 03:23 PM #5
Member
- Join Date
- May 2010
- Posts
- 14
- Rep Power
- 0
Java Code:import java.io.*; import java.util.Random; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class Project7PartB { public static void main(String args[]) { FileOutputStream out; // declare a file output object PrintStream Rand; // declare variable Rand try { // Create a new file output stream // connected to "myfile.txt" out = new FileOutputStream("myfile.txt"); // Connect print stream to the output stream Rand = new PrintStream( out ); Random randomnumber = new Random(); int yes[] = new int[10]; for(int i=0; i < 10; i++) { yes[i] = randomnumber.nextInt(51)+ 50; System.out.println(yes[i]); // Write output to consule Rand.println(yes[i]); // Write output to file } } catch (Exception e){ System.err.println ("Error writing to file"); } } }
-
cselic closes his writer but erakhman doesn't. This may seem like a small point, but if you have enough of this sort of thing going on in a program, you'll risk running out of resources. I would recommend to both that the BufferedWriter be closed in a finally block after checking that it's not null.
- 05-23-2010, 04:10 PM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 277
- Rep Power
- 4
Well, I have problem with finally block.
if I write something like this:
Then I have error:Java Code:} catch(IOException e) { e.printStackTrace(); System.exit(1); } finally { writer.close(); } }
"The local variable writer may not have been initialized
Unhandled exception type IOException"
- 05-23-2010, 04:39 PM #8
Member
- Join Date
- May 2010
- Posts
- 14
- Rep Power
- 0
Thanks moderator, now i have completed my code :
Java Code:import java.io.*; import java.util.Random; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class Project7PartB { public static void main(String args[]) { FileOutputStream out=null; // declare a file output object PrintStream Rand; // declare variable Rand try { // Create a new file output stream // connected to "myfile.txt" out = new FileOutputStream("myfile.txt"); // Connect print stream to the output stream Rand = new PrintStream( out ); Random randomnumber = new Random(); int yes[] = new int[10]; for(int i=0; i < 10; i++) { yes[i] = randomnumber.nextInt(51)+ 50; System.out.println(yes[i]); // Write output to consule Rand.println(yes[i]); // Write output to file } } catch (Exception e){ System.err.println ("Error writing to file"); } finally { try { out.close(); // Close file } catch (Exception e) { System.err.println ("Error close file"); } } } }Last edited by erakhman; 05-23-2010 at 04:48 PM.
-
Sorry for nit-picking, and I'm no pro at this either, but I'd still modify this further by checking that out isn't null first before closing it and would try to use the most specific exception possible:
As always, corrections are most welcome!Java Code:try { //...... code deleted for sake of brevity } catch (FileNotFoundException e) { System.err.println("Error writing to file"); e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } }
- 05-23-2010, 04:54 PM #10
Member
- Join Date
- May 2010
- Posts
- 14
- Rep Power
- 0
Ok, i think you're right, thanks ya...
- 05-23-2010, 09:30 PM #11
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 277
- Rep Power
- 4
On this way my compiler continue to report an error, and requires writting another IOException.Java Code:Code: try { //...... code deleted for sake of brevity } catch (FileNotFoundException e) { System.err.println("Error writing to file"); e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } }
Something like this:
Java Code:try { } catch(FileNotFoundException e) { } [B][COLOR="Purple"]catch(IOException e) { }[/COLOR][/B] finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } }Last edited by cselic; 05-23-2010 at 09:35 PM.
-
Don't leave your catch block empty unless you are the type that enjoys driving a car with blindfolds on.
- 05-23-2010, 10:05 PM #13
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 277
- Rep Power
- 4
I don't leave them empty. I did it for simplicity ;)Don't leave your catch block empt
Sorry.
Complete code:
Java Code:} catch (FileNotFoundException e) { System.err.println("Error writing to file"); e.printStackTrace(); } [B][COLOR="Navy"]catch(IOException e) { e.printStackTrace(); }[/COLOR][/B] finally { try { if (writer != null) { writer.close(); } } catch (IOException e) { e.printStackTrace(); } }
- 05-24-2010, 03:42 AM #14
Member
- Join Date
- May 2010
- Posts
- 14
- Rep Power
- 0
try edit your code, like this :
Java Code:public static void main(String[] args) { BufferedWriter writer = null; try { writer = new BufferedWriter(new FileWriter(fileName));
- 05-24-2010, 09:56 AM #15
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 277
- Rep Power
- 4
Similar Threads
-
creating a graph from a text file
By chinolee in forum New To JavaReplies: 3Last Post: 11-23-2009, 08:36 AM -
Creating&Saving as a different Text file
By right2001 in forum New To JavaReplies: 2Last Post: 04-07-2009, 07:17 AM -
want sql Query ouput in the calling HTML Form.. Pls Help
By Faheem_Ahmed in forum Advanced JavaReplies: 2Last Post: 03-05-2009, 08:08 PM -
find and replace text from a text file
By gezzel in forum New To JavaReplies: 2Last Post: 09-19-2008, 04:04 PM -
creating a text based game
By Phobos0001 in forum New To JavaReplies: 1Last Post: 02-12-2008, 04:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks