writting and reading a file

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

writting and reading a file

 
0
  #1
Nov 20th, 2008
I have to write two programs, one writes to file based on what the user selects. The other reads the file, and outputs what the user selects. And I'm really not sue if I'm doing this right.

Here is what I have for writting to the file:
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. /*
  6.  * class LogOrder
  7.  * Allows user to pick from five pieces of clothing to order
  8.  * user enters name and picks clothing and presses order.
  9.  * Stores users order to "order.txt"
  10.  * @author Kimberlie Davis
  11.  * @version 11/20/08
  12.  */
  13.  
  14. public class LogOrder
  15. {
  16. /*
  17.   * main method
  18.   * declares and sets frame as visible
  19.   * @param args not used
  20.   */
  21. public static void main(String args[])
  22. {
  23. EventQueue.invokeLater(
  24. new Runnable()
  25. {
  26. public void run()
  27. {
  28. LogOrderFrame mainFrame = new LogOrderFrame();
  29. mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30. mainFrame.setVisible(true);
  31. }
  32. });
  33.  
  34. }
  35. }
  36.  
  37. /*
  38.  * LogOrderFrame method
  39.  * sets up frame
  40.  * Declares and adds panel
  41.  */
  42. class LogOrderFrame extends JFrame
  43. {
  44. public LogOrderFrame()
  45. {
  46. this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  47. LogOrderPanel panel = new LogOrderPanel();
  48. this.add(panel);
  49. this.setTitle("Log order");
  50. }
  51. private static int DEFAULT_WIDTH = 500;
  52. private static int DEFAULT_HEIGHT = 500;
  53. }
  54.  
  55. /*
  56.  * LogOrderPanel method
  57.  * Layouts and panels
  58.  * Sets up panel with components
  59.  * adds actionListener to button
  60.  */
  61. class LogOrderPanel extends JPanel
  62. {
  63. public LogOrderPanel()
  64. {
  65. //set up Buffered writer
  66. try
  67. {
  68. writer = new BufferedWriter(new FileWriter("order.txt"));
  69. }
  70. catch(IOException e)
  71. {
  72. }
  73. //set up layouts
  74. this.setLayout(new BorderLayout());
  75.  
  76. //north
  77. JPanel north = new JPanel(new BorderLayout());
  78. this.add(north, BorderLayout.NORTH);
  79. //north center
  80. JPanel northCenter = new JPanel();
  81. north.add(northCenter, BorderLayout.CENTER);
  82.  
  83.  
  84. //center
  85. JPanel center = new JPanel();
  86. this.add(center, BorderLayout.CENTER);
  87. Box boxLayout = Box.createVerticalBox();
  88. center.add(boxLayout);
  89.  
  90. //set up components
  91. JLabel companyName = new JLabel("Clothing Inc.");
  92.  
  93. JLabel askName = new JLabel("Please enter your name:");
  94. enterName = new JTextField(5);
  95.  
  96. //Array of 5 checkboxes
  97. for(int i = 0; i < 5; i++)
  98. {
  99. check[i] = new JCheckBox();
  100. }
  101. check[0].setText("Jacket");
  102. check[1].setText("Shirt");
  103. check[2].setText("Pants");
  104. check[3].setText("Socks");
  105. check[4].setText("Shoes");
  106.  
  107. JButton order = new JButton("Order");
  108.  
  109. //add components
  110. northCenter.add(askName);
  111. northCenter.add(enterName);
  112. boxLayout.add(check[0]);
  113. boxLayout.add(check[1]);
  114. boxLayout.add(check[2]);
  115. boxLayout.add(check[3]);
  116. boxLayout.add(check[4]);
  117. north.add(companyName, BorderLayout.NORTH);
  118. this.add(order, BorderLayout.SOUTH);
  119.  
  120. ClothingChooser chooseAction = new ClothingChooser();
  121. order.addActionListener(chooseAction);
  122. }
  123.  
  124. /*
  125.   * ClothingChooser class
  126.   * implements ActionListener
  127.   * sets up actions for button
  128.   */
  129. private class ClothingChooser implements ActionListener
  130. {
  131. /*
  132.   * actionPerformed method
  133.   * checks what checkboxes have been selected
  134.   * stores users order to file "order.txt"
  135.   * @param ActionEvent
  136.   */
  137. public void actionPerformed(ActionEvent e)
  138. {
  139.  
  140. try
  141. {
  142. try
  143. {
  144. writer.write("Order:" + '\n');
  145. writer.write("For: " + enterName.getText() + '\n');
  146.  
  147. if(check[0].isSelected())
  148. writer.write('\t' + "Jacket" + '\n');
  149. if(check[1].isSelected())
  150. writer.write('\t' + "Shirt" + '\n');
  151. if(check[2].isSelected())
  152. writer.write('\t' + "Pants" + '\n');
  153. if(check[3].isSelected())
  154. writer.write('\t' + "Socks" + '\n');
  155. if(check[4].isSelected())
  156. writer.write('\t' + "Shoes" + '\n');
  157. writer.write('\n');
  158. }
  159. finally
  160. {
  161. JOptionPane.showMessageDialog(null, "Conratulations " + enterName.getText() + " your order was successful!");
  162. }
  163. }
  164. catch(IOException exception)
  165. {
  166. JOptionPane.showMessageDialog(null, "Error: Order not receive\n Please call Clothing Inc. to order");
  167. }
  168. }
  169. }
  170.  
  171. //datafields
  172. JCheckBox[] check = new JCheckBox[5];
  173. BufferedWriter writer;
  174. JTextField enterName;
  175. }
Did I do this correctly?
Last edited by christiangirl; Nov 20th, 2008 at 2:12 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: writting and reading a file

 
0
  #2
Nov 20th, 2008
I figured that out, but am still having problems. here is my new code:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. /*
  6.  * class LogOrder
  7.  * Allows user to pick from five pieces of clothing to order
  8.  * user enters name and picks clothing and presses order.
  9.  * Stores users order to "order.txt"
  10.  * @author Kimberlie Davis
  11.  * @version 11/20/08
  12.  */
  13.  
  14. public class LogOrder
  15. {
  16. /*
  17.   * main method
  18.   * declares and sets frame as visible
  19.   * @param args not used
  20.   */
  21. public static void main(String args[])
  22. {
  23. EventQueue.invokeLater(
  24. new Runnable()
  25. {
  26. public void run()
  27. {
  28. LogOrderFrame mainFrame = new LogOrderFrame();
  29. mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30. mainFrame.setVisible(true);
  31. }
  32. });
  33.  
  34. }
  35. }
  36.  
  37. /*
  38.  * LogOrderFrame method
  39.  * sets up frame
  40.  * Declares and adds panel
  41.  */
  42. class LogOrderFrame extends JFrame
  43. {
  44. public LogOrderFrame()
  45. {
  46. this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  47. LogOrderPanel panel = new LogOrderPanel();
  48. this.add(panel);
  49. this.setTitle("Log order");
  50. }
  51. private static int DEFAULT_WIDTH = 500;
  52. private static int DEFAULT_HEIGHT = 500;
  53. }
  54.  
  55. /*
  56.  * LogOrderPanel method
  57.  * Layouts and panels
  58.  * Sets up panel with components
  59.  * adds actionListener to button
  60.  */
  61. class LogOrderPanel extends JPanel
  62. {
  63. public LogOrderPanel()
  64. {
  65. //Writer
  66. writeTo = new File("order.txt");
  67. /*try
  68.   {
  69.   //toStream = new FileWriter(writeTo);
  70.   }
  71.   catch(IOException e)
  72.   {
  73.   }*/
  74. //set up layouts
  75. this.setLayout(new BorderLayout());
  76.  
  77. //north
  78. JPanel north = new JPanel(new BorderLayout());
  79. this.add(north, BorderLayout.NORTH);
  80. //north center
  81. JPanel northCenter = new JPanel();
  82. north.add(northCenter, BorderLayout.CENTER);
  83.  
  84.  
  85. //center
  86. JPanel center = new JPanel();
  87. this.add(center, BorderLayout.CENTER);
  88. Box boxLayout = Box.createVerticalBox();
  89. center.add(boxLayout);
  90.  
  91. //set up components
  92. JLabel companyName = new JLabel("Clothing Inc.");
  93.  
  94. JLabel askName = new JLabel("Please enter your name:");
  95. enterName = new JTextField(5);
  96.  
  97. //Array of 5 checkboxes
  98. for(int i = 0; i < 5; i++)
  99. {
  100. check[i] = new JCheckBox();
  101. }
  102. check[0].setText("Jacket");
  103. check[1].setText("Shirt");
  104. check[2].setText("Pants");
  105. check[3].setText("Socks");
  106. check[4].setText("Shoes");
  107.  
  108. JButton order = new JButton("Order");
  109.  
  110. //add components
  111. northCenter.add(askName);
  112. northCenter.add(enterName);
  113. boxLayout.add(check[0]);
  114. boxLayout.add(check[1]);
  115. boxLayout.add(check[2]);
  116. boxLayout.add(check[3]);
  117. boxLayout.add(check[4]);
  118. north.add(companyName, BorderLayout.NORTH);
  119. this.add(order, BorderLayout.SOUTH);
  120.  
  121. ClothingChooser chooseAction = new ClothingChooser();
  122. order.addActionListener(chooseAction);
  123. }
  124.  
  125. /*
  126.   * ClothingChooser class
  127.   * implements ActionListener
  128.   * sets up actions for button
  129.   */
  130. private class ClothingChooser implements ActionListener
  131. {
  132. /*
  133.   * actionPerformed method
  134.   * checks what checkboxes have been selected
  135.   * stores users order to file "order.txt"
  136.   * @param ActionEvent
  137.   */
  138. public void actionPerformed(ActionEvent e)
  139. {
  140. try
  141. {
  142. try
  143. {
  144. toStream = new FileWriter(writeTo);
  145. toStream.write("Order:" + '\n');
  146.  
  147. toStream.write("For: " + enterName.getText() + '\n');
  148.  
  149. if(check[0].isSelected())
  150. toStream.write('\t' + "Jacket" + '\n');
  151. if(check[1].isSelected())
  152. toStream.write('\t' + "Shirt" + '\n');
  153. if(check[2].isSelected())
  154. toStream.write('\t' + "Pants" + '\n');
  155. if(check[3].isSelected())
  156. toStream.write('\t' + "Socks" + '\n');
  157. if(check[4].isSelected())
  158. toStream.write('\t' + "Shoes" + '\n');
  159. toStream.write('\n');
  160. toStream.close();
  161. }
  162. finally
  163. {
  164. JOptionPane.showMessageDialog(null, "Conratulations " + enterName.getText() + " your order was successful!");
  165. }
  166. }
  167. catch(IOException exception)
  168. {
  169. JOptionPane.showMessageDialog(null, "Error: Order not receive\n Please call Clothing Inc. to order");
  170. }
  171. }
  172. }
  173.  
  174. //datafields
  175. JCheckBox[] check = new JCheckBox[5];
  176. File writeTo;
  177. JTextField enterName;
  178. FileWriter toStream;
  179. }

The problems I'm having now is inside of the file the text is not formatted how I want it, its supposed to be all on different lines. And also, if the user submits another order the file is supposed to show both, but it is only showing one.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 16
Reputation: vee_liang is an unknown quantity at this point 
Solved Threads: 1
vee_liang vee_liang is offline Offline
Newbie Poster

Re: writting and reading a file

 
1
  #3
Nov 20th, 2008
...
toStream = new FileWriter(writeTo);
toStream.write("Order:" + '\n');

...

The logic from that line is that each time you have a new order coming, it will "create" a new blank page. Maybe you can try to use 'if' to check whether or not your order.txt is empty. If it's not then just add your line(s).

Or easier if you just use your BufferedWriter code instead of that FileWriter code.

As for the line breakdown, if you are using BufferedWriter just add command:

  1. writer.newLine();
Vee Liang
To be right, first you have to know what wrong is.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: writting and reading a file

 
0
  #4
Nov 20th, 2008
I'm having trouble getting the if statement to work.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: writting and reading a file

 
1
  #5
Nov 20th, 2008
In what way?

You do know, it is always helpful if you post exactly what it is that is not happening as expected (along with all compiler/error messages).
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: writting and reading a file

 
0
  #6
Nov 20th, 2008
Sorry, I usually do I'm just tired lol.

Its not having an error, the file just isnt storying all submitted orders. It is still only storing the last one.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: writting and reading a file

 
0
  #7
Nov 20th, 2008
So take a look at liang's answer again. It has nothing to do with the if statement, it has to do with the fact that you are creating a new FileWriter with every "button-click", which will overwrite everything that's been written before it.

If you want to recreate the FileWriter every time then do it in append mode

  1. new FileWriter(file, true);

And, you should be closing the writer in the finally block, and, as your code is now, even if there is an exception, the "success" message will still be shown (that should be in the try block, and the close in the finally block).
Last edited by masijade; Nov 20th, 2008 at 5:01 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 16
Reputation: vee_liang is an unknown quantity at this point 
Solved Threads: 1
vee_liang vee_liang is offline Offline
Newbie Poster

Re: writting and reading a file

 
0
  #8
Nov 20th, 2008
so u know how to check whether or not the file is empty. Read the file then write it to your order.txt along with the new order.

that is the simplest way.
Vee Liang
To be right, first you have to know what wrong is.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: writting and reading a file

 
0
  #9
Nov 20th, 2008
Originally Posted by vee_liang View Post
so u know how to check whether or not the file is empty. Read the file then write it to your order.txt along with the new order.

that is the simplest way.
Uuuhhmm, what about "append"?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 16
Reputation: vee_liang is an unknown quantity at this point 
Solved Threads: 1
vee_liang vee_liang is offline Offline
Newbie Poster

Re: writting and reading a file

 
0
  #10
Nov 20th, 2008
what I meant by add line is 'append'. sorry for the misunderstanding, though. I thought that it is the common 'language'.
Vee Liang
To be right, first you have to know what wrong is.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 877 | Replies: 11
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC