ex1
字节流方式写入 “追加”的时候,在生成流对象时,设置第二个参数为true。FileOutputStream fos=new FileOutputStream(toFile,true);//设置成true可以追加
package CH9.T01;
import java.io.IOException;
/**
* Created by Fant on 2016/12/18.
*/
public class Main {
public static void main (String[] args) throws IOException{
String source="D:\\JavaProjects\\School\\src\\CH9\\T01\\sourceFile.txt" ;
String target="D:\\JavaProjects\\School\\src\\CH9\\T01\\target.txt" ;
FileAppend fa=new FileAppend(source,target);
fa.excute();
}
}
package CH9.T01;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by Fant on 2016/12/18.
*/
public class FileAppend {
private String fromFile;
private String toFile;
FileAppend(String _fromFile,String _toFile){
fromFile=_fromFile;
toFile=_toFile;
}
public void excute () throws IOException{
FileInputStream fis=new FileInputStream(fromFile);
FileOutputStream fos=new FileOutputStream(toFile,true );
int data;
while ((data=fis.read())!=-1 ){
fos.write(data);
}
}
}
ex2
BufferedWriter
, BufferedReader
必须要刷新流。但是由于 换行 ,Enter键按下 会自动换行,因此有时不需要 flush
void InputInfo()throws IOException{
InputStreamReader inputStreamReader=new InputStreamReader(System.in);
BufferedReader cmdBr=new BufferedReader(inputStreamReader);
String line=null ;
while ((line=cmdBr.readLine())!=null ){
if (line.length()==0 ){
break ;
}
bw.write(line);
bw.newLine();
}
bw.close();