JAva書き込みファイル読み書き操作(IOストリーム、バイトストリーム)

1387 ワード


package copyfile; import java.io.*; public class copy { public static void main(String[] args) throws IOException { copyFile("d:/new/a.txt","d:/new/b.txt",true);//oldpath,newpath, } public static void copyFile(String oldpth,String newpath,boolean add) throws IOException{ FileInputStream in = null; FileOutputStream fs = null; try { // , File oldfile=new File(oldpth); if(oldfile.exists()){ // in=new FileInputStream(oldpth); fs=new FileOutputStream(newpath,add); // byte[] buffer=new byte[10]; int length; while(true){ int len=in.read(buffer);// , -1, /* : * String s=new String(buffer); * s.trim();( ) */ if(len==-1)break; fs.write(buffer); } System.out.println("OK"); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ in.close(); fs.close(); } } }

/***IOストリームのデータ書き込みと読み出し*本質的にはFileReader(「c:text.txt」)またはFileWriter(「c:text 2.txt」)*read()またはwrite()を介して単一文字を読み出しまたは書き込みデータに格納*操作が面倒で効率が悪い.その後、バッファフローの概念が導入されたため、*BufferedReaderオブジェクトが出現し、このオブジェクトを介してfile Readで読み取ったデータをバッファ*して効率を向上させる**/