Hack Forums
[Java] Reading text file content into a JTextArea help - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Reading text file content into a JTextArea help (/showthread.php?tid=5817356)



Reading text file content into a JTextArea help - friiz1337 - Mar 28, 2018

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();
               }



RE: Reading text file content into a JTextArea help - m0noxide - Mar 28, 2018

Code
if ("Savee".equals(ae.getActionCommand())) {

Could it be because you misspelled save?


RE: Reading text file content into a JTextArea help - friiz1337 - Mar 28, 2018

(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 :)


RE: Reading text file content into a JTextArea help - ✔ Soufian ✔ - Mar 28, 2018

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);
    }



RE: Reading text file content into a JTextArea help - friiz1337 - Mar 28, 2018

(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 :/


RE: Reading text file content into a JTextArea help - friiz1337 - Mar 30, 2018

Still struggling to figure out how to do it :/


RE: Reading text file content into a JTextArea help - RootFolder - Mar 31, 2018

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)


RE: Reading text file content into a JTextArea help - Conejo - Apr 3, 2018

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();

}

}

}

}