Java folder operation to determine if the multi-level path exists and does not exist (including path character analysis under Windows and Linux)

Source: Internet
Author: User

Compatible with Windows and Linux.

Analysis:

The following paths are represented under Windows:

(Standard) D:\test\1.txt

(nonstandard, refer to Linux) d:/test/1.txt

Then in Java, especially when working with the file object in the path of Windows, it is very bad to do so because you do not know how many slashes "\" to represent.

The slash "\" is the escape character (ref.: http://www.cnblogs.com/EasonJim/p/6561576.html).

While Java's file object is either Windows or Linux, the input path is not standard, the output path is the standard path of the current system, but it is very strange to print with ordinary print on the path, especially the Windows standard path, and the same as shown above But, in turn, using the printed path to the new File ("D:\test\1.txt") will prompt the inverse of the character error.

After research, if like windows, this method can only be constructed to recognize a path like this: D:\\test\\1.txt, and this path is only printed to see, if you put this into the file object to construct, also reported the escape character error; To solve the problem, that is to say, The file object will eventually recognize the path of the double slash, but it is escaped with a double slash path to be recognized and ultimately written as: D:\\\\test\\\\1.txt this is correct.

The above analysis of the path on the Windows for future operation of the path is helpful, such as cutting the path of the string, the path when the variable is stored, it contains the escape character, that is, "\\\\" four slash-based.

There is also a point to note, because the two paths listed above, in the non-standard path near the cutting point, you can first use the file object to construct, and then get the absolute path passed in, so that the output is the standard path, the variable also contains the correct escape character.

So a path problem like this will not occur under Linux, such as a Linux representation in the following form:

/users/jim/test/1.txt

As you can see, the backslash "/" used by Linux is not an escape character on the character, so the file object is output as-is on both the input and the output.

Specific operation:

Reference: http://zyjustin9.iteye.com/blog/2164934

The code is as follows:

/*** Determine if the multi-level path exists and does not exist to create * *@paramFilePath supports path with file name: D:\news\2014\12\abc.text, and path without file name: D:\news\2014\12*/     Public Static voidIsexistdir (String filePath) {string paths[]= {""}; //Cutting Path        Try{String TempPath=NewFile (FilePath). Getcanonicalpath ();//The file object is converted to a standard path and is cut, and there are two types of Windows and Linuxpaths = Temppath.split ("\\\\");//Windows            if(paths.length==1) {paths = Temppath.split ("/");}//Linux}Catch(IOException e) {System.out.println ("Cut path Error");        E.printstacktrace (); }        //determine if there is a suffix        BooleanHastype =false; if(paths.length>0) {String TempPath= Paths[paths.length-1]; if(Temppath.length () >0){                if(Temppath.indexof (".") >0) {Hastype=true; }            }        }                //Create a folderString dir = paths[0];  for(inti = 0; I < paths.length-(hastype?2:1); i++) {//note the length of the loop here, with the suffix is the file path, no folder path            Try{dir= dir + "/" + paths[i + 1];//using the standard notation of Linux to splice, because Windows can recognize such a path, so the use of we must upholdFile Dirfile =NewFile (dir); if(!dirfile.exists ())                    {Dirfile.mkdir (); System.out.println ("Directory successfully created:" +dirfile.getcanonicalfile ()); }            } Catch(Exception e) {System.err.println ("Folder creation exception occurred");            E.printstacktrace (); }        }    }    

Test Project: Https://github.com/easonjim/5_java_example/tree/master/dirtest

Java folder operation to determine if the multi-level path exists and does not exist (including path character analysis under Windows and Linux)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.