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..
Code public static void main( String[] args ) throws Exception
{
final String testFile = "test.txt";
JFrame frame = new JFrame();
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane( textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
JButton load = new JButton( "Load text from test.txt" );
JButton save = new JButton( "Save to test.txt" );
JPanel buttonPanel = new JPanel();
buttonPanel.add( load );
buttonPanel.add( save );
frame.add( scrollPane, BorderLayout.CENTER );
frame.add( buttonPanel, BorderLayout.SOUTH );
load.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent event )
{
try
{
String text = readTextFromFile( testFile );
textArea.setText( text );
textArea.validate();
}
catch( IOException exception )
{
System.err.println( "Couldn't read from \"" + testFile + "\"" );
exception.printStackTrace();
}
}
});
save.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent event )
{
try
{
writeToFile( testFile, textArea.getText() );
}
catch( IOException exception )
{
System.err.println( "Couldn't write to \"" + testFile + "\"" );
exception.printStackTrace();
}
}
});
frame.setSize( 500, 500 );
frame.setVisible( true );
}
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();
}
}
}
}
|