Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to change a file extension of a file location
    primarykey
    data
    text
    <p>i have an application that a user uploads a file to,</p> <p>but what i want is to detect the file extension of the uploaded file and if a doc file do something else do nothing</p> <p>anyone have any ideas?</p> <p>is there a way to change the file extension for example is a file was D:/test.doc to change to D:/test.pdf </p> <p>i know this will not convert the file format, that will be done later</p> <p>the file location changes every time etc so i just need a way to take the location, and change the file extension</p> <p>code i have currently </p> <pre><code>File file = new File("oldname"); File file2 = new File("newname"); // Rename file (or directory) boolean success = file.renameTo(file2); if (!success) { // File was not successfully renamed } </code></pre> <p>but this does not do it :( The problem with this code is i must give the new file name, and i can not as it should be exactly the same as the old file name just with different extensions</p> <p>EDIT 2 </p> <p>hopefully this will clear it up as the last question was a little confusing</p> <p>basically, i have a program that converts an uploaded doc file to a pdf, but the location of the uploaded doc changes each time, as it creates a folder for each user </p> <p>this is defined in the variable <code>String location</code></p> <p>what i want to do is take this variable <code>location</code> as this will end with a <code>.doc</code> and i want to change the <code>location</code> from <code>.doc</code> to <code>.pdf</code></p> <p>I have managed to find out the name of the file, without hte extension by using :</p> <pre><code> String path = location; File f = new File(path); System.out.println(f.getName()); String fName = f.getName(); System.out.println(fName.substring(0, fName.lastIndexOf('.'))); </code></pre> <p>but how can i find out the path before the name of file </p> <p>EDIT for the last time i have found a solution</p> <pre><code> System.out.println(path2.substring(0, path2.lastIndexOf('.'))); newLocation = (path2.substring(0, path2.lastIndexOf('.')))+".pdf"; System.out.println(newLocation); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload