Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Reading text file content into a JTextArea help

Here is my code and I can't seem to get the text file content to display when I run my program. Also I tried removing the txtNewNote.setEditable(false) still didn't work.

Code
JTextArea txtNewNote = new JTextArea();
if ("Savee".equals(ae.getActionCommand())) {
           int dialogButton = JOptionPane.YES_NO_OPTION;
           JOptionPane.showConfirmDialog(null, "Do you want to save?", "", dialogButton);
           txtNewNote.setEditable(false);

           String fileName = "CourseDetails.txt";
           String line;

           try { // writing

               BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true));
               txtNewNote.write(writer);
               writer.newLine();
               writer.close();

           } catch (IOException ex) {
               System.out.println("Error");
               // reading
               try (BufferedReader myReader = new BufferedReader(new FileReader("CourseDetails.txt"))) {
                   String text = null;
                   while ((text = myReader.readLine()) != null) {
                       txtNewNote.read(myReader, "CourseDetails");
                   }
               } catch (IOException exp) {
                   exp.printStackTrace();
               }
Code
if ("Savee".equals(ae.getActionCommand())) {

Could it be because you misspelled save?
(Mar 28, 2018 12:06 PM)m0noxide Wrote:  
Code
if ("Savee".equals(ae.getActionCommand())) {

Could it be because you misspelled save?

It's not it. I've named my button "Savee".


(Mar 28, 2018 05:01 PM)friiz1337 Wrote:  It's not it. I've named my button "Savee".

When the button is pressed it saves the info into a text file and that info I want to be displayed in the same text area where I write it. Basically a frame where I store notes on a text area :)
Try this to read, it must work:

Code
try {
        FileReader file = new FileReader("CourseDetails.txt");
        BufferedReader br = new BufferedReader(file);
        String line = br.readLine();

        while (line != null) {
            System.out.println(line);
            line = br.readLine();
        }
        br.close();
    } catch (IOException e) {
        System.out.println("Error: " + e);
    }
(Mar 28, 2018 05:06 PM)✔ Soufian ✔ Wrote:  Try this to read, it must work:

Code
   try {
       FileReader file = new FileReader("CourseDetails.txt");
       BufferedReader br = new BufferedReader(file);
       String line = br.readLine();

       while (line != null) {
           System.out.println(line);
           line = br.readLine();
       }
       br.close();
   } catch (IOException e) {
       System.out.println("Error: " + e);
   }

Doesn't work. And I have to change the string name since I've already defined it before.
What I want to do is whenever I type a text into that text area I want the text to be saved on a txt file and also to be read and shown by the text area. In short I want a text file(textarea) inside my program(frame)


(Mar 28, 2018 05:23 PM)friiz1337 Wrote:  Doesn't work. And I have to change the string name since I've already defined it before.
What I want to do is whenever I type a text into that text area I want the text to be saved on a txt file and also to be read and shown by the text area. In short I want a text file(textarea) inside my program(frame)

I've done the saving to a txt file part. But reading it and getting the output on the textarea is the big problem :/
Still struggling to figure out how to do it :/
Read from file
Code
    public static String readTextFromFile( String filepath ) throws IOException
    {
        BufferedReader reader = new BufferedReader( new FileReader( new File( filepath )));
        StringBuilder textFromFile = new StringBuilder();
        
        String line = "";
        while( ( line = reader.readLine() ) != null )
        {
            textFromFile.append( line + "\n" );
        }
        
        return textFromFile.toString();
    }

Write to file
Code
public static void writeToFile( String filepath, String textToWrite ) throws IOException
    {
        PrintWriter writer = new PrintWriter( new File( filepath ));
        writer.write( textToWrite );
        writer.close();
    }

Frame, don't know why i threw that one in..
Spoiler (Click to View)
You can try something like that


Code
public class ReadFileExample1 {

private static final String FILENAME = "E:\\test\\filename.txt";

public static void main(String[] args) {

BufferedReader br = null;
FileReader fr = null;

try {

//br = new BufferedReader(new FileReader(FILENAME));
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);

String sCurrentLine;

while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (br != null)
br.close();

if (fr != null)
fr.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

}


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by Formula 1Dec 6, 2019 01:11 AM 23 707 Dec 6, 2019 01:11 AM
Last Post: itcorp
[C] by pebblakOct 15, 2019 05:24 AM 6 305 Oct 15, 2019 05:24 AM
Last Post: Joseph Ducreux
[C] by nuvexy.Oct 6, 2019 03:13 PM 13 512 Oct 6, 2019 03:13 PM
Last Post: themayonnaiseguy