IO文件復制粘貼
阿新 • • 發佈:2019-03-06
string bubuko ack com nts tac leo pri 技術分享
項目
package com.I_O; import java.io.*; /** * 文件文件 */ public class Copy_I_O { /** * 復制粘貼方法 */ public void copy(){ InputStream is=null; OutputStream os=null; try { is=new FileInputStream("復制.txt"); os=new FileOutputStream("粘貼.txt"); byte[] bytes=new byte[1024]; int len=-1; try { while((len=is.read(bytes))!=-1){ System.out.println("復制完成"); } //復制對象 os.write(bytes,0,bytes.length); //粘貼對象 System.out.println("粘貼成功"); /** * 遍歷集合 */ System.out.println("復制的內容:"); for(byte b:bytes){ if(b==0){ continue; } System.out.print((char)b); } }catch (IOException e) { e.printStackTrace(); }finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } } /** * 創建文件方法 */ public void newfile(){ File file=new File("復制.txt"); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } /** * 復制粘貼main方法 */ public static void main(String[] args) { Copy_I_O c=new Copy_I_O(); c.copy(); } }
運行
IO文件復制粘貼