Java IO Learning File class

Source: Internet
Author: User

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

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.