How would you open a (text)file and display it's contents in a jtextarea?
Printable View
How would you open a (text)file and display it's contents in a jtextarea?
Open the file using a FileReader or perhaps FileInputStream + InputStreamReader (if the encoding is weird). Read it completely in. Set the text of the JTextArea using setText(String).
I should have been a little more clear with the question. I want the user to choose a file using the jfilechooser or showdialog method and then display the file in the textarea. Would I still use the FileReader in this case? That is after I get the selected file the user wants to view?
Yes. The JFileChooser only comes in when selecting the file name of the file to load.
Thank you so much for your help.
O.K. I just got everything working and now it's only reading one line of the file. I want it to read the whole thing. Could someone please see what I need to do?
Code:public void processOpen()
{
/* process Opening a new file
*/
BufferedReader br = null;
StringTokenizer st;
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
String getFile = chooser.getSelectedFile().getPath();
if(returnVal == JFileChooser.APPROVE_OPTION)
{
try {
FileReader filegetter = new FileReader(getFile);
br = new BufferedReader(filegetter);
String line = br.readLine();
st = new StringTokenizer(line);
while (st.hasMoreTokens())
{
textArea2.append(st.nextToken());
}
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (Exception myE)
{
myE.printStackTrace();
}
finally
{
try{
if (br != null)
{
br.close();
}
}
catch(IOException io)
{
io.printStackTrace();
}
}
}
}
You need to call readLine in a loop.
Could you give me a pointer on what the loops suppose to look like? I don't know really know how to go about it.
Here's how to get all of a file into a string:
A few notes:Code:Reader r = new InputStreamReader(new BufferedInputStream(
new FileInputStream("obj.xml")), Charset.forName("UTF-8"));
char[] buffer = new char[1024];
StringBuffer loaded = new StringBuffer();
int res;
do {
res = r.read(buffer);
if(res > 0) {
loaded.append(buffer, 0, res);
}
} while(res == 1024);
buffer = null;
String content = loaded.toString();
1) I chose to do the buffering before the stream->reader conversion. It shouldn't really matter either way.
2) This code loads UTF-8 files. If that's not what you want (text files in Windows are saved as Windows-1215 or something like that by default), you need to change the value in the Charset.forName call.
3) For large files, this requires a very large amount of memory. It might be wise to do the parsing chunkwise in some fashion and discard the chunk afterwards.
Thank you so much for taking your time to help me.
I found an even easier solution:
Code:JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
String getFile = chooser.getSelectedFile().getPath();
try {
textArea2.read(new FileReader(getFile),null);
}
catch(IOException e)
{
e.printStackTrace();
}