本文目錄一覽:
- 1、java複製文件夾以及其中的文件
- 2、java如何拷貝一個文件夾內的多個指定的文件到另外一個指定的文件夾下?
- 3、java中複製文件的兩種方法是什麼
- 4、java 編寫FileCopy類,要求將1個文件的內容同時複製成多個文件.使用命令行完成文件名的輸入。
- 5、java如何複製拷貝一個文件到另一個文件夾?如:a文件夾中的.data文件拷貝到b文件夾。
- 6、JAVA高手請進!求一個JAVA程序:將一個文件中的內容複製到另一個文件中。
java複製文件夾以及其中的文件
這是 我修改後的,能使用。 package configuration;import java.io.*;
/**
* @author Administrator
*
* Class Function:
*/
public class copyDirectory {
public void startCopy(String topath,String frompath) throws IOException {
File copy=new File(topath); //拷貝到何處 路徑
File bycopy=new File(frompath); //從何處拷貝 路徑
//創建備份目錄
copy.mkdirs();
//開始拷貝
File[] file=bycopy.listFiles();
try{
if(file.length!=0){
for(int i=0;ifile.length;i++){
if(file[i].isFile()){
FileInputStream input=new FileInputStream(file[i]);
FileOutputStream output=new FileOutputStream(copy+”\\”+file[i].getName());
byte[] b=new byte[1024*10];
int len=input.read(b);
System.out.println(“=========================================是文件================================================================”);
System.out.println(“拷貝”+topath+”\\”+file[i].getName());
System.out.println(“目標”+frompath+”\\”+file[i].getName());
while((len=input.read(b))!=-1){
output.write(b,0,len);
}
output.flush();
output.close();
input.close();
}
if(file[i].isDirectory()){
copyDirectiory(copy+”\\”+file[i].getName(),bycopy+”\\”+file[i].getName());
System.out.println(“=========================================是目錄================================================================”);
System.out.println(“拷貝”+topath+”\\”+file[i].getName());
System.out.println(“目標”+frompath+”\\”+file[i].getName());
}
}
}
}catch(Exception e){
}finally{
file=null;
}
}
public void copyDirectiory(String topath,String frompath) throws IOException{
File copy=new File(topath);
File bycopy=new File(frompath);
//創建拷貝目錄
copy.mkdirs();
//開始拷貝
File[] file=bycopy.listFiles();
try{
if(file.length!=0){
for(int i=0;ifile.length;i++){
if(file[i].isFile()){
FileInputStream input=new FileInputStream(file[i]);
FileOutputStream output=new FileOutputStream(copy+”\\”+file[i].getName());
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(file[i].isDirectory()){
copyDirectiory(copy+”\\”+file[i].getName(),bycopy+”\\”+file[i].getName());
}
}
}
}catch(Exception e){
}finally{
file=null;
}
}}
java如何拷貝一個文件夾內的多個指定的文件到另外一個指定的文件夾下?
你好:
請看代碼:
/**
* 把一個文件夾里的所有文件包括文件夾 一併原樣拷貝到另一個目錄中;
*@author shuishui
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyDir001 {
public static File dirFrom;
public static File dirTo;
// 目標路徑創建文件夾
public void listFileInDir(File file) {
File[] files = file.listFiles();
for (File f : files) {
String tempfrom = f.getAbsolutePath();
String tempto = tempfrom.replace(dirFrom.getAbsolutePath(),
dirTo.getAbsolutePath()); // 後面的路徑 替換前面的路徑名
if (f.isDirectory()) {
File tempFile = new File(tempto);
tempFile.mkdirs();
listFileInDir(f);
} else {
System.out.println(“源文件:” + f.getAbsolutePath());
//
int endindex = tempto.lastIndexOf(“\\”);// 找到”/”所在的位置
String mkdirPath = tempto.substring(0, endindex);
File tempFile = new File(mkdirPath);
tempFile.mkdirs();// 創建立文件夾
System.out.println(“目標點:” + tempto);
copy(tempfrom, tempto);
}
}
}
/**
* 封裝好的文件拷貝方法
*/
public void copy(String from, String to) {
try {
InputStream in = new FileInputStream(from);
OutputStream out = new FileOutputStream(to);
byte[] buff = new byte[1024];
int len = 0;
while ((len = in.read(buff)) != -1) {
out.write(buff, 0, len);
}
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
File fromfile = new File(“e:\\shui\\test”);// 源文件夾
File tofile = new File(“e:\\Jying\\shui”);// 目標
CopyDir001 copy = new CopyDir001();
// 設置來源去向
copy.dirFrom = fromfile;
copy.dirTo = tofile;
copy.listFileInDir(fromfile);
}
}
java中複製文件的兩種方法是什麼
第一種方法:古老的方式
public static long forJava(File f1,File f2) throws Exception{
long time=new Date().getTime();
int length=2097152;
FileInputStream in=new FileInputStream(f1);
FileOutputStream out=new FileOutputStream(f2);
byte[] buffer=new byte[length];
while(true){
int ins=in.read(buffer);
if(ins==-1){
in.close();
out.flush();
out.close();
return new Date().getTime()-time;
}else
out.write(buffer,0,ins);
}
}
方法的2參數分別是原始文件,和拷貝的目的文件.這裡不做過多介紹.
實現方法很簡單,分別對2個文件構建輸入輸出流,並且使用一個位元組數組作為我們內存的緩存器, 然後使用流從f1 中讀出數據到緩存里,在將緩存數據寫到f2裡面去.這裡的緩存是2MB的位元組數組
第2種方法:使用NIO中的管道到管道傳輸
public static long forTransfer(File f1,File f2) throws Exception{
long time=new Date().getTime();
int length=2097152;
FileInputStream in=new FileInputStream(f1);
FileOutputStream out=new FileOutputStream(f2);
FileChannel inC=in.getChannel();
FileChannel outC=out.getChannel();
int i=0;
while(true){
if(inC.position()==inC.size()){
inC.close();
outC.close();
return new Date().getTime()-time;
}
if((inC.size()-inC.position())20971520)
length=(int)(inC.size()-inC.position());
else
length=20971520;
inC.transferTo(inC.position(),length,outC);
inC.position(inC.position()+length);
i++;
}
}
實現方法:在第一種實現方法基礎上對輸入輸出流獲得其管道,然後分批次的從f1的管道中像f2的管道中輸入數據每次輸入的數據最大為2MB
java 編寫FileCopy類,要求將1個文件的內容同時複製成多個文件.使用命令行完成文件名的輸入。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.util.Date;
import java.util.Scanner;
public class FileCopy {
public static void main(String[] args) throws Exception {
File f1 = new File(“D:\\test\\test.txt”);
String path = “D:\\test\\”;
System.out.print(“請輸入要複製的文件個數:”);
Scanner sc = new Scanner(System.in);
int cnt = sc.nextInt();
for(int i = 0 ; i cnt ; i++){
System.out.print(“請輸入第”+(i+1)+”個文件名:”);
String newName = sc.next();
System.out.println(“第”+(i+1)+”個文件的名字為:”+newName+”.txt”);
File f2 = new File(path+newName+”.txt”);
forTransfer(f1,f2);
}
}
/**
* @author Samsung
* @date 2017年4月20日15:20:25
* 實現文件內容的複製
*
* */
public static long forTransfer(File f1,File f2) throws Exception{
long time=new Date().getTime();
int length=2097152;
FileInputStream in=new FileInputStream(f1);
FileOutputStream out=new FileOutputStream(f2);
FileChannel inC=in.getChannel();
FileChannel outC=out.getChannel();
int i=0;
while(true){
if(inC.position()==inC.size()){
inC.close();
outC.close();
return new Date().getTime()-time;
}
if((inC.size()-inC.position())20971520)
length=(int)(inC.size()-inC.position());
else
length=20971520;
inC.transferTo(inC.position(),length,outC);
inC.position(inC.position()+length);
i++;
}
}
}
java如何複製拷貝一個文件到另一個文件夾?如:a文件夾中的.data文件拷貝到b文件夾。
你可以個java inputStrem流和outputStream流來實現這個功能。
import java.io.*;
public class FileStreamDemo {
public static void main(String[] args) {
try {
// 來源文件
FileInputStream in = new FileInputStream(“D:/b.txt”);
// 目的文件
FileOutputStream out = new FileOutputStream(“C:/a.txt”);
byte[] bytearray = new byte[1024];
do {
in.read(bytearray, 0, 1024);
out.write(bytearray);
} while (in.available() 0);
in.close();
out.close();
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
JAVA高手請進!求一個JAVA程序:將一個文件中的內容複製到另一個文件中。
最簡單的io流問題,不用什麼高手,
我給你寫個方法,參數是2個字元串,第一個寫原文件的全路徑,第二個寫目標文件的全路進。 你試試吧
public void copy(String fromFilePath, String toFilePath) {
try {
FileInputStream fis = new FileInputStream(fromFilePath);
FileOutputStream fos = new FileOutputStream(toFilePath);
byte[] b = new byte[100];
try {
while (fis.read(b) != (-1)) {
fos.write(b);
}
if (fis != null) {
fis.close();
fis = null;
}
if (fos != null) {
fos.flush();
fos.close();
fos = null;
}
} catch (IOException e) {
System.out.println(“io異常”);
}
} catch (FileNotFoundException e) {
System.out.println(“源文件不存在”);
}
public static void main(String[] args) {
//自己把路徑補齊,別忘了!!!!!!!!!!!!!!!!
String fromFilePath=” “; // 源文件的全路徑。 比方”d://myphoto//nihao.mp3”
String toFilePath=” “; //目標文件的全路勁。 如果不存在會自動建立,如存在則在文件尾繼續添加
new CopyTest().copy(fromFilePath, toFilePath);
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/289317.html