How do I add file extensions automatically?

Say I wanted a ".bra" extension to a file. Without having to type ".bra" manually everytime I want to save, what could I use to have it automatically save with one?
[171 byte] By [Kosmosisa] at [2007-11-27 5:10:11]
# 1
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.
paulcwa at 2007-7-12 10:30:11 > top of Java-index,Java Essentials,Java Programming...
# 2
Also, you'll want to check to see if the name the user enters already ends with .bra, and then not add it. Two .bras would just be weird and confusing.
jverda at 2007-7-12 10:30:11 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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"

?

Kosmosisa at 2007-7-12 10:30:11 > top of Java-index,Java Essentials,Java Programming...
# 4

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

paulcwa at 2007-7-12 10:30:11 > top of Java-index,Java Essentials,Java Programming...
# 5

> 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...)

pbrockway2a at 2007-7-12 10:30:11 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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

Kosmosisa at 2007-7-12 10:30:11 > top of Java-index,Java Essentials,Java Programming...
# 7

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".

paulcwa at 2007-7-12 10:30:11 > top of Java-index,Java Essentials,Java Programming...
# 8

> 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?

Kosmosisa at 2007-7-12 10:30:11 > top of Java-index,Java Essentials,Java Programming...
# 9

> 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.

jverda at 2007-7-12 10:30:11 > top of Java-index,Java Essentials,Java Programming...
# 10
> What is "hand"?Obviously it is what removes the .bra.
floundera at 2007-7-12 10:30:11 > top of Java-index,Java Essentials,Java Programming...
# 11
If I were writing this program, the variable would be called "teeth".
paulcwa at 2007-7-12 10:30:11 > top of Java-index,Java Essentials,Java Programming...
# 12
Oh, programmers.I'm aware that I just need to concatenate strings, I'm just not finding a method to set the path name. File has getAbsolutePath(), but how does one set the new path name?
Kosmosisa at 2007-7-12 10:30:11 > top of Java-index,Java Essentials,Java Programming...
# 13
Create a new File object, and pass the new, concatenated String to its constructor.
paulcwa at 2007-7-12 10:30:11 > top of Java-index,Java Essentials,Java Programming...
# 14

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");

floundera at 2007-7-12 10:30:11 > top of Java-index,Java Essentials,Java Programming...
# 15

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.

Kosmosisa at 2007-7-21 21:20:45 > top of Java-index,Java Essentials,Java Programming...
# 16

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

paulcwa at 2007-7-21 21:20:45 > top of Java-index,Java Essentials,Java Programming...
# 17
I suggest you read reply #14 again.
ejpa at 2007-7-21 21:20:45 > top of Java-index,Java Essentials,Java Programming...
# 18

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.

Kosmosisa at 2007-7-21 21:20:45 > top of Java-index,Java Essentials,Java Programming...
# 19

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.

paulcwa at 2007-7-21 21:20:45 > top of Java-index,Java Essentials,Java Programming...
# 20
getName was just an example. You probably need getPath.
floundera at 2007-7-21 21:20:45 > top of Java-index,Java Essentials,Java Programming...
# 21

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.

Kosmosisa at 2007-7-21 21:20:45 > top of Java-index,Java Essentials,Java Programming...
# 22
> If rv is APPROVE_OPTION, that means that the user selected something, right?I don't think so. The API talks about the file having been set by "typing the filename into the UI", and Sun's Tutorial talks about calling exists() on the returned file.
pbrockway2a at 2007-7-21 21:20:45 > top of Java-index,Java Essentials,Java Programming...
# 23
...and, of course, once I take out the . in ("Brackets", ".bra"), everything works.Neeeevermind. Thanks for everyone's help.
Kosmosisa at 2007-7-21 21:20:45 > top of Java-index,Java Essentials,Java Programming...
# 24
Try FileNameExtensionFilter bra = new FileNameExtensionFilter("Brackets", "bra"); without the dot.
pbrockway2a at 2007-7-21 21:20:45 > top of Java-index,Java Essentials,Java Programming...