Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 8th, 2007, 2:13 PM   #1
Riddick
Newbie
 
Join Date: Sep 2006
Posts: 14
Rep Power: 0 Riddick is on a distinguished road
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?
Riddick is offline   Reply With Quote
Old Apr 8th, 2007, 5:36 PM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Try the renameTo method of the File class.
titaniumdecoy is offline   Reply With Quote
Old Apr 9th, 2007, 4:15 AM   #3
Riddick
Newbie
 
Join Date: Sep 2006
Posts: 14
Rep Power: 0 Riddick is on a distinguished road
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 is offline   Reply With Quote
Old Apr 11th, 2007, 10:22 AM   #4
Riddick
Newbie
 
Join Date: Sep 2006
Posts: 14
Rep Power: 0 Riddick is on a distinguished road
I've solved it now.

I just appended the parameter in the FileWriter

FileWriter fileWriter = new FileWriter(fileName + ext);
Riddick is offline   Reply With Quote
Old Apr 13th, 2007, 7:55 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
But what if filename is "something.txt"?

if (!filename.substr(filename.length() - 4).equals(ext))
{
    filename += ext;
}
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 14th, 2007, 12:31 PM   #6
Riddick
Newbie
 
Join Date: Sep 2006
Posts: 14
Rep Power: 0 Riddick is on a distinguished road
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.
Riddick is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:14 AM.

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