Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*SentenceRecorder.java
- 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
- */
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JButton;
- import javax.swing.JTextArea;
- import javax.swing.JScrollPane;
- import java.awt.GridBagLayout;
- import java.awt.GridBagConstraints;
- import static java.awt.GridBagConstraints.*;
- import java.awt.Insets;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.Dimension;
- import java.io.File;
- import java.nio.file.Files;//for readAllLines()
- import java.nio.file.Paths;//used to get the path of the file
- import java.util.ArrayList;//to work with the array list to save the sentences in an ArrayList
- import java.util.List;
- import java.util.Collection;
- import java.nio.charset.Charset;
- import java.nio.charset.StandardCharsets;
- import java.util.Scanner;
- import java.io.FileWriter;
- import java.lang.SecurityException;//if no permission to write to file
- import java.io.FileNotFoundException;//if the file can't be created
- import java.io.IOException;
- import java.util.Properties;//to get working directory
- import java.io.BufferedWriter;
- public class SentenceRecorder extends JFrame{
- private JLabel instructions;
- private JButton submit;
- private GridBagLayout gbLayout;
- private JButton clear;
- private JTextArea input;
- private String stringInput;
- private ArrayList< String > allSentences = new ArrayList< String >();//stores all the sentences in the file
- private String filePath;
- private String fileName;
- private File file;
- private BufferedWriter fileWriter;
- private String workingDir;
- private String separator;
- private Scanner scannerInput;//to input data into the file
- private Scanner scannerOutput;//to output data from the file
- private final static Charset ENCODING = StandardCharsets.UTF_8;
- //private Properties workingDir;
- /* private String string1;
- private String string2; */
- public SentenceRecorder(){
- super("List of sentences to remember");
- separator = System.getProperty("line.separator");//getting the system-dependent separator
- workingDir = System.getProperty("user.dir");
- instructions = new JLabel("Enter your sentence.");
- input = new JTextArea(10,12);//holds the text input
- submit = new JButton("Submit");//submit button
- clear = new JButton("Clear");//clear button
- stringInput = "";//initialize string to empty
- gbLayout = new GridBagLayout();
- //System.out.println("workingDir is " + workingDir);
- //input = new Scanner(System.in);
- fileName = "sample.txt";
- /* filePath = workingDir + "\\sample.txt";
- file = new File(filePath); */
- file = new File(workingDir, fileName);
- allSentences = fileInArray();//copying content of file in arrayList
- printSentenceArray();
- setLayout(gbLayout);//set layout of jframe
- add(instructions, new GridBagConstraints(0,0,2,1,0,0,CENTER,HORIZONTAL, new Insets(10,15,10,15),0,0));
- add(new JScrollPane(input), new GridBagConstraints(0,1,2,1,0.5,0.5,CENTER,BOTH, new Insets(10,15,10,15),10,10));
- add(submit, new GridBagConstraints(0,2,1,1,1.0,1.0,CENTER,HORIZONTAL,new Insets(10,15,10,15),1,1));
- add(clear, new GridBagConstraints(1,2,1,1,1.0,1.0,CENTER,HORIZONTAL,new Insets(10,15,10,15),1,1));
- //System.out.printf("Enter your sentence or end of file - ctrl+z or Enter+ctrl+d\n");
- ProcessButtonHandling handler1 = new ProcessButtonHandling();
- ClearButtonHandling handler2 = new ClearButtonHandling();
- submit.addActionListener(handler1);
- clear.addActionListener(handler2);
- }//end of constructor
- //inner class for event handlings
- private class ProcessButtonHandling implements ActionListener{
- public void actionPerformed(ActionEvent event){
- stringInput = input.getText();//copy text from textArea to string
- scannerInput = new Scanner(stringInput);
- try{
- fileWriter = new BufferedWriter( new FileWriter(file,true));
- }
- catch(SecurityException securityException){//if you don't have write access
- System.err.println("You don't have write access to this file");
- System.exit(1);
- }
- catch(FileNotFoundException fileNotFoundException){
- System.err.println("Error opening or creating the file");
- System.exit(1);
- }
- catch(IOException ioexception){
- System.err.println("General Error with IO");
- System.exit(1);
- }
- while(scannerInput.hasNext()){
- stringInput = scannerInput.nextLine();
- try{
- fileWriter.append(stringInput + separator);
- }
- catch(IOException ioexception){
- System.err.println("General Error with IO");
- ioexception.printStackTrace();
- System.exit(1);
- }
- //System.out.printf("Enter your sentence or end of file - ctrl+z or Enter+ctrl+d\n");
- }
- CloseFile();
- Clear();
- }//end of actionPerformed
- }//end of inner class
- private class ClearButtonHandling implements ActionListener{
- public void actionPerformed(ActionEvent event){
- Clear();
- }//end of actionPerformed
- }
- private void Clear(){
- stringInput = "";
- input.setText("");
- CloseFile();
- }
- public void CloseFile(){
- try{
- fileWriter.close();
- }
- catch(IOException ioexception){
- System.err.println("General Error with IO");
- ioexception.printStackTrace();
- System.exit(1);
- }
- }//closeFile
- public ArrayList<Object> fileInArray(){//save file text into array
- try{
- scannerOutput = new Scanner(new File(workingDir, fileName));
- }
- catch(FileNotFoundException fileNotFoundException){
- System.err.println("Error opening the file");
- System.exit(1);
- }
- Path path = Paths.get(fileName);
- return Files.readAllLines(path, ENCODING);
- }
- public void printSentenceArray(){
- System.out.println("Array contains: ");
- for(int count = 0; count < allSentences.size(); count++){
- System.out.printf("%s", allSentences.get(count));
- }
- }
- public String searchWord(){// search for keyword and return the whole string/s
- }
- }//end of SentenceRecorder
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement