Advertisement
Guest User

Untitled

a guest
Jan 8th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. /*SentenceRecorder.java
  2. takes a sentence or word as input and saves it on text file. Every new word/sentence is appended at the end of the file
  3. */
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JButton;
  7. import javax.swing.JTextArea;
  8. import javax.swing.JScrollPane;
  9. import java.awt.GridBagLayout;
  10. import java.awt.GridBagConstraints;
  11. import static java.awt.GridBagConstraints.*;
  12. import java.awt.Insets;
  13. import java.awt.event.ActionEvent;
  14. import java.awt.event.ActionListener;
  15. import java.awt.Dimension;
  16. import java.io.File;
  17.  
  18.  
  19. import java.nio.file.Files;//for readAllLines()
  20. import java.nio.file.Paths;//used to get the path of the file
  21. import java.util.ArrayList;//to work with the array list to save the sentences in an ArrayList
  22. import java.util.List;
  23. import java.util.Collection;
  24. import java.nio.charset.Charset;
  25. import java.nio.charset.StandardCharsets;
  26.  
  27. import java.util.Scanner;
  28. import java.io.FileWriter;
  29. import java.lang.SecurityException;//if no permission to write to file
  30. import java.io.FileNotFoundException;//if the file can't be created
  31. import java.io.IOException;
  32. import java.util.Properties;//to get working directory
  33. import java.io.BufferedWriter;
  34.  
  35. public class SentenceRecorder extends JFrame{
  36. private JLabel instructions;
  37. private JButton submit;
  38. private GridBagLayout gbLayout;
  39. private JButton clear;
  40. private JTextArea input;
  41. private String stringInput;
  42. private ArrayList< String > allSentences = new ArrayList< String >();//stores all the sentences in the file
  43. private String filePath;
  44. private String fileName;
  45. private File file;
  46. private BufferedWriter fileWriter;
  47. private String workingDir;
  48. private String separator;
  49. private Scanner scannerInput;//to input data into the file
  50. private Scanner scannerOutput;//to output data from the file
  51. private final static Charset ENCODING = StandardCharsets.UTF_8;
  52. //private Properties workingDir;
  53. /* private String string1;
  54. private String string2; */
  55. public SentenceRecorder(){
  56. super("List of sentences to remember");
  57. separator = System.getProperty("line.separator");//getting the system-dependent separator
  58. workingDir = System.getProperty("user.dir");
  59. instructions = new JLabel("Enter your sentence.");
  60. input = new JTextArea(10,12);//holds the text input
  61. submit = new JButton("Submit");//submit button
  62. clear = new JButton("Clear");//clear button
  63. stringInput = "";//initialize string to empty
  64. gbLayout = new GridBagLayout();
  65. //System.out.println("workingDir is " + workingDir);
  66. //input = new Scanner(System.in);
  67. fileName = "sample.txt";
  68. /* filePath = workingDir + "\\sample.txt";
  69. file = new File(filePath); */
  70. file = new File(workingDir, fileName);
  71. allSentences = fileInArray();//copying content of file in arrayList
  72. printSentenceArray();
  73. setLayout(gbLayout);//set layout of jframe
  74. add(instructions, new GridBagConstraints(0,0,2,1,0,0,CENTER,HORIZONTAL, new Insets(10,15,10,15),0,0));
  75. add(new JScrollPane(input), new GridBagConstraints(0,1,2,1,0.5,0.5,CENTER,BOTH, new Insets(10,15,10,15),10,10));
  76. add(submit, new GridBagConstraints(0,2,1,1,1.0,1.0,CENTER,HORIZONTAL,new Insets(10,15,10,15),1,1));
  77. add(clear, new GridBagConstraints(1,2,1,1,1.0,1.0,CENTER,HORIZONTAL,new Insets(10,15,10,15),1,1));
  78. //System.out.printf("Enter your sentence or end of file - ctrl+z or Enter+ctrl+d\n");
  79.  
  80. ProcessButtonHandling handler1 = new ProcessButtonHandling();
  81. ClearButtonHandling handler2 = new ClearButtonHandling();
  82. submit.addActionListener(handler1);
  83. clear.addActionListener(handler2);
  84. }//end of constructor
  85. //inner class for event handlings
  86. private class ProcessButtonHandling implements ActionListener{
  87. public void actionPerformed(ActionEvent event){
  88. stringInput = input.getText();//copy text from textArea to string
  89. scannerInput = new Scanner(stringInput);
  90. try{
  91. fileWriter = new BufferedWriter( new FileWriter(file,true));
  92. }
  93. catch(SecurityException securityException){//if you don't have write access
  94. System.err.println("You don't have write access to this file");
  95. System.exit(1);
  96. }
  97. catch(FileNotFoundException fileNotFoundException){
  98. System.err.println("Error opening or creating the file");
  99. System.exit(1);
  100. }
  101. catch(IOException ioexception){
  102. System.err.println("General Error with IO");
  103. System.exit(1);
  104. }
  105. while(scannerInput.hasNext()){
  106. stringInput = scannerInput.nextLine();
  107. try{
  108. fileWriter.append(stringInput + separator);
  109. }
  110. catch(IOException ioexception){
  111. System.err.println("General Error with IO");
  112. ioexception.printStackTrace();
  113. System.exit(1);
  114. }
  115. //System.out.printf("Enter your sentence or end of file - ctrl+z or Enter+ctrl+d\n");
  116. }
  117. CloseFile();
  118. Clear();
  119. }//end of actionPerformed
  120.  
  121. }//end of inner class
  122. private class ClearButtonHandling implements ActionListener{
  123. public void actionPerformed(ActionEvent event){
  124. Clear();
  125. }//end of actionPerformed
  126. }
  127. private void Clear(){
  128. stringInput = "";
  129. input.setText("");
  130. CloseFile();
  131. }
  132. public void CloseFile(){
  133. try{
  134. fileWriter.close();
  135. }
  136. catch(IOException ioexception){
  137. System.err.println("General Error with IO");
  138. ioexception.printStackTrace();
  139. System.exit(1);
  140.  
  141. }
  142. }//closeFile
  143. public ArrayList<Object> fileInArray(){//save file text into array
  144. try{
  145. scannerOutput = new Scanner(new File(workingDir, fileName));
  146. }
  147. catch(FileNotFoundException fileNotFoundException){
  148. System.err.println("Error opening the file");
  149. System.exit(1);
  150. }
  151. Path path = Paths.get(fileName);
  152. return Files.readAllLines(path, ENCODING);
  153.  
  154. }
  155. public void printSentenceArray(){
  156. System.out.println("Array contains: ");
  157. for(int count = 0; count < allSentences.size(); count++){
  158. System.out.printf("%s", allSentences.get(count));
  159. }
  160.  
  161. }
  162. public String searchWord(){// search for keyword and return the whole string/s
  163.  
  164.  
  165. }
  166. }//end of SentenceRecorder
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement