Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How to change File Type of a File Using java

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:

    // 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
    }

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?
    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());
     
    		}
     
     
    	}
    }
    Attached Files Attached Files
    Last edited by helloworld922; March 30th, 2010 at 11:04 AM.

  6. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  7. #7
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

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

  9. #9
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: How to change File Type of a File Using java

    Thats what i though but just had to check

Similar Threads

  1. Writing in a file using Java
    By JavaPF in forum File Input/Output Tutorials
    Replies: 4
    Last Post: December 17th, 2011, 04:33 PM
  2. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  3. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM
  4. File handling in java
    By srikanth in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: February 22nd, 2010, 02:11 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM