在進行IO中的輸出流的時候,有時會遇到輸出的文件中沒有被寫入內容,這時可能是由於沒使用flush()函數
故,可以這樣寫:
public class demo1 {
public static void main(String[] args) {
File fileOutput = new File("D:\\workspaceIDEA\\demo1\\src\\com\\stream\\aa.txt");
String inputPath = "D:\\workspaceIDEA\\demo1\\src\\com\\stream\\ss.txt";
try {
if (!fileOutput.exists()){
fileOutput.createNewFile();
}
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(inputPath));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(fileOutput));
int len;
byte[] bytes = new byte[1024];
while ((len=bufferedInputStream.read(bytes))!=-1){
bufferedOutputStream.write(bytes,0,len);
//調用flush函數
bufferedOutputStream.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}