Common file manipulation methods for Java NIO __java

Source: Internet
Author: User
Automatic resource management: Automatically turn off resources that implement the Autocloseable interface
public void Test8 () {
Try (FileChannel Inchannel = Filechannel.open (Paths.get ("1.jpg"), Standardopenoption.read);
FileChannel Outchannel = Filechannel.open (Paths.get ("2.jpg"), Standardopenoption.write, standardopenoption.create)) {

Bytebuffer buf = bytebuffer.allocate (1024);
Inchannel.read (BUF);

}catch (IOException e) {

}
}

/*
Files Common method: for manipulating content
Seekablebytechannel Newbytechannel (path Path, openoption...how): Gets a connection to the specified file, how to specify how to open it.
Directorystream newdirectorystream (path Path): Opens the directory specified by path
InputStream newinputstream (path Path, openoption...how): Getting InputStream objects
OutputStream newoutputstream (path Path, openoption...how): Getting OutputStream objects
*/
public void Test7 () throws ioexception{
Seekablebytechannel Newbytechannel = Files.newbytechannel (Paths.get ("1.jpg"), Standardopenoption.read);

directorystream<path> Newdirectorystream = Files.newdirectorystream (Paths.get ("e:/"));

for (Path Path:newdirectorystream) {
SYSTEM.OUT.PRINTLN (path);
}
}

/*
Files Common methods: used to judge
Boolean exists (path path, linkoption ... opts): Determine if a file exists
Boolean isdirectory (path path, linkoption ... opts): Determine if it is a directory
Boolean isexecutable (path Path): Determining whether an executable file
Boolean Ishidden (path Path): Determining if a file is hidden
Boolean isreadable (path Path): Determining whether a file is readable
Boolean iswritable (path Path): Determining whether a file is writable
Boolean notexists (path path, linkoption ... opts): Determine if the file does not exist
public static <a extends basicfileattributes> A readattributes (Path path,class<a> type,linkoption ...) Options): Gets the properties associated with the file specified by path.
*/
public void Test6 () throws ioexception{
Path PATH = Paths.get ("E:/nio/hello7.txt");
System.out.println (files.exists (path, linkoption.nofollow_links));

Basicfileattributes readattributes = files.readattributes (path, Basicfileattributes.class, LinkOption.NOFOLLOW_ LINKS);
System.out.println (Readattributes.creationtime ());
System.out.println (Readattributes.lastmodifiedtime ());

Dosfileattributeview Fileattributeview = Files.getfileattributeview (path, Dosfileattributeview.class, Linkoption.nofollow_links);

Fileattributeview.sethidden (FALSE);
}

/*
Files Common methods:
Path copy (path src, path dest, copyoption ... how): Copying of files
Path CreateDirectory (path path, fileattribute<?> ... attr): Create a directory
Path CreateFile (path path, fileattribute<?> ... arr): Create a file
void Delete (path path): Delete a file
Path Move (path src, path dest, copyoption...how): moving src to dest location
Long size (path Path): Returns the size of the file specified by path
*/
public void Test5 () throws ioexception{
Path path1 = Paths.get ("E:/nio/hello2.txt");
Path path2 = Paths.get ("E:/nio/hello7.txt");

System.out.println (Files.size (path2));

Files.move (path1, path2, Standardcopyoption.atomic_move);
}

public void Test4 () throws ioexception{
Path dir = paths.get ("E:/nio/nio2");
Files.createdirectory (dir);

Path file = Paths.get ("E:/nio/nio2/hello3.txt");
Files.createfile (file);

Files.deleteifexists (file);
}

public void Test3 () throws ioexception{
Path path1 = Paths.get ("E:/nio/hello.txt");
Path path2 = Paths.get ("E:/nio/hello2.txt");

Files.copy (path1, path2, standardcopyoption.replace_existing);
}

/*
The Get () method provided by Paths is used to obtain the Path object:
Path Get (String A, string ... more): Used to concatenate multiple strings into paths.
Path Common methods:
Boolean EndsWith (String path): Determines whether to end with path path
Boolean startswith (String path): Determine whether to start with a path
Boolean Isabsolute (): Determining whether an absolute path
Path GetFileName (): Returns the file name associated with the calling Path object
Path GetName (int idx): The pathname of the specified index position IDX returned
int GetNameCount (): Returns the number of elements behind the path root
Path GetParent (): Returns a Path object that contains the entire route and does not contain the file path specified by the Path object
Path Getroot (): Returns the root path of the calling path object
Path Resolve (path p): Resolves a relative path to an absolute path
Path Toabsolutepath (): Returns the calling path object as an absolute path
String toString (): Returns a string representation of the calling Path object
*/
public void Test2 () {
Path PATH = Paths.get ("E:/nio/hello.txt");

System.out.println (Path.getparent ());
System.out.println (Path.getroot ());

Path NewPath = path.resolve ("E:/hello.txt");
System.out.println (NewPath);

Path path2 = Paths.get ("1.jpg");
Path NewPath = Path2.toabsolutepath ();
System.out.println (NewPath);

System.out.println (Path.tostring ());
}
public void Test1 () {
Path PATH = Paths.get ("e:/", "nio/hello.txt");

System.out.println (Path.endswith ("Hello.txt"));
System.out.println (Path.startswith ("e:/"));

System.out.println (Path.isabsolute ());
System.out.println (Path.getfilename ());

for (int i = 0; i < Path.getnamecount (); i++) {
System.out.println (Path.getname (i));
}
}

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.