File f1 = new File(args[0]); // 源文件的File對象 File f2 = new File(args[1]); // 目標文件的File對象 InputStream input = null; // 準備好輸入流對象,讀取源文件 OutputStream out = null; // 準備好輸出流對象,寫入目標文件 input = new FileInputStream(f1); out = new FileOutputStream(f2); int temp = 0; while ((temp = input.read()) != -1) { // 開始拷貝 out.write(temp); // 邊讀邊寫 } input.close(); // 關閉 out.close(); // 關閉
InputStream input = System.in ; // 從鍵盤接收數據 byte b[] = new byte[5] ; // 開闢空間,接收數據 System.out.print("請輸入內容:") ; // 提示信息 int len = input.read(b) ; // 接收數據 System.out.println("輸入的內容爲:" + new String(b,0,len)) ; input.close() ; // 關閉輸入流
BufferedReader buf = null ; // 聲明對象 buf = new BufferedReader(new InputStreamReader(System.in)) ; // 將字節流變爲字符流 String str = null ; // 接收輸入內容 System.out.print("請輸入內容:") ; try{ str = buf.readLine() ; // 讀取一行數據 }catch(IOException e){ e.printStackTrace() ; // 輸出信息 } System.out.println("輸入的內容爲:" + str) ;
File f = new File("D:" + File.separator + "test.txt"); // 指定操做文件 Scanner scan = null; scan = new Scanner(f); // 從鍵盤接收數據 StringBuffer str = new StringBuffer(); while (scan.hasNext()) { str.append(scan.next()).append('\n'); // 取數據 System.out.println("文件內容爲:" + str); }
InputStream is1 = null ; // 輸入流1 InputStream is2 = null ; // 輸入流1 OutputStream os = null ; // 輸出流 SequenceInputStream sis = null ; // 合併流 is1 = new FileInputStream("d:" + File.separator + "a.txt") ; is2 = new FileInputStream("d:" + File.separator + "b.txt") ; os = new FileOutputStream("d:" + File.separator + "ab.txt") ; sis = new SequenceInputStream(is1,is2) ; // 實例化合並流 int temp = 0 ; // 接收內容 while((temp=sis.read())!=-1){ // 循環輸出 os.write(temp) ; // 保存內容 }