Java IO(字元流)複製檔案

2021-09-02 19:21:52 字數 1429 閱讀 6256

1.複製檔案(包含中文)

/** *

* @param path 目標檔案

* @param target 需要複製到的指定檔案

* @return boolean true 複製成功 ;false 複製失敗

* @throws ioexception

*/ public static boolean copy(string path, string target) throws ioexception

file targetfile=new file(path);

if(!targetfile.getparentfile().exists())

filereader fr=new filereader(file);

filewriter fw=new filewriter(target);

char buf=new char[1024];

int len;

while((len=fr.read(buf))!=-1)

fr.close();

fw.close();

long end=system.currenttimemillis();

system.out.println("總共耗時:"+(end-start)+"毫秒");

return true;

}​

2.複製二進位制檔案()

/**

* * @param path 目標檔案

* @param target 需要複製到的指定檔案

* @return boolean true 複製成功 ;false 複製失敗

* @throws ioexception

*/ private static boolean copy(string path, string target) throws ioexception

file targetfile=new file(target);

if(!targetfile.getparentfile().exists())

inputstream is=new fileinputstream(file);

outputstream os=new fileoutputstream(targetfile);

byte buf=new byte[1024];

int len;

while((len=is.read(buf))!=-1)

is.close();

os.close();

long end=system.currenttimemillis();

system.out.println("總共耗時:"+(end-start)+"毫秒");

return true;

}

java IO流 字元流FileReader

1.字元流是什麼 字元流是可以直接讀寫字元的io流 字元流讀取字元,就要先讀取到位元組資料,然後轉為字元.如果要寫出字元,需要把字元轉為位元組再寫出.樣列 filereader fr new filereader txt int x fr.read system.out.println x char...

Java IO流中拷貝(複製)檔案的寫法

1.使用位元組流讀寫複製檔案 位元組流讀寫複製檔案 param src 原始檔 param out 目標檔案 public static void inputstreamoutputstream string src,string out catch filenotfoundexception e ...

java IO 檔案流詳解

本篇博文學習了一下內容 fileinputstream 位元組輸入流 構造方法 讀取檔案 fileoutputstream 位元組輸出流 構造方法 寫入檔案 filerearder 字元輸入流 構造方法 讀取檔案 filewriter 字元輸出流 構造方法 寫出檔案 位元組檔案和字元檔案 總結 構造...