开发者

How to check whether a (String) location is a valid saving path in Java?

I am receiving a string from user which should be used as a location to save content to a file. This string should contain enough information, like a directory + file name.

My question is, how can I check whether the provided string is a valid path to save content to a file (at least in theory)?

It does not matter whether directories are created or not, or whether one has proper access to the location itself. I am only interested in checking the structure of the provided string.

How should I pro开发者_JAVA技巧ceed? I was thinking about creating a File object, then extracting its URI. Is there any better way?


You can use File.getCanonicalPath() to validate according the current OS rules.

import java.io.File;
import java.io.IOException;

public class FileUtils {
  public static boolean isFilenameValid(String file) {
    File f = new File(file);
    try {
       f.getCanonicalPath();
       return true;
    }
    catch (IOException e) {
       return false;
    }
  }

  public static void main(String args[]) throws Exception {
    // true
    System.out.println(FileUtils.isFilenameValid("well.txt"));
    System.out.println(FileUtils.isFilenameValid("well well.txt"));
    System.out.println(FileUtils.isFilenameValid(""));

    //false
    System.out.println(FileUtils.isFilenameValid("test.T*T"));
    System.out.println(FileUtils.isFilenameValid("test|.TXT"));
    System.out.println(FileUtils.isFilenameValid("te?st.TXT"));
    System.out.println(FileUtils.isFilenameValid("con.TXT")); // windows
    System.out.println(FileUtils.isFilenameValid("prn.TXT")); // windows
    }
  }


Have you looked at Apache Commons IO? This library includes various things for handling path information which may help e.g. FilenameUtils.getPath(String filename) which returns the path from a full filename.


Easiest: try to save, listen for exceptions.

The only time I'd do something more complicated would be if the writing was to be deferred, and you want to give the user his feedback now.


Here is what I have so far:

private static boolean checkLocation(String toCheck) {

    // If null, we necessarily miss the directory section
    if ( toCheck == null ) {
        System.out.println("Missing directory section");
        return false;
    }

    String retrName = new File(toCheck).toURI().toString();

    // Are we dealing with a directory?
    if ( retrName.charAt(retrName.length()-1) == '/') {
        System.out.println("Missing file name");
        return false;
    }

    return true;

}

This tells me whether I have a proper directory structure and whether I am pointing to a directory rather than a file. I do not need I/O access.

I have noticed that if I use the File.createNewFile() method on a location pointing explicitly to a directory (which does not exist yet), Java creates a file with no extension, which is plain wrong. Either it should create a directory or it should throw some kind of error.

Also, the File constructors tend to add the current directory if none is provided in the argument. It is not documented, but no real harm in my case.

If anyone has a better solution, I'll approve it.

EDIT

I have finally combined the above with the input from RealHowTo.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜