Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Files  RSS feed

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For my chess application, I'd like to be able to save/load positions, so they can be played again...
Currently, on my applications' JMenuBar, there is a JMenu with the JMenuItems 'Load' and 'Save'.
'Save' creates a small text file 'output.txt' and saves this in the same directory as the jar file of the application.
'Load' simply looks for the 'output.txt' file in the same directory as the jar file of the application, and reads in the data.

My problem is... The user is forced to save the file in the same directory as the jar file.
On a typical menu, under 'File', when you click 'Save As', you are able to browse your hard-drive for a place to save, also when you click 'open' you browse for a file to open.
I would like to use the same idea for my application, but I can't figure out how it works exactly.

Can anyone help, thanks.
 
fred Joly
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at JfileChooser
You lucky man, the job is already made.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, I've got it working, but one little problem when saving files.

Using the code below:

You can see I've set the filter to "txt" files, which works, but it doesn't save them as a text file, it just saves them as type-'File', not
type-'Text Document'. This means I have to choose a program to open the file with.

Is there anyway I can save them as text files?
Thanks
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because the file chooser does not automatically append a file extension to the name when you just enter a name without an extension. You'll have to add it yourself in your code, if the user didn't type it in.

JFileChooser tutorial
[ September 25, 2006: Message edited by: Jesper Young ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!