得到filesystem的執行個體
有兩個靜態方法可以得到filesystem介面的執行個體
public static FileSystem get(Configuration conf) throws IOException
public static FileSystem get(URI uri, Configuration conf) throws IOException
第一個方法得到預設的檔案系統,具體由設定檔的fs.default.name
屬性決定。
第二個方法由URI的首碼決定是哪個檔案系統(參考前一篇文章),如果URI沒有首碼也是得到預設的檔案系統。
使用open方法得到檔案的輸入資料流
有了filesystem的執行個體,我們就可以調用open方法得到某個檔案的輸入資料流了
public FSDataInputStream open(Path f) throws IOException
public abstract FSDataInputStream open(Path f, int bufferSize) throws IOException
第一個方法用預設的buffer大小4K
使用FSDataInputStream讀資料
FSDataInputStream派生自java.io.DataInputStream,支援隨機讀取。
public class FSDataInputStream extends DataInputStream
implements Seekable, PositionedReadable {
void seek(long pos) throws IOException;
public int read(long position, byte[] buffer, int offset, int length) throws IOException;
}
使用FSDataOutputStream寫資料
filesystem介面建立檔案的介面
public FSDataOutputStream create(Path f) throws IOException
還可以append到一個已經存在的檔案
public FSDataOutputStream append(Path f) throws IOException
public class FSDataOutputStream extends DataOutputStream implements Syncable {
public void write(int b) throws IOException;
public void write(byte b[], int off, int len) throws IOException;
}
filesystem的檔案系統操作方法
public boolean mkdirs(Path f) throws IOException ;
public void copyToLocalFile(boolean delSrc, Path src, Path dst) throws IOException;
public abstract boolean delete(Path f, boolean recursive) throws IOException;
public abstract boolean rename(Path src, Path dst) throws IOException;
public abstract FileStatus[] listStatus(Path f) throws IOException;