本文目錄一覽:
java如何拷貝文件到另一個目錄下
/**
*
複製單個文件
*
@param
oldPath
String
原文件路徑
如:c:/fqf.txt
*
@param
newPath
String
複製後路徑
如:f:/fqf.txt
*
@return
boolean
*/
public
void
copyFile(String
oldPath,
String
newPath)
{
try
{
int
bytesum
=
0;
int
byteread
=
0;
File
oldfile
=
new
File(oldPath);
if
(oldfile.exists())
{
//文件存在時
InputStream
inStream
=
new
FileInputStream(oldPath);
//讀入原文件
FileOutputStream
fs
=
new
FileOutputStream(newPath);
byte[]
buffer
=
new
byte[1444];
int
length;
while
(
(byteread
=
inStream.read(buffer))
!=
-1)
{
bytesum
+=
byteread;
//字節數
文件大小
System.out.println(bytesum);
fs.write(buffer,
0,
byteread);
}
inStream.close();
}
}
catch
(Exception
e)
{
System.out.println(“複製單個文件操作出錯”);
e.printStackTrace();
}
}
/**
*
複製整個文件夾內容
*
@param
oldPath
String
原文件路徑
如:c:/fqf
*
@param
newPath
String
複製後路徑
如:f:/fqf/ff
*
@return
boolean
*/
public
void
copyFolder(String
oldPath,
String
newPath)
{
try
{
(new
File(newPath)).mkdirs();
//如果文件夾不存在
則建立新文件夾
File
a=new
File(oldPath);
String[]
file=a.list();
File
temp=null;
for
(int
i
=
0;
i
file.length;
i++)
{
if(oldPath.endsWith(File.separator)){
temp=new
File(oldPath+file[i]);
}
else{
temp=new
File(oldPath+File.separator+file[i]);
}
if(temp.isFile()){
FileInputStream
input
=
new
FileInputStream(temp);
FileOutputStream
output
=
new
FileOutputStream(newPath
+
“/”
+
(temp.getName()).toString());
byte[]
b
=
new
byte[1024
*
5];
int
len;
while
(
(len
=
input.read(b))
!=
-1)
{
output.write(b,
0,
len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夾
copyFolder(oldPath+”/”+file[i],newPath+”/”+file[i]);
}
}
}
catch
(Exception
e)
{
System.out.println(“複製整個文件夾內容操作出錯”);
e.printStackTrace();
}
}
利用JAVA語言編寫一個 名為copy的程序 實現文件的拷貝功能,應該怎樣做?
import java.io.File;\x0d\x0aimport java.io.FileInputStream;\x0d\x0aimport java.io.FileNotFoundException;\x0d\x0aimport java.io.FileOutputStream;\x0d\x0aimport java.io.IOException;\x0d\x0apublic class Copy {\x0d\x0a/**\x0d\x0a* @param args\x0d\x0a*/\x0d\x0apublic static void main(String[] args) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0aif(args.length!=2){\x0d\x0aSystem.out.print(“沒有輸入正確數目的參數,程序退出!”);\x0d\x0aSystem.exit(0);\x0d\x0a}\x0d\x0aFile fileS = new File(“./”+args[0]);\x0d\x0aFile fileD = new File(“./”+args[1]);\x0d\x0aif(fileD.exists())System.out.println(“目標文件 “+args[1]+” 已存在!”);\x0d\x0abyte[] temp = new byte[50];\x0d\x0aint totalSize = 0;\x0d\x0atry {\x0d\x0aFileInputStream fr = new FileInputStream(fileS);\x0d\x0aFileOutputStream fo = new FileOutputStream(fileD);\x0d\x0aint length = 0;\x0d\x0awhile((length = fr.read(temp, 0, temp.length)) != -1){\x0d\x0atotalSize += length;\x0d\x0afo.write(temp, 0, length);\x0d\x0a}\x0d\x0aSystem.out.println(“文件 “+args[0]+” 有 “+totalSize+” 個字節”);\x0d\x0aSystem.out.println(“複製完成!”);\x0d\x0a} catch (FileNotFoundException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0aSystem.out.println(“源文件 “+args[0]+” 不存在!”);\x0d\x0a} catch (IOException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a}
java按比特位進行COPY
System.arrayCopy可以實現byte數組的copy,可以基本解決你的問題,不過如果bit不是8的倍數可能會多拷貝幾個,做個掩碼,把最後一個字節與(and)一下,就行了。
Java怎麼實現文件拷貝
工具/原料
一台配置了java環境的電腦
一款適合自己的開發集成環境,這裡用的是eclipse Kepler
文件拷貝DEMO
1.首先,理清思路,然後我們再動手操作。
拷貝,有源文件,和目的文件。
如果原文件不存在,提示,報錯。
如果目的文件不存在,創建空文件並被覆蓋。
如果目的地址,也即目的路徑不存在,創建路徑。
拷貝,輸入流,輸出流,關閉流。
拷貝前輸出文件大小,計算拷貝大小,比較並核實。輸出。
2.首先呢,先判斷傳參是否完整。
如果不夠兩個參數,或者多於兩個參數,提示錯誤。
如果目標文件不存在,創建 空文件繼續複製。
3.在開始前,輸出被拷貝的源文件的大小。
4.獲得文件名稱,即短名。也即路徑下的文件全名(包括文件擴展名)。
5.拷貝的關鍵,這裡用的簡單的緩衝流。從源文件到目的文件。
number of bytes copied 即是對拷貝長度的累計,直到拷貝完成,輸出。
6.將步驟二中的判斷並拷貝文件的代碼寫在一個main函數中,
執行拷貝,拷貝完成。結果拷貝大小和源文件大小一致,成功。
7.在執行前,記得輸入參數。
如果是使用命令提示符,執行 javac CopyFile.java 之後,
執行 java CopyFile [源文件長名] [目的文件長名]
如果是使用的eclipse,在運行前設置一下運行參數,完成後點擊運行,如下圖。
P.S. 這裡面的所謂“長名”是指完整絕對路徑+文件名+文件類型擴展名
這裡的源文件及目的文件的名稱分別為:
E:/IP_Data.rar 和 D:/testFiles/IP_Data.rar
END
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/154567.html