導航:首頁 > 文件教程 > java復制文件到文件夾

java復制文件到文件夾

發布時間:2022-09-13 00:55:51

『壹』 怎樣用java復制一個文件到指定目錄

import java.io.*;

public class CopyFile {
public static void main(String[] args) {
try{
FileInputStream input=new FileInputStream("f:\\downloads\\kon.jpg");//可替換為任抄何路徑何和文件名
FileOutputStream output=new FileOutputStream("f:\\kon.jpg");//可替換為任何路徑何和文件名

int in=input.read();
while(in!=-1){
output.write(in);
in=input.read();
}
}catch (IOException e){
System.out.println(e.toString());
}
}
}

『貳』 java怎麼復制一個文件到另一個文件夾

主要是用到裡面的i/o流。代碼例子如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
* java讀寫文件,復制文件
* 讀取d:/1.txt文件內容,寫入f:/text.txt文件中.
* @author young
*
*/
public class FileWriterTest {
// 讀寫文件
public static void rwFile(){
FileWriter fw = null;
BufferedReader br = null;
try {
fw = new FileWriter("f:\\text.txt", true);
br = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\1.txt"), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("文件內容: " + line);
fw.write(line);
fw.flush();
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
rwFile();
}
}
首先在D盤新建文件1.txt,輸入任意內容。然後執行java代碼即可。

『叄』 java中怎樣按位元組讀取文件並復制到另一個文件夾

這里以位元組流,FileOutputStream為例。代碼例子如下:

importjava.io.File;
/**
*把一個文件夾中的文件復制到一個指定的文件夾
*@authoryoung
*
*/
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;

publicclassCopyFile{
publicstaticvoidmain(String[]args){
/*指定源exe文件的存放路徑*/
Stringstr="f:/jdk-1_5_0_06-windows-i586-p.exe";
/*指定復制後的exe的目標路徑*/
Stringstrs="e:/.exe";
/*創建輸入和輸出流*/
FileInputStreamfis=null;
FileOutputStreamfos=null;

try{
/*將io流和文件關聯*/
fis=newFileInputStream(str);

fos=newFileOutputStream(strs);
byte[]buf=newbyte[1024*1024];
intlen;
while((len=fis.read(buf))!=-1){
fos.write(buf,0,len);

}
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
try{
fis.close();
fos.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
}

『肆』 java中IO怎麼將一個文件復制到另外一個文件夾

package tv.bilibili;

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

public class IoPlay {
public static void main(String[] args) throws IOException{
File f = new File("E:\\新建文本文檔.txt");
File f1 = new File("D:\\");
IoPlay io = new IoPlay();
io.(f,f1);
}

public void (File f,File f1) throws IOException{ //復制文件的方法!
if(!f1.exists()){
f1.mkdir();
}
if(!f1.exists()){//路徑判斷,是路徑還是單個的文件
File[] cf = f.listFiles();
for(File fn : cf){
if(fn.isFile()){
FileInputStream fis = new FileInputStream(fn);
FileOutputStream fos = new FileOutputStream(f1 + "\\" +fn.getName());
byte[] b = new byte[1024];
int i = fis.read(b);
while(i != -1){
fos.write(b, 0, i);
i = fis.read(b);
}
fis.close();
fos.close();
}else{
File fb = new File(f1 + "\\" + fn.getName());
fb.mkdir();
if(fn.listFiles() != null){//如果有子目錄遞歸復制子目錄!
(fn,fb);
}
}
}
}else{
FileInputStream fis = new FileInputStream(f);
FileOutputStream fos = new FileOutputStream(f1 + "\\" +f.getName());
byte[] b = new byte[1024];
int i = fis.read(b);
while(i != -1){
fos.write(b, 0, i);
i = fis.read(b);
}
fis.close();
fos.close();
}
}
}

以上是我自己寫的,講一個文件復制到例外一個地方,無論是文件,還是文件夾都可以

『伍』 JAVA 把指定的文件 復制到目標文件夾下 怎麼寫啊

// 由絕對路徑得到輸出流
//path源路徑
//fileCopeToPath 目標路徑
String path="D:\我的文檔\My Pictures\1.jpg ";
String fileCopeToPath=" D:\我的文檔\test ";
File file =new File(path);
if(file.exists){
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos= new FileOutputStream( fileCopeToPath+ File.separator +path.subString(path.lastIndexOf("/\"),path.length));
byte[] b = new byte[fis.available()];
int len = 0;
while ((len = fis.read(b)) != -1)
{
fos.write(b, 0, len);
fos.flush();
}
fos.close();
fis.close();
}

『陸』 java如何復制拷貝一個文件到另一個文件夾如:a文件夾中的.data文件拷貝到b文件夾。

你這JAVA基礎太一般了,有空多看看書吧,樓下那個不行,我給你寫一個等著啊

『柒』 java如何拷貝文件到另一個目錄下

/**
*
復制單個文件
*
@param
oldPath
String
原文件路徑
如:c:/fqf.txt
*
@param
newPath
String
復制後路徑
如:f:/fqf.txt
*
@return
boolean
*/
public
void
File(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
Folder(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()){//如果是子文件夾
Folder(oldPath+"/"+file[i],newPath+"/"+file[i]);
}
}
}
catch
(Exception
e)
{
System.out.println("復制整個文件夾內容操作出錯");
e.printStackTrace();
}
}

『捌』 java如何拷貝一個文件夾內的多個指定的文件到另外一個指定的文件夾下

你好:

請看代碼:

/**
*把一個文件夾里的所有文件包括文件夾一並原樣拷貝到另一個目錄中;
*@authorshuishui
*/
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;

publicclassCopyDir001{

publicstaticFiledirFrom;
publicstaticFiledirTo;

//目標路徑創建文件夾
publicvoidlistFileInDir(Filefile){
File[]files=file.listFiles();
for(Filef:files){
Stringtempfrom=f.getAbsolutePath();
Stringtempto=tempfrom.replace(dirFrom.getAbsolutePath(),
dirTo.getAbsolutePath());//後面的路徑替換前面的路徑名
if(f.isDirectory()){
FiletempFile=newFile(tempto);
tempFile.mkdirs();
listFileInDir(f);
}else{
System.out.println("源文件:"+f.getAbsolutePath());
//
intendindex=tempto.lastIndexOf("\");//找到"/"所在的位置
StringmkdirPath=tempto.substring(0,endindex);
FiletempFile=newFile(mkdirPath);
tempFile.mkdirs();//創建立文件夾
System.out.println("目標點:"+tempto);
(tempfrom,tempto);
}
}
}
/**
*封裝好的文件拷貝方法
*/
publicvoid(Stringfrom,Stringto){
try{
InputStreamin=newFileInputStream(from);
OutputStreamout=newFileOutputStream(to);

byte[]buff=newbyte[1024];
intlen=0;
while((len=in.read(buff))!=-1){
out.write(buff,0,len);
}
in.close();
out.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}

publicstaticvoidmain(String[]args){
Filefromfile=newFile("e:\shui\test");//源文件夾
Filetofile=newFile("e:\Jying\shui");//目標

CopyDir001=newCopyDir001();
//設置來源去向
.dirFrom=fromfile;
.dirTo=tofile;
.listFileInDir(fromfile);

}
}

『玖』 怎樣用Java復制文件到指定目錄 在線等,急!!!!

java的來File類對文件進行操作不涉及源編碼問題,只是流對象的處理而已
首先用File類定位到該文件夾,遍歷dll文件為一個數組存儲起來
然後循環遍歷這四個文件 使用outputstream(可以加緩沖)寫入你的目標文件夾

代碼量不是很大 只是邏輯必須清晰

而且如果不是必須用java操作 我覺得寫批處理比寫java簡單的多

『拾』 java 復制一個指定文件夾下的指定文件 到另一個指定文件夾下

下面是我學習過程中總結的幾個復制文件的方法,代碼如下:

/**
*復制媒體文件,該例子是復制1.mp3文件,列出了四種方式.
*/
importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;

{

publicstaticvoidmain(String[]args)throwsIOException{
/**
*共有四個方法,但建議用demo1,demo2;因為demo3需要創建數組,如果文件大,光創建數組就需要很多時間;demo4一定也不要用,
*效率非常慢.
*/
demo1();
demo2();
demo3();
demo4();

}

publicstaticvoiddemo1()throwsFileNotFoundException,IOException{
FileInputStreamfis=newFileInputStream("d:\1.mp3");
FileOutputStreamfos=newFileOutputStream("d:\01.mp3");

intlen=0;
byte[]buf=newbyte[1024];
while((len=fis.read(buf))!=-1){
fos.write(buf,0,len);
}
fis.close();
fos.close();
}

publicstaticvoiddemo2()throwsIOException{
FileInputStreamfis=newFileInputStream("d:\1.mp3");
BufferedInputStreambufis=newBufferedInputStream(fis);

FileOutputStreamfos=newFileOutputStream("d:\02.mp3");
BufferedOutputStreambufos=newBufferedOutputStream(fos);

intlen=0;
while((len=bufis.read())!=-1){
bufos.write(len);
}
bufis.close();
bufos.close();
}

//不建議這種方式
publicstaticvoiddemo3()throwsIOException{
FileInputStreamfis=newFileInputStream("d:\1.mp3");
FileOutputStreamfos=newFileOutputStream("d:\03.mp3");

byte[]buf=newbyte[fis.available()];
fis.read(buf);
fos.write(buf);
fos.close();
fis.close();

}

publicstaticvoiddemo4()throwsIOException{
FileInputStreamfis=newFileInputStream("d:\1.mp3");
FileOutputStreamfos=newFileOutputStream("d:\04.mp3");

intch=0;

while((ch=fis.read())!=-1){
fos.write(ch);
}
fos.close();
fis.close();
}
}
閱讀全文

與java復制文件到文件夾相關的資料

熱點內容
ps3文件分割視頻 瀏覽:280
微信圖片一鍵轉發軟體 瀏覽:331
如何判斷s200plc編程電纜 瀏覽:691
太原編程培訓班哪個好 瀏覽:171
樹葉吹奏教程 瀏覽:6
社交app帶來了哪些社會問題 瀏覽:394
如何安裝愛寶8800數據採集器 瀏覽:712
文件保存了怎麼找不到了 瀏覽:476
彩票網站怎麼辨真假 瀏覽:840
pr找不到該文件 瀏覽:963
java移除panel 瀏覽:354
jsp填充jsp 瀏覽:166
海關外貿大數據在哪裡查 瀏覽:381
思特奇java筆試題 瀏覽:121
葫蘆俠在手機中的文件名 瀏覽:813
plc編程應該怎麼收錢 瀏覽:584
c語言中源文件由什麼組成 瀏覽:890
linuxhttpdphp配置文件 瀏覽:607
拆單數據要怎麼保存 瀏覽:17
mac電腦怎樣壓縮文件到100m 瀏覽:645

友情鏈接