1. 程式人生 > >java io實現檔案複製

java io實現檔案複製


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {

	void readFile(String fromPath, String toPath) {

		File fromfile = new File(fromPath);
		File tofile = new File(toPath);

		FileInputStream inputStream = null;
		FileOutputStream outputStream = null;

		BufferedInputStream bufferedInputStream = null;
		BufferedOutputStream bufferedOutputStream = null;
		try {

			inputStream = new FileInputStream(fromfile);
			outputStream = new FileOutputStream(tofile);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		if (null != inputStream && null != outputStream) {

			bufferedInputStream = new BufferedInputStream(inputStream);
			bufferedOutputStream = new BufferedOutputStream(outputStream);
			byte[] b = new byte[1024];
			try {
				while (-1 != (bufferedInputStream.read(b))) {
					bufferedOutputStream.write(b);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

		if (null != bufferedInputStream) {

			try {
				bufferedInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (null != bufferedOutputStream) {
			try {
				bufferedOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (null != inputStream) {
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (null != outputStream) {
			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		new Test().readFile("d:/test.docx", "d:/test2.docx");

	}

}

相關推薦

java io實現檔案複製

import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java

Java API實現檔案複製

public class FileUtil { /** * 利用緩衝流複製檔案 * @param from原始檔 * @param to目標檔案 */ public static boolean bufCopy(String from,String t

java實現檔案複製的方法

路徑: String oldpath = "/sdcard/youku/HyteraAppStore.apk"; String newpath = "/sdcard/youku/youku_vip_logger/HyteraAppStore.apk"; 複製方法: pub

Java實現檔案複製(servlet)

String filePath = request.getParameter("filePath");filePath = request.getServletContext().getRealPath("/") + filePath;String targetPath =

6、使用IO實現檔案複製並修改檔案字尾

程式設計題目: 6.編寫一個程式,將D:\java目錄下的所有.java檔案複製到D:\jad目錄下,並將原來檔案的副檔名從.java改為.jad。 示例程式碼: package program.stream.exercise06; impo

Java實現檔案複製

程式碼實現複製檔案 /*  * 編寫一個程式,將d:\java目錄下的所有.java檔案複製到d:\jad目錄下,並將原來檔案的副檔名從.java改為.jad。 * <複製前改名> */public class CopyFolder {public stat

IO實現檔案複製

import  java.io.*; public  class IO2 { public static void main(String []args){       try { File f1= new File("F:" + File.separator + "do

java實現檔案複製功能

簡單的檔案複製程式,當然只是複製過程中的一部分,仔細分析還有很多功能,比如判斷磁碟是否有足夠空間,效率等問題,這些問題後面會牽涉到 public void copyFolder(String oldPath,String newPath){ File nf = new F

Java實現檔案複製的兩種方式

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import j

java 實現檔案複製的兩種方式

  檔案複製首先我們要考慮的是用字元流還是檔案流操作呢?     若我們考慮的僅僅是文字之類的複製,字元流要好些。但是不可能總是文字檔案吧,有可能是聲音,影象,視訊等等,所以使用位元組流操作比較好。     複製的兩種方式       1.將原始檔中的內容全部讀取到記憶體中,並一次性寫入到目標檔案      

Java UDP實現檔案傳輸

編寫基於TCP/IP網路利用UDP通訊的二進位制檔案傳輸程式,傳輸到指定對端後要存檔。 利用FileStreams類實現檔案傳輸。 程式存放格式: Receive_Thread.java package UDP_File; import java.io.File; import

物理機與遠端伺服器或者虛擬機器實現檔案複製(SCP)

1.使用SCP命令實現 參考:https://blog.csdn.net/yhblog/article/details/83927141 2.使用filezilla工具實現檔案複製 注:使用filezilla傳輸前需要填寫遠端伺服器ip,使用者名稱,密碼(填寫使用者名稱最好填寫要用

檔案流,實現檔案複製

package com.io.demo1; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * 檔案流,實現檔案複製 */ public class

java web實現檔案下載

javaweb實現檔案下載 實現檔案下載 解決下載檔名帶有中文亂碼問題 效果圖 實現思路 根據請求連接獲取檔名和檔案型別,設定響應頭,獲取輸入流和輸出流 核心程式碼 public void doGet(HttpServletR

Java Swing實現檔案的簡單讀取

用java Swing實現讀寫檔案的操作 開發平臺:eclipse 安裝WindowsBuilder外掛 參考:https://blog.csdn.net/stormdony/article/details/79497030 新建專案及介面佈局 1.File->new-&g

java IO檔案檔案

今天,線上出了一點問題,需要通過檢視日誌才能解決。最終,也的確是通過檢視日誌解決了問題。接著就需要對日誌檔案進行過濾,查詢日誌檔案中我們想要的資料,然後存入庫中。 於是寫了一個簡單的讀寫檔案操作。日後再使用的時候,我就不再去寫了,直接copy就好了。方便的記錄一下: /**

java IO:取得檔案或目錄資訊(是否為檔案/目錄,檔案大小,最後修改日期)

public class TestDemo3 { public static void main(String[] args) { File file=new File("h:"+File.separator+"my.exe"); if

Java Servlet實現檔案上傳並讀取Zip壓縮包中檔案的真實型別

1.上傳檔案servlet PS: 使用ant.jar中的 org.apache.tools.zip.ZipEntry 物件,防止亂碼 package com.chenl.servlets; import java.io.File; import java.io.IOExcep

Java Commons實現檔案的歸檔壓縮

轉載: https://snowolf.iteye.com/blog/648652  在linux下,tar是一個歸檔命令。當然,如果配合gzip、bzip2就可以達到歸檔+壓縮的效果!  我們通過tar獲得歸檔壓縮檔案其實恰恰包含了歸檔和壓縮兩個操作,並且其操作次

Qt5 實現檔案複製並在指定路徑下建立資料夾

最近在基於qt5平臺的應用介面上想實現本地選擇檔案,並把檔案拷貝到指定路徑下。找到以下這篇文章參考,參考其中幾個函式的寫法實現了想要的功能,故記錄一下,方便以後查詢參考。 我在標頭檔案中定義幾個變數:        QFileDialog    *mFil