1. Take a look at the four static variables first
static String |
pathSeparatorThe system-dependent path-separator character, represented as a string for convenience. |
static char |
pathSeparatorCharThe System-dependent path-separator character. |
static String |
separatorThe system-dependent default Name-separator character, represented as a string for convenience. |
static char |
separatorCharThe system-dependent default Name-separator character. |
Because the system path representation of the Windows and UNIX kernel is inconsistent (Windows is ' \ ', the Unix kernel is '/'), Java provides a common path divider, which is automatically transformed according to the different systems, but in practice use '/', Both Windows and UNIX kernel systems are supported.
Linux under test
Public class filetest { publicstaticvoid main (string[] args) { System.out.println (file.pathseparator); System.out.println (File.pathseparatorchar); System.out.println (file.separator); System.out.println (File.separatorchar); }}
Output
:
:
/
/
How to construct a 2.File class
File(File parent, String child)Creates a new File instance from a parent abstract pathname and a child pathname string. |
File(String pathname)Creates a new File instance by converting the given pathname string to an abstract pathname. |
File(String parent, String child)Creates a new File instance from a parent pathname string and a child pathname string. |
File(URI uri)Creates a new file instance by converting the given file: URI to an abstract pathname. |
First Kind
@Test publicvoidthrows exception{ new File ("/ Joey/soft "); New File (Parent, "son.txt"); System.out.println (Son.getabsolutepath ()); System.out.println (Son.getparent ()); System.out.println (Parent.getname ()); }
Output
/joey/soft/son.txt
/joey/soft
Soft
The second Kind
@Test publicvoidthrows exception{ new File ("/ Joey/soft/son.txt "); System.out.println (Son.getabsolutepath ()); System.out.println (Son.getparent ()); }
Output
/joey/soft/son.txt
/joey/soft
Third Kind
@Test publicvoidthrows exception{ new File ("/ Joey/soft "," Son.txt "); System.out.println (Son.getabsolutepath ()); System.out.println (Son.getparent ()); }
Output:
/joey/soft/son.txt
/joey/soft
Fourth type
@Test publicvoidthrows exception{ new File ("/ Joey/soft "," Son.txt "); New File (F.touri ()); System.out.println (Son.getabsolutepath ()); System.out.println (Son.getparent ()); }
Output
/joey/soft/son.txt
/joey/soft
Of course, you can write the relative path without writing the absolute path.
@Test Public void throws exception{ new File ("test.txt"); System.out.println (Son.getabsolutepath ()); System.out.println (Son.getparent ()); }
Output
/home/joey/eclipse/workspace/iotest/test.txt
Null
Reference: Http://docs.oracle.com/javase/7/docs/api/java/io/File.html#File (Java.net.URI)
Java IO Learning File class