天天看點

操作分布式檔案之五:如何讀寫遠端檔案

FttpAdapter是通過FttpReadAdapter來直接讀取遠端檔案内容

FttpAdapter fa = new FttpAdapter("fttp://10.232.20.151/home/log/1.log");

FttpReadAdapter reader = fa.getFttpReader();

byte[] bts = reader.readAll();

上面是讀取整個檔案的内容,如果檔案内容很大,每次隻讀取一部分内容,需要指定FttpReadAdapter的讀取範圍:

FttpReadAdapter reader = fa.getFttpReader(5,10);

byte[] bts = reader.readAll();

上面表示從第5個位元組,往後讀10個位元組

fa.getFttpReader(5,FileAdapter.m(8)) 從第5個位元組往後讀8M

fa.getFttpReader(5,FileAdapter.k(512)) 從第5個位元組往後讀512K

FttpAdapter是通過FttpWriteAdapter來直接寫入遠端檔案内容

FttpAdapter fa = new FttpAdapter("fttp://10.232.20.151/home/log/1.log");

FttpWriteAdapter writer = fa.getFttpWriter();

int r = writer.write("hello world".getBytes());

上面的FttpWriteAdapter沒有指定寫入範圍,預設為追加在檔案末尾,如果需要指定範圍:

FttpWriteAdapter writer = fa.getFttpWriter(5,10);

int r = writer.write("hello world".getBytes());

上面表示從第5個位元組開始,往後寫10個位元組,寫入内容為“hello world”,如果寫入内容超出10則截斷,不夠則填補空位。

除readAll和write外,也提供readAllSafety和writeSafety方法,它們用法一樣,但是代表排它讀寫,主要用于并發讀寫。

對于數字存儲,FttpAdapter也提供整形讀寫,可以使用getIntFttpReader和getIntFttpWriter,操作跟位元組讀寫類似,隻是寫入或者傳回的是整數,比如:

fa.getIntFttpReader(5,3) 表示從第5個整數開始,往後讀3個整數

fa.getIntFttpWriter().writeInt(new int[]{1,2,3}) 表示将一個整數數組寫入檔案末尾

同樣,整形讀寫也都提供排它讀寫

FttpWriteReadDemo示範了對遠端檔案的讀寫操作

郵箱:[email protected]

企鵝群:241116021

demo源碼指南及開發包下載下傳位址:

http://www.skycn.com/soft/68321.html

繼續閱讀