> It's just part of the name.
>
> If you have some part of your program that takes a
> filename to save to, then when you do the actual
> saving, append a ".bra". It's that simple.
>
> Now, taking off the .bra might be more difficult.
Hmm. Here's my the class that takes care of my saving:
public class saver implements ActionListener{
public void actionPerformed(ActionEvent e){
if(desk.getSelectedFrame() != null){
int rv = hand.showSaveDialog(desk);
if(rv == JFileChooser.APPROVE_OPTION){
File f = hand.getSelectedFile();
desk.getSelectedFrame().setTitle(f.getName());
try{
FileOutputStream fo = new FileOutputStream(f);
ObjectOutputStream oo = new ObjectOutputStream(fo);
JInternalFrame j = desk.getSelectedFrame();
DataPack d = new DataPack(j);
oo.writeObject(d);
oo.close();
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "For some reason, the file " +
"didn't write.");
JOptionPane.showMessageDialog(null,"(" + ex.getMessage() + ")");
}
}
}
}
}
Would I just make a new File and have it's constructor argument be f.getName() + ".bra"
?
What is "hand"?
And for that matter what is the use case here? Is the user selecting a file, and you want to create a new file with the ".bra" extension? Or are you asking the user for a name, and you want to create a filename with that name plus the extension? Is the file likely to exist prior to looking for it?
Message was edited by:
paulcw
> Would I just make a new File and have it's constructor argument be
> f.getName() + ".bra" ?
That doesn't take care of the point raised by jverd: what if the file already ends with ".bra"? The problem is that if the user repeatedly opens and saves the file it could end up with a name like "foo.bra.bra.bra".File f = hand.getSelectedFile();
// what to do here?
desk.getSelectedFrame().setTitle(f.getName());
// etc
I think I might opt for:
(1) If f exists, use it unchanged. (The assumption being that the user knows what they're doing. But note some OS's "help" the user by hiding extensions.)
(2) If f.toLowerCase() equals ".bra" accept f as is.
(3) Otherwise do what you said and make a new file with ".bra" appended to the name. (Of course the user who enters "foo.bra.old" will be out of luck...)
> What is "hand"?
>
> And for that matter what is the use case here? Is
> the user selecting a file, and you want to create a
> new file with the ".bra" extension? Or are you
> asking the user for a name, and you want to create a
> filename with that name plus the extension? Is the
> file likely to exist prior to looking for it?
>
> Message was edited by:
> paulcw
Sorry, forgot to mention "hand" is the name of my JFileChooser, desk is the name of my JDesktopPane.
In this case, the user is saving a file, so I need to make one with a .bra extension. I haven't coded a "Save As"m so it's more than likely to exist.
Message was edited by:
Kosmosis
Are you planning to rename the existing "file" to "file.bra", and then save the user's data in "file"? So "bra" stands for "backup, right away" or something?
Maybe what you really want to do is to:
1) get the new filename
2) rename the existing file to the new filename
3) save data in the existing filename
This still doesn't answer the issue re: whether file.bra already exists, or whether you should allow "file.bra.bra.bra".
> Are you planning to rename the existing "file" to
> "file.bra", and then save the user's data in "file"?
> So "bra" stands for "backup, right away" or
> something?
>
> Maybe what you really want to do is to:
> 1) get the new filename
> 2) rename the existing file to the new filename
> 3) save data in the existing filename
>
> This still doesn't answer the issue re: whether
> file.bra already exists, or whether you should allow
> "file.bra.bra.bra".
The file.bra.bra.bra thing is something I'd want to avoid. I want the data to be saved to "file", but have it saved as a .bra file, so it's path name would be "file.bra". I'm thinking using the exists() method to check. If it doesn't, I append the extension. Otherwise, I leave the name be.
My big question is, how do I append the file extension to begin with?
> My big question is, how do I append the file
> extension to begin with?
Why do you not get that? It's already been answered, and you already showed that you got that part of it.
fname += ".bra";
or
save(fname + ".bra");
or something along those lines.
You're just putting two Strings together. If you're doing file I/O, I assume you've already learned how to concatenate strings.
File class is only an abstract representation. It isn't your actual file. So just get your variable to point to a new object.
File f = new File("ze");
f = new File(f.getName() + ".bra");
I change that, but then it just writes a blank file.
public class saver implements ActionListener{
public void actionPerformed(ActionEvent e){
if(desk.getSelectedFrame() != null){
int rv = hand.showSaveDialog(desk);
if(rv == JFileChooser.APPROVE_OPTION){
File f = hand.getSelectedFile();
Here, f is set to the selected file as returned by the JFileChooser. By initializing it to a new File, I don't think it's carrying over the info assigned to it from the JFileChooser.
It's not initializing it to a new File. The variable "f", of type "File", is local to that block, but declaring f to be a File doesn't mean that you're initializing anything.
f is going to be whatever hand.getSelectedFile returns.
[add]
Wait are you referring to the act of creating a File object, as we suggested, or to the line in your example where you assign the result of getSelectedFile
Message was edited by:
paulcw
Here's my current code:
public class saver implements ActionListener{
public void actionPerformed(ActionEvent e){
if(desk.getSelectedFrame() != null){
int rv = hand.showSaveDialog(desk);
if(rv == JFileChooser.APPROVE_OPTION){
File f = hand.getSelectedFile();
if(!f.exists()){
f = new File(f.getName() + ".bra");
}
desk.getSelectedFrame().setTitle(f.getName());
try{
FileOutputStream fo = new FileOutputStream(f);
ObjectOutputStream oo = new ObjectOutputStream(fo);
JInternalFrame j = desk.getSelectedFrame();
DataPack d = new DataPack(j);
oo.writeObject(d);
oo.close();
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "For some reason, the file " +
"didn't write.");
JOptionPane.showMessageDialog(null,"(" + ex.getMessage() + ")");
}
}
}
}
}
Using this, f retains the information given by getSelectedFile(), but if f is a new file, it'll both retain the previous info and the new file path? When I try to save my file, it'll go flawlessly, except there won;t be any new file in the destination folder, in my case, my computer's desktop.
If rv is APPROVE_OPTION, that means that the user selected something, right?
And therefore, the file must exist, right? Because offhand I can't see a way to use JFileChooser to select something that doesn't exist.
And therefore, "f" will exist.
And therefore, "!f.exists()" will always be false.
And therefore, you don't execute the code that adds the .bra extension.
That's what I'd expect from this code, anyway.
getPath() does the trick! It saves perfectly! .bra and everything. Thanks to all that contributed.
...new problem, though. My FileFilter isn't picking it up.
public class loader implements ActionListener{
public void actionPerformed(ActionEvent e){
FileNameExtensionFilter bra = new FileNameExtensionFilter("Brackets", ".bra");
hand.setFileFilter(bra);
int rv = hand.showOpenDialog(XSFrame.this);
if(rv == JFileChooser.APPROVE_OPTION){
File f = hand.getSelectedFile();
try{
FileInputStream fi = new FileInputStream(f);
ObjectInputStream oi = new ObjectInputStream(fi);
DataPack d = (DataPack) oi.readObject();
oi.close();
JInternalFrame jif = d.getJIF();
jif.setContentPane(d.getContainer());
desk.add(jif);
desk.setSelectedFrame(jif);
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "For some reason, the file " +
"didn't load.");
JOptionPane.showMessageDialog(null, "(" + ex.getMessage() + ")");
}
}
}
}
}
Again, it'll show "file.bra" when the filter's not on, but it won't pick it up while it's on.