Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   File extensions (http://www.programmingforums.org/showthread.php?t=12961)

Riddick Apr 8th, 2007 3:13 PM

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?

titaniumdecoy Apr 8th, 2007 6:36 PM

Try the renameTo method of the File class.

Riddick Apr 9th, 2007 5:15 AM

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());
                }
        }
}


Riddick Apr 11th, 2007 11:22 AM

I've solved it now.

I just appended the parameter in the FileWriter

:

FileWriter fileWriter = new FileWriter(fileName + ext);

Ooble Apr 13th, 2007 8:55 PM

But what if filename is "something.txt"?

:

if (!filename.substr(filename.length() - 4).equals(ext))
{
    filename += ext;
}


Riddick Apr 14th, 2007 1:31 PM

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.


All times are GMT -5. The time now is 2:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC