nio 三 檔案讀取和寫入

2021-08-13 02:12:11 字數 1331 閱讀 7895

讀取:

path p4=paths.get("e:\\版本控制1\\服務端\\2.txt" );

try (bufferedreader reader=files.newbufferedreader(p4, standardcharsets.utf_8))

}簡化版讀取:

listcontents=files.readalllines(p6);

contents.foreach(content->);

寫入:

try (bufferedwriter writer=files.newbufferedwriter(p6,standardcharsets.utf_8))
監測檔案改變

path path=paths.get("e:\\版本控制1");

watchkey key=path.register(watcher, entry_modify);

while (true)

}key.reset();

}

檔案讀取(通道,可選擇位置):

path p= paths.get("e:\\版本控制1\\服務端\\2.txt");

bytebuffer bytebuffer=bytebuffer.allocate(1024);

filechannel filechannel=filechannel.open(p,standardopenoption.read);

long index=filechannel.size()-2

; filechannel.read(bytebuffer,index);

system.out

.println(new string(bytebuffer.array(),standardcharsets.utf_8

));path p= paths.get("e:\\版本控制1\\服務端\\2.txt");

bytebuffer bytebuffer=bytebuffer.allocate(1024);

asynchronousfilechannel channel=asynchronousfilechannel.open(p,standardopenoption.read);

channel.read(bytebuffer, 0, bytebuffer, new completionhandler()

@override

public void failed(throwable exc, bytebuffer attachment)

});

Python小記 6 檔案的讀取 寫入

with open pai.txt as file object 讀取檔案內容,內部有中文字元就不行 gbk codec can t decode byte 0x82 in position 51 illegal multibyte sequence content file object.read...

Node學習筆記(六) 檔案讀取 寫入 刪除

檔案讀取 匯入檔案模組 匯入檔案模組,node自帶的模組 let fs require fs 同步讀取 node預設是非同步 同步讀取檔案,會阻塞和等等 let content fs.readfilesync fs 01.txt console.log content 01txt中的內容是01txt...

day2 檔案的建立,讀取,寫入

一 檔案的建立讀取寫法 1 f open user list.txt r encoding utf 8 開啟建立檔案,檔案控制代碼 路徑 大小 字符集等 2 f.write 資料寫入成功!寫入資料表示式 3 data f.read 讀取資料 4print data 列印資料 5 f.close 關閉...