標籤:集合 style 目的 例子 使用 靜態方法 功能 訪問記憶體 效率
2017-11-05 22:09:04
NIO:new IO就是新IO的意思,JDK4開始出現新IO,新IO和傳統的IO有相同的目的,都是用於進行輸入輸出的,但是新IO使用了不同的方式來處理輸入輸出,採用記憶體對應檔的方式,將檔案或者檔案中的一段區域對應到記憶體中,就可以相訪問記憶體一樣來訪問檔案了,這種方式的效率比舊IO要高很多,但是目前好多地方我們看到還是舊IO為主。
一個小例子:
Path:路徑
Paths:有一個靜態方法返回一個路徑
public static Path get(URI uri)
Files:提供了靜態方法供我們使用
public static long copy(Path source,OutputStream out):提供複製功能
public static Path write(Path path,Iterable<? extends CharSequence> lines,Charset cs):將集合寫入檔案
public class Demo7 { public static void main(String[] args) throws IOException { // 一條陳述式完成複製功能 Files.copy(Paths.get("E:/test.txt"), new FileOutputStream("E:/copy2.txt")); //一條陳述式完成集合的儲存工作 ArrayList<String> array = new ArrayList<>(); array.add("hello"); array.add("world"); array.add("!"); Files.write(Paths.get("E:/array.txt"), array, Charset.forName("GBK")); }}
Java IO流-NIO簡介