louzin
6 months ago
4 changed files with 391 additions and 0 deletions
@ -0,0 +1,211 @@
|
||||
# Path |
||||
## create |
||||
```java |
||||
public void createPath(){ |
||||
//absolute |
||||
//win |
||||
Path path = Paths.get("c:\\a.txt"); |
||||
//linux |
||||
Path path1 = Paths.get("/home/louzin/test"); |
||||
//relative |
||||
Paths.get("basePath","relativePath"); |
||||
Paths.get("d:\\a\\b","b\\c.txt"); |
||||
} |
||||
``` |
||||
## normalize |
||||
将路径标准化 |
||||
```java |
||||
Path path2 = Paths.get("d:\\a\\b\\..\\c.txt"); |
||||
path2.normalize(); |
||||
System.out.println("d:\\a\\c.txt"); |
||||
``` |
||||
# Files |
||||
提供了文件操作基本方法 |
||||
## Files.createDirectory(Path path) |
||||
根据path实例创建一个新目录 |
||||
- 若目录已存在,则会FileAlreadyExistsException |
||||
- 若父目录不存在,则会IOException |
||||
## Files.copy(sourcePath,destinationPath) |
||||
- 第三个参数指定覆盖类型:StandardCopyOption.REPLACE_EXISTING |
||||
## Files.move(sourcePath,destinationPath) |
||||
- 第三个参数指定覆盖类型 |
||||
## Files.delete(path) |
||||
删除文件 |
||||
## Files.walkFileTree() |
||||
遍历目录树功能,将Path实例和FileVisitor作为参数 |
||||
|
||||
FileVisitor是一个接口,必须实现FileVisitor |
||||
|
||||
FileVisitor接口方法每个都返回FileVisitResult枚举实例 |
||||
- CONTINUE:继续 |
||||
- TERMINATE:终止 |
||||
- SKIP_SIBLING:跳过同级 |
||||
- SKIP_SUBTREE:跳过子级 |
||||
|
||||
```java |
||||
public void findFileByName() throws IOException { |
||||
Path path = Paths.get("d:\\Warcraft III\\"); |
||||
String fileToFind = File.separator+"注意事项.txt"; |
||||
Files.walkFileTree(path,new SimpleFileVisitor<Path>(){ |
||||
@Override |
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes){ |
||||
String fileString = file.toAbsolutePath().toString(); |
||||
if(fileString.endsWith(fileToFind)){ |
||||
System.out.println("find:"+file.toAbsolutePath()); |
||||
return FileVisitResult.TERMINATE; |
||||
} |
||||
return FileVisitResult.CONTINUE; |
||||
} |
||||
}); |
||||
} |
||||
``` |
||||
# AsynchronousFileChannel |
||||
异步将数据写入文件 |
||||
## Create AsynchronousFileChannel |
||||
```java |
||||
public void createAsynchronousFileChannel() throws IOException { |
||||
Path path = Paths.get("d:\\a.txt"); |
||||
AsynchronousFileChannel fileChannel = |
||||
AsynchronousFileChannel.open(path, StandardOpenOption.READ); |
||||
} |
||||
``` |
||||
## Future read data |
||||
```java |
||||
public void futureTest(){ |
||||
Path path = Paths.get("d:\\a.txt"); |
||||
AsynchronousFileChannel fileChannel = null; |
||||
try{ |
||||
fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); |
||||
}catch (Exception e){ |
||||
} |
||||
ByteBuffer allocate = ByteBuffer.allocate(1024); |
||||
Future<Integer> read = fileChannel.read(allocate, 0); |
||||
while(!read.isDone()){ |
||||
allocate.flip(); |
||||
while(allocate.hasRemaining()){ |
||||
System.out.println(allocate.get()); |
||||
} |
||||
allocate.clear(); |
||||
} |
||||
} |
||||
``` |
||||
## CompletionHandle |
||||
```java |
||||
public void completHandletest(){ |
||||
Path path = Paths.get("d:\\a.txt"); |
||||
AsynchronousFileChannel fileChannel = null; |
||||
try{ |
||||
fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); |
||||
}catch (Exception e){ |
||||
} |
||||
ByteBuffer allocate = ByteBuffer.allocate(1024); |
||||
fileChannel.read(allocate, 0, allocate, new CompletionHandler<Integer, ByteBuffer>() { |
||||
@Override |
||||
public void completed(Integer result, ByteBuffer attachment) { |
||||
System.out.println("result"+result); |
||||
attachment.flip(); |
||||
while(attachment.hasRemaining()){ |
||||
System.out.println(attachment.get()); |
||||
} |
||||
attachment.clear(); |
||||
} |
||||
@Override |
||||
public void failed(Throwable exc, ByteBuffer attachment) { |
||||
|
||||
} |
||||
}); |
||||
} |
||||
``` |
||||
## Future write |
||||
```java |
||||
public void futureWrite(){ |
||||
Path path = Paths.get("d:\\a.txt"); |
||||
AsynchronousFileChannel fileChannel = null; |
||||
try{ |
||||
fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE); |
||||
}catch (Exception e){ |
||||
} |
||||
ByteBuffer allocate = ByteBuffer.allocate(1024); |
||||
allocate.put("louzin".getBytes(StandardCharsets.UTF_8)); |
||||
allocate.flip(); |
||||
Future<Integer> write = fileChannel.write(allocate, 0); |
||||
while (!write.isDone()){ |
||||
} |
||||
allocate.clear(); |
||||
System.out.println("write over"); |
||||
} |
||||
``` |
||||
## CompletionHandle Write |
||||
```java |
||||
public void completionHandleWrite(){ |
||||
Path path = Paths.get("d:\\a.txt"); |
||||
AsynchronousFileChannel fileChannel = null; |
||||
try{ |
||||
fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE); |
||||
}catch (Exception e){ |
||||
} |
||||
ByteBuffer allocate = ByteBuffer.allocate(1024); |
||||
allocate.put("louzin".getBytes(StandardCharsets.UTF_8)); |
||||
allocate.flip(); |
||||
fileChannel.write(allocate, 0, allocate, new CompletionHandler<Integer, ByteBuffer>() { |
||||
@Override |
||||
public void completed(Integer result, ByteBuffer attachment) { |
||||
System.out.println(result); |
||||
} |
||||
@Override |
||||
public void failed(Throwable exc, ByteBuffer attachment) { |
||||
} |
||||
}); |
||||
} |
||||
``` |
||||
# CharSet |
||||
字符集编码对象 |
||||
|
||||
```java |
||||
import java.nio.charset.Charset; |
||||
import java.util.SortedMap; |
||||
|
||||
public static Charset forName(String charsetName) { |
||||
//通过编码类型获得CharSet对象 |
||||
} |
||||
|
||||
public static SortedMap<String,Charset> availableCharset(){ |
||||
//获得系统支持的所有编码格式 |
||||
} |
||||
|
||||
public static Charset defaultCharset(){ |
||||
//获取虚拟机的默认编码方式 |
||||
} |
||||
|
||||
public static boolean isSupported(String charsetName){ |
||||
//判断支持该编码类型 |
||||
} |
||||
|
||||
public static String name(){ |
||||
//获取CharSet对象的编码类型 |
||||
} |
||||
``` |
||||
```java |
||||
public void charSetTest() throws CharacterCodingException { |
||||
//get charset object |
||||
Charset charset = Charset.forName("UTF-8"); |
||||
//get encoder |
||||
CharsetEncoder charsetEncoder = charset.newEncoder(); |
||||
CharBuffer allocate = CharBuffer.allocate(1024); |
||||
allocate.put("这是UTF-8测试"); |
||||
allocate.flip(); |
||||
//encoder |
||||
ByteBuffer encode = charsetEncoder.encode(allocate); |
||||
System.out.println("编码后"); |
||||
while (encode.hasRemaining()){ |
||||
System.out.println(encode.get()); |
||||
} |
||||
encode.flip(); |
||||
//decoder |
||||
CharsetDecoder charsetDecoder = charset.newDecoder(); |
||||
CharBuffer decode = charsetDecoder.decode(encode); |
||||
while (decode.hasRemaining()){ |
||||
System.out.println(decode.get()); |
||||
} |
||||
} |
||||
``` |
@ -0,0 +1,103 @@
|
||||
package com.louzin.niodemo; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.io.IOException; |
||||
import java.nio.ByteBuffer; |
||||
import java.nio.channels.AsynchronousFileChannel; |
||||
import java.nio.channels.CompletionHandler; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.nio.file.Path; |
||||
import java.nio.file.Paths; |
||||
import java.nio.file.StandardOpenOption; |
||||
import java.util.concurrent.Future; |
||||
|
||||
public class asynchronoustest { |
||||
@Test |
||||
public void createAsynchronousFileChannel() throws IOException { |
||||
Path path = Paths.get("d:\\a.txt"); |
||||
AsynchronousFileChannel fileChannel = |
||||
AsynchronousFileChannel.open(path, StandardOpenOption.READ); |
||||
} |
||||
@Test |
||||
public void futureTest(){ |
||||
Path path = Paths.get("d:\\a.txt"); |
||||
AsynchronousFileChannel fileChannel = null; |
||||
try{ |
||||
fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); |
||||
}catch (Exception e){ |
||||
} |
||||
ByteBuffer allocate = ByteBuffer.allocate(1024); |
||||
Future<Integer> read = fileChannel.read(allocate, 0); |
||||
while(!read.isDone()){ |
||||
allocate.flip(); |
||||
while(allocate.hasRemaining()){ |
||||
System.out.println(allocate.get()); |
||||
} |
||||
allocate.clear(); |
||||
} |
||||
} |
||||
@Test |
||||
public void completHandletest(){ |
||||
Path path = Paths.get("d:\\a.txt"); |
||||
AsynchronousFileChannel fileChannel = null; |
||||
try{ |
||||
fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); |
||||
}catch (Exception e){ |
||||
} |
||||
ByteBuffer allocate = ByteBuffer.allocate(1024); |
||||
fileChannel.read(allocate, 0, allocate, new CompletionHandler<Integer, ByteBuffer>() { |
||||
@Override |
||||
public void completed(Integer result, ByteBuffer attachment) { |
||||
System.out.println("result"+result); |
||||
attachment.flip(); |
||||
while(attachment.hasRemaining()){ |
||||
System.out.println(attachment.get()); |
||||
} |
||||
attachment.clear(); |
||||
} |
||||
@Override |
||||
public void failed(Throwable exc, ByteBuffer attachment) { |
||||
|
||||
} |
||||
}); |
||||
} |
||||
@Test |
||||
public void futureWrite(){ |
||||
Path path = Paths.get("d:\\a.txt"); |
||||
AsynchronousFileChannel fileChannel = null; |
||||
try{ |
||||
fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE); |
||||
}catch (Exception e){ |
||||
} |
||||
ByteBuffer allocate = ByteBuffer.allocate(1024); |
||||
allocate.put("louzin".getBytes(StandardCharsets.UTF_8)); |
||||
allocate.flip(); |
||||
Future<Integer> write = fileChannel.write(allocate, 0); |
||||
while (!write.isDone()){ |
||||
} |
||||
allocate.clear(); |
||||
System.out.println("write over"); |
||||
} |
||||
@Test |
||||
public void completionHandleWrite(){ |
||||
Path path = Paths.get("d:\\a.txt"); |
||||
AsynchronousFileChannel fileChannel = null; |
||||
try{ |
||||
fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE); |
||||
}catch (Exception e){ |
||||
} |
||||
ByteBuffer allocate = ByteBuffer.allocate(1024); |
||||
allocate.put("louzin".getBytes(StandardCharsets.UTF_8)); |
||||
allocate.flip(); |
||||
fileChannel.write(allocate, 0, allocate, new CompletionHandler<Integer, ByteBuffer>() { |
||||
@Override |
||||
public void completed(Integer result, ByteBuffer attachment) { |
||||
System.out.println(result); |
||||
} |
||||
@Override |
||||
public void failed(Throwable exc, ByteBuffer attachment) { |
||||
} |
||||
}); |
||||
} |
||||
} |
@ -0,0 +1,36 @@
|
||||
package com.louzin.niodemo; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.nio.ByteBuffer; |
||||
import java.nio.CharBuffer; |
||||
import java.nio.charset.CharacterCodingException; |
||||
import java.nio.charset.Charset; |
||||
import java.nio.charset.CharsetDecoder; |
||||
import java.nio.charset.CharsetEncoder; |
||||
|
||||
public class charsetDemo { |
||||
@Test |
||||
public void charSetTest() throws CharacterCodingException { |
||||
//get charset object
|
||||
Charset charset = Charset.forName("UTF-8"); |
||||
//get encoder
|
||||
CharsetEncoder charsetEncoder = charset.newEncoder(); |
||||
CharBuffer allocate = CharBuffer.allocate(1024); |
||||
allocate.put("这是UTF-8测试"); |
||||
allocate.flip(); |
||||
//encoder
|
||||
ByteBuffer encode = charsetEncoder.encode(allocate); |
||||
System.out.println("编码后"); |
||||
while (encode.hasRemaining()){ |
||||
System.out.println(encode.get()); |
||||
} |
||||
encode.flip(); |
||||
//decoder
|
||||
CharsetDecoder charsetDecoder = charset.newDecoder(); |
||||
CharBuffer decode = charsetDecoder.decode(encode); |
||||
while (decode.hasRemaining()){ |
||||
System.out.println(decode.get()); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,41 @@
|
||||
package com.louzin.niodemo.pathtest; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.nio.file.*; |
||||
import java.nio.file.attribute.BasicFileAttributes; |
||||
|
||||
public class pathcrud { |
||||
@Test |
||||
public void createPath(){ |
||||
//absolute
|
||||
//win
|
||||
Path path = Paths.get("c:\\a.txt"); |
||||
//linux
|
||||
Path path1 = Paths.get("/home/louzin/test"); |
||||
//relative
|
||||
Paths.get("basePath","relativePath"); |
||||
Paths.get("d:\\a\\b","b\\c.txt"); |
||||
Path path2 = Paths.get("d:\\a\\b\\..\\c.txt"); |
||||
path2.normalize(); |
||||
System.out.println("d:\\a\\c.txt"); |
||||
} |
||||
@Test |
||||
public void findFileByName() throws IOException { |
||||
Path path = Paths.get("d:\\Warcraft III\\"); |
||||
String fileToFind = File.separator+"注意事项.txt"; |
||||
Files.walkFileTree(path,new SimpleFileVisitor<Path>(){ |
||||
@Override |
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes){ |
||||
String fileString = file.toAbsolutePath().toString(); |
||||
if(fileString.endsWith(fileToFind)){ |
||||
System.out.println("find:"+file.toAbsolutePath()); |
||||
return FileVisitResult.TERMINATE; |
||||
} |
||||
return FileVisitResult.CONTINUE; |
||||
} |
||||
}); |
||||
} |
||||
} |
Loading…
Reference in new issue