JavaIO flow path of the file

Slash and backslash

Forward slash, slash, also known as the left, is the symbol "/";

Backslash, also known as forward slashes, symbols are "\".

In the Unix/Linuxseparator, the positive use of the inclined path "/", such as "cd /home/java";

In Windowsthe path separated using a backslash "\", for example "F:\yihong_\book".

I developed in Windowsthe platform, the Javaconfiguration file in the file are used in Road King "\\", and the project is deployed Linuxon, all files are used in Road King "/".

Escape character

WindowsI copied the F-address “F:\yihong_\book”and paste it into a Java program, it will automatically become “F:\\yihong_\\book”. This time the escape occurred, this operation is done automatically idea.

String path = "F:\\yihong_\\book"; correct

String path = "F:\yihong_\book"; Incorrect

In the java code development \on behalf of the escape character.

JavaThe escape character \escaped not a need to escape, anything can escape, escape it only supports it:
\b \t \n \f \r \" \' \\

Absolute and relative paths

It indicates that the file path can be divided into absolute and relative paths.

WindowsAbsolute path: drive letter, such as startF:\yihong_\book

Windowsrelative path:

. It refers to the current directory

.. It refers to the parent directory of the current directory

./bookIt represents the current directory bookfolder

/bookThe letter represents the current bookfolder

LinuxAbsolute path:  to rootthe root directory path / beginning of such / representation rootroot directory

Linuxrelative path:

./ It refers to the current directory

../ It refers to the parent directory of the current directory

.It represents the current directory, ..on behalf of the parent directory.

Use File.seperatorsplicing file path

 

String fileSeperator = File.separator;

File.separatorIt will use the correct file delimiter character depending on the operating system.

eg:

String dirName = FilenameUtils.getBaseName(parentStack.getFileName()) + File.separator + parentStack.getMethodName() + File.separator;

 

Java common system and method for obtaining path

 

// 分隔符
String fileSeperator = File.separator;

// 用户主目录
String userHome = System.getproperties().getProperty("user.home");

// Java实时运行环境的安装目录
String javaPath = System.getproperties().getProperty("java.home");

// 操作系统名称
String osName = System.getproperties().getProperty("os.name");

// 当前用户程序所在目录
String userDir = System.getproperties().getProperty("user.dir");

// JDK的安装目录
String jdkDir = System.getproperties().getProperty("java.ext.dirs");

 

Published 12 original articles · won praise 4 · Views 1690

Guess you like

Origin blog.csdn.net/Cymothoe/article/details/104620966