![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2006
Posts: 14
Rep Power: 0
![]() |
File extensions
Hi, i'm trying to add a file extension to a file when the user has typed in the filename without an extension (in a JFileChooser). I've managed to recognise that the file is missing an extension.
String extension = null;
String fileName = file.getName();
int i = fileName.lastIndexOf('.');
if(i > 0 && i < fileName.length() - 1)
{
extension = fileName.substring(i + 1).toLowerCase();
}And from here i can work out the required extension from the filefilters but i don't know how to rename the file before i save it. Can anyone help? |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Try the renameTo method of the File class.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Sep 2006
Posts: 14
Rep Power: 0
![]() |
Hi thanks. But my understanding of renameTo is that it renames a 'physical' file on disk. However my file object has not necessarily been stored to disk. I.e. it's the first time the filename is being used. Here is my code (some of it). The way i need it to work is, if you try to save the file with no extension then the filename is automatically appended to '.txt'. Thanks.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class FileTest
{
private static JFrame frame = new JFrame();
private static JPanel panel = new JPanel();
private static JTextPane txtPane = new JTextPane();
private static File file = null;
private static JFileChooser chooser = new JFileChooser();
private static JButton button = new JButton("Save Me");
public static void main(String[] args)
{
panel.setLayout(new BorderLayout());
frame.setContentPane(panel);
JScrollPane pane = new JScrollPane(txtPane);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
save();
}
});
frame.getContentPane().add(button, BorderLayout.SOUTH);
panel.add(pane);
frame.setSize(400, 300);
frame.setVisible(true);
}
public static void save()
{
int option = chooser.showSaveDialog(frame);
if(option != chooser.APPROVE_OPTION)
return;
file = chooser.getSelectedFile();
setExt();
FileWriter writer = null;
try
{
writer = new FileWriter(file);
txtPane.write(writer);
}
catch(IOException ex){}
finally
{
try{ writer.close(); }
catch(IOException ioe){}
}
}
public static void setExt()
{
String ext = null;
String fileName = file.getName();
int i = fileName.lastIndexOf('.');
if(i > 0 && i < fileName.length() - 1)
{
ext = fileName.substring(i + 1).toLowerCase();
System.out.println(ext);
}
else
{
fileName = fileName + ".txt";
boolean renamed = file.renameTo(new File(fileName));
if(renamed)
System.out.println("Renamed: " + file.getName());
else
System.out.println("Not Renamed: " + file.getName());
}
}
} |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Sep 2006
Posts: 14
Rep Power: 0
![]() |
I've solved it now.
I just appended the parameter in the FileWriter FileWriter fileWriter = new FileWriter(fileName + ext); |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
But what if filename is "something.txt"?
if (!filename.substr(filename.length() - 4).equals(ext))
{
filename += ext;
} |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Sep 2006
Posts: 14
Rep Power: 0
![]() |
In the actual application the setExtention method returns a null value to a string variable if the file has an extension. Then depending on that variable the file is save with or without an appended extension.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| OnlineTextEditor.Com! | Sane | Show Off Your Open Source Projects | 43 | Jun 16th, 2006 8:55 AM |
| add mutiple users to the smbpasswd file. | Pizentios | Bash / Shell Scripting | 3 | Oct 20th, 2005 12:48 PM |
| After execution - Error cannot locate /Skin File? | wchar | Visual Basic | 1 | Mar 5th, 2005 9:04 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |
| Structure char field to a disk file | ehab_aziz2001 | C++ | 0 | Feb 10th, 2005 2:42 PM |