Advertisement
gatoatigrado3

stack overflow problem Java BufferedWriter version

Apr 9th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. // Modified from http://javadb.com/write-to-file-using-bufferedwriter
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7.  
  8. /**
  9. *
  10. * @author javadb.com
  11. */
  12. public class Main {
  13.  
  14. /**
  15. * Prints some data to a file using a BufferedWriter
  16. */
  17. public void writeToFile(String filename) {
  18.  
  19. BufferedWriter bufferedWriter = null;
  20.  
  21. try {
  22.  
  23. //Construct the BufferedWriter object
  24. bufferedWriter = new BufferedWriter(new FileWriter(filename));
  25. for (int i = 1; i <= 10000000; i++) {
  26. bufferedWriter.write(String.valueOf(i));
  27. bufferedWriter.newLine();
  28. }
  29.  
  30. } catch (FileNotFoundException ex) {
  31. ex.printStackTrace();
  32. } catch (IOException ex) {
  33. ex.printStackTrace();
  34. } finally {
  35. //Close the BufferedWriter
  36. try {
  37. if (bufferedWriter != null) {
  38. bufferedWriter.flush();
  39. bufferedWriter.close();
  40. }
  41. } catch (IOException ex) {
  42. ex.printStackTrace();
  43. }
  44. }
  45. }
  46.  
  47. /**
  48. * @param args the command line arguments
  49. */
  50. public static void main(String[] args) {
  51. new Main().writeToFile("/tmp/file");
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement