Read / write program in Java using JFileChooser - java

How would I link the file choosen from a JFileChooser to a file and how would I convert it to string being able to display and edit it in a TextArea?
I have the GUI set up using swing, but the link between actionListener and the JFileChooser is not complete.
Any help would be much appreciated.
Code: http://pastebin.com/p3fb17Wi
EDIT: I found this program, that does pretty much what i wanted to, but it does not allow me to save the actual file : http://www.java-forums.org/new-java/8856-how-get-content-text-file-write-jtextarea.html

To be able to save the changes you have made, you will have to use a Save Dialog. In the example you have quoted, a File Open Dialog is used. They work in a similar way, all that you need to do is then get the file to which the user would like to store the changes made, open a stream to it and write the data back. This tutorial shows you how to use the various File Choosers.

All text components support a read(...) and write(...) method. So all you have to do is get the name of the File and create your FileReader or FileWriter and then invoke the method.
All the file chooser is used for is the get the File name to be used by the reader or writer. So the basic code would be:
File saveFile = chooser.getSelectedFile();
FileWriterr writerr = new FileWriter( saveFile );
textArea.write(writer)
Of course you will probably want to use a Buffered reader/writer.

Related

Opening a file in using java

I'd like to have it so that my java executeble will open a text file up when a Jbutton is pressed.
I dont know where to start so if someone could just nudge me in right direction that would be great
when is searched for opening a file using java it was all about reading in data from a file
Opening a File Pointer:
File f = new File("/path/to/file.txt");
Opening a file input stream (For reading):
FileInputStream in = new FileInputStream("/path/to/file.txt");
Opening a file output stream (for writing):
FileOutputStream out = new FileOutputStream("/path/to/file.txt");
Please take better care to use the documentation and Google. Stack Overflow is not here to do your homework for you.
Edit:
To appease #Takendarkk, I've included this:
Random Access File (More advanced that the previous functions, it opens a file with the mannerisms which C/C++ would use.):
RandomAccessFile raf = new RandomAccessFile("/path/to/file.txt", "rw+");

how to write tags,meta data to a file with android java(when we right click on windows>properties)

I want to add a comment to a file(video,txt,etc...). right click on a file on windows the comment should appear.
File tempf= new File (Environment.getExternalStorageDirectory().getAbsolutePath()+"/picedittemp");
tempf.setcommentOrTittle("mycomment to file");
Not all files contain a comment field so there is no standard way of doing it. Each file type however will have a library which will allow you to modify its tags. An mp3 for example has the JAudioTagger library which allows you to set tags like this.
//No guarantee this will work (just an example)
File track = new File("C:/path/to/song.mp3");
AudioFile f = AudioFileIO.read(track);
f.getTag().setField(FieldKey.COMMENT,"My Comment");

Writing from console to text file, file data not saved

I am trying to save the content of the java console into a text file but each time I close the program the text file goes blank and rewrites to it. i.e. if I write to a file today, close the program and come back and run it again tomorrow, it has remembered the information written to it.
You want to open the OutputStream in append mode. Demo code:
PrintWriter out = new PrintWriter(
new FileOutputStream(new File(filename), true));
What you experience is the normal behavior when you write a stream to a file, and this is not specific to the Java API.

Save my Unique ID (String) and retrieving it later when I re-launch my Java application

I want to save a Unique ID (which is a String) which gets created when I launch my Java application. Now I want to save this somewhere (I think in some file on the disk) so that when I relaunch my application I should be able to read it and use that ID.
I want to know what is the good way to saving such ID. I am thinking of creating a Properties file and save it then retrieve it from it when I relaunch application. Is there a better or standard way for this?
EDIT :
Additionally what should be the folder location for storing on the disk. Should it be relative to my execution path or some Logged-in user specific path?
1. If its the same Java application that writes or reads this String, then use Serialization, it will be in non-readable form when saved.
2. If reading and writing is from different program, then use Text file.
3. Using Property file will be also a good approach.
If your app/program needs to store more data at some point sqlite3 might be the best option for you. It is easy to implement and use.
Download sqlite3
EDIT: How many IDs will be stored in the app? If there are just a few, a textfile or property file is enough.
EDIT2: Navigate to your Documents folder on your machine and you will see folders of programs/games. Thats where you should place the file/db. However you can also store it in the installation path on your hard drive. Also make sure your user launches the app trough a shortcut, not the actual execution file
Use the FileWriter and File classes from Java.
It should be something like that:
File f = new File(your path here);
if (f.exists()){
BufferedReader br = new BufferedReader(new FileReader(your path here));
String a = br.readLine();
br.close();
}else{
FileWriter fw = new FileWriter(your path here);
fw.write(your ID String);
fw.flush();
fw.close();
I hope this is want u meant.
Best regards
edit: just noticed too late that your edited your post....

How to "Open" and "Save" using java

I want to make an "Open" and "Save" dialog in java. An example of what I want is in the images below:
Open:
Save:
How would I go about doing this?
You want to use a JFileChooser object. It will open and be modal, and block in the thread that opened it until you choose a file.
Open:
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(modalToComponent) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
// load from file
}
Save:
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(modalToComponent) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
// save to file
}
There are more options you can set to set the file name extension filter, or the current directory. See the API for the javax.swing.JFileChooser for details. There is also a page for "How to Use File Choosers" on Oracle's site:
http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html
I would suggest looking into javax.swing.JFileChooser
Here is a site with some examples in using as both 'Open' and 'Save'. http://www.java2s.com/Code/Java/Swing-JFC/DemonstrationofFiledialogboxes.htm
This will be much less work than implementing for yourself.
Maybe you could take a look at JFileChooser, which allow you to use native dialogs in one line of code.
You can find an introduction to file dialogs in the Java Tutorials. Java2s also has some example code.
First off, you'll want to go through Oracle's tutorial to learn how to do basic I/O in Java.
After that, you will want to look at the tutorial on how to use a file chooser.
You may also want to consider the possibility of using SWT (another Java GUI library). Pros and cons of each are listed at:
Java Desktop application: SWT vs. Swing

Categories

Resources