4.Java NIOでよく使われる操作方法

5283 ワード

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.DosFileAttributeView;

import org.junit.Test;

public class TestNIO_2 {
	
	
	//      :       AutoCloseable      
	@Test
	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    :      
			SeekableByteChannel newByteChannel(Path path, OpenOption…how) :           ,how       。
			DirectoryStream newDirectoryStream(Path path) :    path      
			InputStream newInputStream(Path path, OpenOption…how):   InputStream   
			OutputStream newOutputStream(Path path, OpenOption…how) :    OutputStream   
	 */
	@Test
	public void test7() throws IOException{
		SeekableByteChannel newByteChannel = Files.newByteChannel(Paths.get("1.jpg"), StandardOpenOption.READ);
		
		DirectoryStream newDirectoryStream = Files.newDirectoryStream(Paths.get("e:/"));
		
		for (Path path : newDirectoryStream) {
			System.out.println(path);
		}
	}
	
	/*
		Files    :    
			boolean exists(Path path, LinkOption … opts) :         
			boolean isDirectory(Path path, LinkOption … opts) :        
			boolean isExecutable(Path path) :           
			boolean isHidden(Path path) :          
			boolean isReadable(Path path) :         
			boolean isWritable(Path path) :         
			boolean notExists(Path path, LinkOption … opts) :          
			public static A readAttributes(Path path,Classtype,LinkOption...options):pathで  したファイルに   けられたプロパティを  します。
*/
@Test
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の   な  :
Path copy(Path src,Path dest,CopyOption...how):ファイルのコピー
Path createDirectory(Path path,FileAttribute>...attr):ディレクトリの  
Path createFile(Path path,FileAttribute>...arr):ファイルの  
void delete(Path path):ファイルを  
Path move(Path src,Path dest,CopyOption...how):srcをdest  に  
long size(Path path):path  ファイルのサイズを します。
*/
@Test
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);
}
@Test
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);
}
@Test
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);
}
/*
Pathsが  するget()メソッドは、Pathオブジェクトを  するために  されます。
Path get(String first,String...more):  の  をパスに  に  するために  されます。
Pathの   な  :
boolean endsWith(String path):pathパスで  するかどうかを  する
boolean startsWith(String path):pathパスで  するかどうかを  する
boolean isAbsolute():  パスかどうかを  
Path getFileName(): び したPathオブジェクトに   けられたファイル を します。
Path getName(int idx):  したインデックス  idxのパス を します。
int getNameCount():Pathルートの ろにある  の を します。
Path getParent():Pathオブジェクトが  したファイルパスを まないパス  を むことを す
Path getRoot():Pathオブジェクトを び すルートパスを します。
Path resolve(Path p):  パスを  パスに  
Path toAbsolutePath(): び しPathオブジェクトを  パスとして します。
String toString():Pathオブジェクトを び す     を します。
*/
@Test
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());
}
@Test
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));
}
}
}