|
-
Nov 14th, 2004, 01:31 PM
#1
Thread Starter
Frenzied Member
jfilechooser{resolved}
How would you open a (text)file and display it's contents in a jtextarea?
Last edited by System_Error; Nov 20th, 2004 at 08:47 AM.
-
Nov 14th, 2004, 02:42 PM
#2
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).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 14th, 2004, 03:33 PM
#3
Thread Starter
Frenzied Member
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?
-
Nov 14th, 2004, 04:18 PM
#4
Yes. The JFileChooser only comes in when selecting the file name of the file to load.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 15th, 2004, 06:03 AM
#5
Thread Starter
Frenzied Member
Thank you so much for your help.
-
Nov 19th, 2004, 05:36 PM
#6
Thread Starter
Frenzied Member
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();
}
}
}
}
-
Nov 19th, 2004, 06:54 PM
#7
You need to call readLine in a loop.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 20th, 2004, 08:32 AM
#8
Thread Starter
Frenzied Member
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.
-
Nov 20th, 2004, 08:46 AM
#9
Here's how to get all of a file into a string:
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();
A few notes:
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 20th, 2004, 08:47 AM
#10
Thread Starter
Frenzied Member
Thank you so much for taking your time to help me.
-
Nov 26th, 2004, 07:44 AM
#11
Thread Starter
Frenzied Member
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();
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|