How to change File Type of a File Using java
Hi,
I really appreciate your help in advance.
This is what i am trying to achieve.
I have a file say "test.xls" in some location say "X"
I wanted to open this file and read data of it, for which i already have a program that actually does it.
But the problem is, it does not recognize the file type, because even though the file name says test.xls the type of the file is a webpage(*.htm, *.html).
Now the workaround is to manually open the file and change the filetype to Microsoft Office Excel Workbook(.xls)
Once i do this, i am able to do anything with the file using Java.
I wanted to know if it is anyway possible to change the type of the file using Java, without having to include the manual steps. I am Automating this process. Please help, i can provide you with more information if my objective is not clear.
Excel Version: 2003
Thanks,
-Akash
Re: How to change File Type of a File Using java
Did you just want to rename the file? If so, the File class has a method for renaming a file:
Code :
// File (or directory) with old name
File file = new File("oldname");
// File (or directory) with new name
File file2 = new File("newname");
// Rename file (or directory)
boolean success = file.renameTo(file2);
if (!success)
{
// File was not successfully renamed
}
Re: How to change File Type of a File Using java
Hi,
Thank you first of all for taking out some time to help me.
As i said earlier the issue is not about the file extension. The extension of the file is already .xls, so that is ok. But the type of the file, the way you find out the type is open excel and do File -->Save as
You will see two dropdown boxes, one is the file name and the other is the type. I want to change the type using the Java code not the name.
Please let me know if you think it is possible to change the type?
Thanks again,
-Akash
Re: How to change File Type of a File Using java
Hello akash169.
Please only post in the correct forum. There is no point posting 3 of the same threads. We will answer you as quickly as possible regardless. It just creates more work for us having to remove the extra threads.
Isn't changing the extension name, technically changing the type?
Please post your code so we can try it..
1 Attachment(s)
Re: How to change File Type of a File Using java
Hi,
I am sorry for posting the same in three places. I have attached the actual excel document i use with this code. Also to run this code without compilation errors, you will need the POI Api Jar file. I tried to attach the POI zip with this post, but the size limit restricts me in doing so. So the .xls file has the type webpage(*.htm, *.html). If you run this code with this file you will get an error, but if you open the file manually, change the type to Excel workbook using the file Save-As Dialog and re-run this program it displays the cell content correctly.
So is there a way in java, i can open the file change the file type to Excel Workbook(*.xls) and then run my program?
Code :
public class Excel_Read extends Excel_ReadHelper
{
public void testMain(Object[] args)
{
try{
// This is where my file resides, please change this based on your local directory pat
InputStream ins = new FileInputStream("\\\\QA\\Projects\FileName.xls") ;
POIFSFileSystem fs = new POIFSFileSystem(ins);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0); // first sheet
HSSFRow row = sheet.getRow(0); // eighth row
HSSFCell cell = row.getCell((short)0); // first cell
String cellcontent = cell.toString();
System.out.println(cellcontent); // Display The Value of the First Cell
}
catch(FileNotFoundException e)
{
System.out.println(e.getMessage());
}
catch(Exception r)
{
System.out.println(r.getMessage());
}
}
}
Re: How to change File Type of a File Using java
I thought the same too, that changing the extension will change the type, but it does not do it.
If you keep the file name as say "Akash.xls"
The File type should say Excel Work Book(*.xls), but it says WebPage(*.htm, *.html)
No Clue Why ?
Re: How to change File Type of a File Using java
Are you sure you are changing the extension of the file type not just the name, for example if you don't have extensions for known file types set to visible then you would see this, "Akash.xls" but what it actually is would be like this, "Akash.xls.htm" or .html you get the idea. So infact you are not changing the file extension just the name.
Regards,
Chris
Re: How to change File Type of a File Using java
Opening via the Java File class always includes the extension. Only windows explorer will try to hide the extension (if the setting is on).
The only way to change the type of a file is to open that file up with the original type, then save out to a file that has the new type you want.
Re: How to change File Type of a File Using java
Thats what i though but just had to check :P