0

so I'm designing a text editor. For the Open/Save methods, I'm trying to use a TextArea (it doesn't have to be one, it's just my current method). Now, I have two problems right now:

1) When I load a file, it currently doesn't remove the contents currently in the text editor. For example, if I typed in "Owl", then loaded a file that contained "Rat", it would end up as "OwlRat". To solve this, I plan to use the replaceRange method (again however, it isn't absolute, any suggestions would be great!). However, I must replace all the contents of the text editor, not just selected text, and I can't figure out how to do that. Any tips?

2) Currently, when I load a file, nothing will happen unless I saved that file the same time I ran the application. So, for example, running the program, saving a file, closing the program, running the program again, and then loading the file will give nothing. I know this is because the String x doesn't carry over, but I can't think of anyway to fix it. Somebody suggested Vectors, but I don't see how they would help...

Here is the code for the Open/Save methods:

Open:

public void Open(String name){    
    File textFile = new File(name + ".txt.");
      BufferedReader reader = null;  
      try 
      {  
         textArea.append(x);
         reader = new BufferedReader( new FileReader( textFile));  
         reader.read();  
     }  
      catch ( IOException e)  
      {  
     }  
      finally 
     {  
         try 
          {  
             if (reader != null)  
                 reader.close();  
         }  
        catch (IOException e)  
         {                     
         }  
     }  
 } 

Save:

public void Save(String name){   
File textFile = new File(name + ".txt");
BufferedWriter writer = null;   
try  
{   
    writer = new BufferedWriter( new FileWriter(textFile));   
    writer.write(name);
    x = textArea.getText();

}   
catch ( IOException e)   
{   
}   
finally  
{   
   try  
  {   
           if ( writer != null)   
                    writer.close( );   
   }   
    catch ( IOException e)   
    {   
   }   
}  
}
tshepang
  • 10,772
  • 21
  • 84
  • 127
  • 2
    Way to log those exceptions. What's `x`? – Dave Newton Sep 06 '12 at 00:10
  • 1) Please use a consistent and logical indent for code blocks. 2) For every `catch`, add `e.printStackTrace();` 3) Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use it consistently. – Andrew Thompson Sep 06 '12 at 02:01
  • Consider using [`JTextComponent.read(Reader, Object)`](http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#read%28java.io.Reader,%20java.lang.Object%29) & [`write(Writer)`](http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#write%28java.io.Writer%29). More robust. – Andrew Thompson Sep 06 '12 at 03:41

3 Answers3

1

I had this same problem my guy friend, after much thought and research I even found a solution.

You can use the ArrayList to put all the contents of the TextArea and send as parameter by calling the save, as the writer just wrote string lines, then we use the "for" line by line to write our ArrayList in the end we will be content TextArea in txt file. if something does not make sense, I'm sorry is google translator and I who do not speak English.

Watch the Windows Notepad, it does not always jump lines, and shows all in one line, use Wordpad ok.


private void SaveActionPerformed(java.awt.event.ActionEvent evt) {

    String NameFile = Name.getText();
    ArrayList< String > Text = new ArrayList< String >();

    Text.add(TextArea.getText());

    SaveFile(NameFile, Text);
} 

public void SaveFile(String name, ArrayList< String> message) {

    path = "C:\\Users\\Paulo Brito\\Desktop\\" + name + ".txt";

    File file1 = new File(path);

    try {

        if (!file1.exists()) {

            file1.createNewFile();
        }


        File[] files = file1.listFiles();


        FileWriter fw = new FileWriter(file1, true);

        BufferedWriter bw = new BufferedWriter(fw);

        for (int i = 0; i < message.size(); i++) {

            bw.write(message.get(i));
            bw.newLine();
        }

        bw.close();
        fw.close();

        FileReader fr = new FileReader(file1);

        BufferedReader br = new BufferedReader(fr);

        fw = new FileWriter(file1, true);

        bw = new BufferedWriter(fw);

        while (br.ready()) {

            String line = br.readLine();

            System.out.println(line);

            bw.write(line);
            bw.newLine();

        }
        br.close();
        fr.close();

    } catch (IOException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error in" + ex);        
}
0

There's a lot going on here...

  1. What is 'x' (hint: it's not anything from the file!), and why are you appending it to the text area?
  2. BufferedReader.read() returns one character, which is probably not what you're expecting. Try looping across readline().
  3. Follow Dave Newton's advice to handle your exceptions and provide better names for your variables.
  4. The text file will persist across multiple invocation of your program, so the lack of data has nothing to do with that.

Good luck.

Alain Collins
  • 15,596
  • 2
  • 29
  • 53
0

Use textArea.setText(TEXT); rather than append; append means to add on to, so when you append text to a TextArea, you add that text to it. setText on the other hand will set the text, replacing the old text with the new one (which is what you want).

As far as why it's failing to read, you are not reading correctly. First of all, .read() just reads a single character (not what you want). Second, you don't appear to do anything with the returned results. Go somewhere (like here) to find out how to read the file properly, then take the returned string and do textArea.setText(readString);.

And like the others said, use e.printStackTrace(); in all of your catch blocks to make the error actually show up in your console.

Community
  • 1
  • 1
Alex Coleman
  • 6,611
  • 1
  • 18
  • 30