File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Writing Content to file not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Writing Content to file not working" Watch "Writing Content to file not working" New topic
Author

Writing Content to file not working

Mary Brennan
Greenhorn

Joined: Oct 29, 2012
Posts: 7
Hi I'm a java newbie so go easy on me A brief background on what I'm trying to do with my program. I want to take an input from a user using JOptionPane when they click on the "add a word" button. The word will then be added to a file that has been created when the program is first executed. At the moment I can get the word to write to the file however, I am having issues in only allowing 1 instance of the word to write to the file. I want a dialog box to show when the user tries to enter a word that already exists in the file. I would also like to store the words in alphabetical order in the file but again I'm not entirely sure how to go about implementing this. I've copied below the code I have done so far. Any help would be greatly appreciated

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Welcome to the Ranch
What happens when you write that text to the file from a command‑line program? Do you get one line, or do you get multiple lines?

So many people think a GUI program is a GUI with a program attached. It isn’t. It is a program hidden behind the GUI. Get the program working first, then you can put the GUI on top.
Your program will need a public interface with methods like public void writeToFile(String text, File f) in.
By the way: never use \r or \n unless you have been told you require that particular sequence. There is a BufferedWriter method which write line ends, with the correct combination for your system.
Mary Brennan
Greenhorn

Joined: Oct 29, 2012
Posts: 7
Hi Campbell Richie . Thanks for getting back to me. I've reworked my code a bit and I have each word being added to the .txt file now. I can delete words from the file too but I'm having a slight problem in that the same word can get entered multiple times. I have written code that I thought would check each line of the file and check to see if that word was equal to the word the user was trying to enter. Unfortunately it doesn't seem to do what I thought it would. I've copied the code for the add word button below if you would be able to have a look? In regards to "\r\n" what would you suggest instead?

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2287
    
  49
In regards to "\r\n" what would you suggest instead?

As Campbell has already said use the BufferedWriter to do so. It has a newLine() method which adds the appropriate newline char(s) for your system.

BTW I would advise you use HashSet rather than Vector. Vector is a legacy class that isn't often used in new code. Also, as you only want one of each word storing them in a set is a better approach. If you want them sorted into their natural order use a TreeSet instead.

I'm not sure I fully understand the logic of what you are trying to do. If the number of words is low enough to be guaranteed to fit into a collection then why not read the file into the collection on starting your program and then add/delete words from the collection to keep it in sync with your file. Checking if you already have a word is then simply a case of using the collection's contains() method. If, on the other hand, you may have too many words for a collection then you will have to read the file each time you want to add a word but then why are you storing the file words that don't equal the word you have in a Vector.

I suggest you put the code to one side for a moment and write out the steps you would take if you were doing this manually.
Mary Brennan
Greenhorn

Joined: Oct 29, 2012
Posts: 7
Thanks Tony. If I knew how to use HashSet or TreeSet I would. Unfortunately I haven't yet covered these in college and my lecturer has suggested we use vectors or ArrayLists - though they are urging us to use Vectors instead of ArrayLists.

I'll try and clarify what I am trying to do. When the user enters a word I want to add it to the text file however, if the user enters a word that is already included in the text file I want a dialog box to display informing the user that the word already exists. The word therefore will not be stored a second time. Hope that makes sense. I have to order alphabetically also but I can worry about that once I can ensure that only one instance of a word appears in the dictionary.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2287
    
  49
I've no idea why your tutor would be pushing you towards Vectors. The only advantage they have over ArrayLists is they are synchronized and hence thread safe but that is unlikely to be an issue for you (and there are simple ways of making ArrayLists thread safe anyway).

Given your restrictions I would use an ArrayList. You can do as I said earlier, adding all the words in the file to the ArrayList on startup and then check the ArrayList when you want to add a new word.
BTW you can easily sort an ArrayList of Strings into alphabetical order using the java.utils.Collections.sort method
Don Ho
Greenhorn

Joined: Mar 07, 2012
Posts: 9

I think you should think about what your code is supposed to be doing first. Referring to your actionPerformed() Method: I assume you want to read the whole file and check if the added word is already present. Have a look at line 30, what does it do and what is it supposed to do?

As for Vector vs any other kind of collection, imho you do not have to worry about this kind of stuff right now... maybe later, when you got the basic functionality working
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Agree you get the basic functionality working first. But if you can find a collections framework class which makes it easier, so much the better.
I would suggest you ask why you have been told to use Vector. I know there are some teachers who are actually many years out of date and teach in a fashion which was current > 10 years ago.
You actually have a conflict there. You are trying to write into a text file, whilst needing the functionality of a Set. Reading a large text file to see whether it contains a particular word is only slightly faster than writing that word on the keyboard. Reading a 1000000‑element HashSet to see whether it contains a particular word will take something in the order of 2 nanoseconds.
Adding is maybe even faster.
If at all possible, get permission to use a SetI won’t say any more, but you can now see how you can test whether you have already used a word.

I had to break some of the long lines of code in your post, and get rid of the blank lines; both make the code difficult to read. Always use spaces for indenting, not tabs. You have gone one better: you used both.
Mary Brennan
Greenhorn

Joined: Oct 29, 2012
Posts: 7
Thanks for all the suggestions. I decided to go with a Vector and to load the words in once the application is executed and output the words in the vector once it is exited. Makes everything much easier than what I had been making it for myself.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
You’re welcome
It is surprising how thinking about what you are doing makes it so much easier, isn’t it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing Content to file not working