0

The code works as it has to until user inputs a filename with extension (.txt) and it already exists. So if the file "test.txt" exists and the user decides to name the new file as "test", it will be named as "test(1).txt", but if the user adds extension like "test.txt", the file will be named as "test.txt" and the next file user names "test.txt" will be saved as "test.txt(1).txt". Is it possible to get the name of file from JFileChooser, remove it's extension if user input it and use it as name of the new file after adding number in the middle of original file name and it's extension? I can get name without extension as String type, but I need it as File type.

         File ft = fc.getSelectedFile();
                String ext = ".txt";
                File tmp = new File(ft.getPath());
                if (!fc.getSelectedFile().getAbsolutePath().endsWith(ext)){ 
                    ft = new File (ft + ext);
                }       
                File test =  new File(ft.getPath());
                File temp = new File(ft.getPath());
                File temp1 = new File(ft.getPath());
                int count = 1;
                while (temp.exists()) {
                    if(tmp.getAbsolutePath().endsWith(ext)){

                    }
                    File ft1 = new File (tmp + "(" + count + ")");
                    ft = new File (tmp + "(" + count + ")" + ext);
                    count++;
                    temp = new File(ft.getPath());
                    temp1 = new File(ft1.getPath());
                }
                if (!temp1.getAbsolutePath().endsWith(ext)){ 
                    ft = new File (temp1 + ext);
                }
                int cnt = count - 1;
                if (!test.equals(temp)){
                JOptionPane.showMessageDialog(null, "File already exists. So it's saved with (" + cnt + ") at the end.");               
                }   
Sta
  • 13
  • 1
  • 5
  • You may find some help here (read different answers): http://stackoverflow.com/questions/3571223/how-do-i-get-the-file-extension-of-a-file-in-java – Jean-Baptiste Yunès Mar 21 '16 at 11:02
  • All of them uses String type variables and it's not working for me or at least I don't know how to use them in my case. – Sta Mar 21 '16 at 11:07

2 Answers2

0

Hmm, I think this entry might be useful as well: Remove filename extension in Java

I currently lack the time to properly test it (or better test it at all) but shouldn't it work this way:

public static String removeExtention(File f) {

    String name = f.getName();

    // Now we know it's a file - don't need to do any special hidden
    // checking or contains() checking because of:
    final int lastPeriodPos = name.lastIndexOf('.');
    if (lastPeriodPos <= 0)
    {
        // No period after first character - return name as it was passed in
        return f;
    }
    else
    {
        // Remove the last period and everything after it
        File renamed = new File(f.getParent(), name.substring(0, lastPeriodPos));
        return renamed;
    }
}

I briefly tried to adjust the code from the posting mentioned above and it may very well contain some errors or flaws. (If you find some, do not hesitate to comment on them. Some of them might be due to my current lack of time, but I am always willing to learn and improve.) However I hope this may help you to find a proper solution to your problem.

Community
  • 1
  • 1
Marco N.
  • 185
  • 2
  • 8
  • I'm informed about this, but how can I use it to change variable ft, which is File type? – Sta Mar 21 '16 at 12:09
  • I am not sure if I get your usecase 100% correctly. But as far as I understand you have a `File` variable obtained from your `JFileChooser`. Your problem is, that you want to add an (1) or any other extension in case of duplicates. And as far as I can see that is exactly what the method above does. You insert a `File` variable. The first `if` call should detect whether there is an extension. If not just insert your code to add a (1) and return the new `File`. If an extension like txt exists, use the `else` case to append a (1) or (2) and return the renamed file. Otherwise clarification please. – Marco N. Mar 21 '16 at 12:14
0

OK so I've tried to make this work without changing your code too much. Try this:

String filePath = fc.getSelectedFile().getAbsolutePath();
final String ext = ".txt";
String filePathWithoutExt;

if (filePath.endsWith(ext)) {
    filePathWithoutExt = filePath.substring(0, filePath.length() - ext.length());
} else {
    filePathWithoutExt = filePath;
}

File test = new File(filePathWithoutExt + ext);
File temp = new File(filePathWithoutExt + ext);

int count = 0;

while (temp.exists()) {
    count++;
    temp = new File(filePathWithoutExt + "(" + count + ")" + ext);
}

if (!test.equals(temp)) {
    JOptionPane.showMessageDialog(null,
        "File already exists. So it's saved with (" + count + ") at the end.");
}

EDIT:

By the recommendation of Marco N. it could be better to determine whether or not an extension exists by finding the last position of the . since this would also work with extensions other than ".txt". This value would then be used to split the string. The replacement code would look like this:

final int lastPeriodPos = filePath.lastIndexOf(".");

if (lastPeriodPos >= 0) {
    filePathWithoutExt = filePath.substring(0, lastPeriodPos);
} else {
    filePathWithoutExt = filePath;

However this would also have some issues if the user entered a file name that contained the . anywhere other than just before the file extension.

Redtama
  • 1,603
  • 1
  • 18
  • 35