wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes About Menu Items Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "About Menu Items" Watch "About Menu Items" New topic
Author

About Menu Items

eLL Pascual
Greenhorn

Joined: May 22, 2008
Posts: 21
I created my own notepad...
How can I open a text file and append all the texts in my text area?
How can I save what I've written in my text area?
Please help me. Thanks!
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Hello Cedric ,

I think to get some replies you should post your question at Swing / AWT / SWT / JFace forum.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

I'd suggest the I/O forum instead, since the reading of the file is harder than adding it to a JTextArea.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
"eLL Pascual"?? Is that a real name?

And this thread has nothing to do with menu items. Please make sure your subject line bears some resemblance to the contents of your thread.

With the development of the Formatter class in Java 5 it became very easy to write to a text file. You just pass the File to the constructor of the Formatter object and tell it to format. Similarly reading a File is really easy with the Scanner class.

If Formatter and Scanner are all you need, then I think I shall keep the thread here, otherwise I can move it to IO later.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
yeah campee! It's his real name. Initially he used 'Cedric Diggory' (Harry Potter) as a Name. So i was just kidding.Go easy.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

I already had contact with him about it, and it's his real name, albeit abbreviated. Kind of like how William can become Bill.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Thank you Rob. I stand corrected.

Ell: How are you getting on with it? Have you got it to work with Scanner or Formatter? Should I move this thread elsewhere?
eLL Pascual
Greenhorn

Joined: May 22, 2008
Posts: 21
My real name is rafael. It became eLL because my friends wanted a one syllable nick so they decided to change it to eLL (rafa[EL]).

To Campbell Ritchie:
I am already done with scanner but I have some problems with it. the other one(formatter), I dont know that yet.

To all:
I have more questions because there has been some progress in my work. So please go to my other posts. Thanks
eLL Pascual
Greenhorn

Joined: May 22, 2008
Posts: 21
I thought I can move my post myself but this is what I got.

"Only administrators or moderators may perform this action."

Campbell Ritchie, please move my post to I/O forum.
eLL Pascual
Greenhorn

Joined: May 22, 2008
Posts: 21
About the title of my post...

Actually, I created open and save menu items so I thought "About menu items" is the best title for my post. But my question is how to make them work.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Thank you, eLL. You are right; I can move this thread and you can't.

Please post more details of what you are doing with Scanner; it will give a better idea of what is going wrong.
Formatter is more-or-less the opposite of Scanner; you can use it to write to a text file, and also set formats with % tags while you are doing it.

Sorry for messing you around about your displayed name, and sorry even more for what follows


I-O I-O away this thread will go!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Rafael,

Please use the edit button instead of posting several posts after each other in the future.
[ June 05, 2008: Message edited by: Rob Prime ]
eLL Pascual
Greenhorn

Joined: May 22, 2008
Posts: 21
Details? Ahhmmm. Actually I decided to stop using Scanner because I saw a demo using FileInputStream and FileOutputStream working very well. I just followed how the programmer used the codes and found myself dealing with the same problem I had when I was still using Scanner. FileInputStream is reading only one line of the text area, it is ignoring the succeeding lines. While the FileOutputStream in the demo is writing texts in a text file the way I typed the texts in the textArea. But in my own program, it is writing the texts in one straight line only even though I had several lines. Look at my example.

In my textArea:

Cedric
Diggory
Harry Potter

In the text file:

[] - supposedly a square

Cedric[]Diggory[]Harry Potter

But the demo is working well. I dont know what is the problem. Actually the only difference between my codes and the programmer's code are names.

Since Campbell told me that Scanner is very easy to use, I guess I will show you my code and tell me what is wrong. The problems with my Scanner is the same with the problems I mentioned above.

code:
----------------------------------------------------------------------------

else if(source.getText().equalsIgnoreCase("open")){
System.out.print("Open Press");
int returnVal = fileChooser.showOpenDialog(notepad.this);
File file = fileChooser.getSelectedFile();

Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader(file)));

while (s.hasNext()) {
textArea.append(s.next());
}
}
catch(Exception e){}
finally {
if (s != null) {
s.close();
}
}

}

----------------------------------------------------------------------------

I dont know how to use Formatter yet but I already downloaded a Formatter API so I guess I have to study it first.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Menu Items