1. Use Bufferedinputstream to read the file stream and Bufferedoutputstream write the file stream:
protected Static voidwriteFile2 (String InputPath, String outputPath) {Bufferedinputstream bis=NULL; Bufferedoutputstream Bos=NULL; Try{bis=NewBufferedinputstream (NewFileInputStream (InputPath)); Bos=NewBufferedoutputstream (NewFileOutputStream (OutputPath)); intBuffer = 1024 * 1024; byte[] data =New byte[buffer]; intLen; while(len = bis.read (data))! =-1) {bos.write (data,0, Len); } } Catch(Exception e) {e.printstacktrace (); } finally { Try { if(bis! =NULL) {bis.close (); } if(Bos! =NULL) {bos.close (); } } Catch(IOException e) {} }}
2. BufferedReader read file stream and BufferedWriter write file stream:
protected Staticstring ReadFile (String inputpath) {BufferedReader br=NULL; Try{br=NewBufferedReader (NewInputStreamReader (NewFileInputStream (InputPath)); StringBuffer result=NewStringBuffer (); String lineString=NULL; while((lineString = Br.readline ())! =NULL) {result.append (lineString); Result.append ("\ n"); } returnresult.tostring (); } Catch(IOException e) {e.printstacktrace (); return NULL; } finally { Try { if(BR! =NULL) {br.close (); } } Catch(IOException e) {} }}
protected Static voidWriteFile (String inputstring, String outputPath) {BufferedWriter bw=NULL; Try{BW=NewBufferedWriter (NewFileWriter (OutputPath)); Bw.write (inputstring); } Catch(IOException e) {e.printstacktrace (); } finally { Try { if(BW! =NULL) {bw.close (); } } Catch(IOException e) {} }}
Java input and Output stream summary