
java程序题
利用文件输入/输出流编写一个实现文件拷贝的应用程序,路径名以及源文件名和目标文件名通过命令行参数传入。成员变量:PrivateStringSonrcePath//源路径P...
利用文件输入/输出流编写一个实现文件拷贝的应用程序,路径名以及源文件名和目标文件名通过命令行参数传入。
成员变量:Private String SonrcePath //源路径
Private String SonrceFile //源文件
Private String DestLath //目标路径
Private String DestFile //目标文件 展开
成员变量:Private String SonrcePath //源路径
Private String SonrceFile //源文件
Private String DestLath //目标路径
Private String DestFile //目标文件 展开
1个回答
推荐于2016-10-08
展开全部
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.util.Scanner;
public class CopyTest {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("请输入源路径名:");
Scanner s1 = new Scanner(System.in);
String SonrcePath = s1.next(); // 源路径
System.out.println(SonrcePath);
System.out.println("请输入源文件名:");
Scanner s2 = new Scanner(System.in);
String SonrceFile = s2.next(); // 源文件
System.out.println("请输入目标路径名:");
Scanner s3 = new Scanner(System.in);
String DestLath = s3.next();// 目标路径
System.out.println("请输入目标路径名:");
Scanner s4 = new Scanner(System.in);
String DestFile = s4.next();// 目标文件
// TODO Auto-generated method stub
File f1 = new File(SonrcePath + File.separator + SonrceFile);
File f2 = new File(DestLath + File.separator + DestFile);
CopyTest.copyFile(f2, f1);
}
/**
* 复制文件。targetFile为目标文件,file为源文件
*
* @param targetFile
* @param file
*/
public static void copyFile(File targetFile, File file) {
if (targetFile.exists()) {
System.out.println("文件" + targetFile.getAbsolutePath()
+ "已经存在,跳过该文件!");
return;
} else {
createFile(targetFile, true);
}
System.out.println("复制文件" + file.getAbsolutePath() + "到"
+ targetFile.getAbsolutePath());
try {
InputStream is = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
fos.write(buffer);
}
is.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void createFile(File file, boolean isFile) {
if (!file.exists()) {
if (!file.getParentFile().exists()) {
createFile(file.getParentFile(), false);
} else {
if (isFile) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
file.mkdir();
}
}
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class CopyTest {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("请输入源路径名:");
Scanner s1 = new Scanner(System.in);
String SonrcePath = s1.next(); // 源路径
System.out.println(SonrcePath);
System.out.println("请输入源文件名:");
Scanner s2 = new Scanner(System.in);
String SonrceFile = s2.next(); // 源文件
System.out.println("请输入目标路径名:");
Scanner s3 = new Scanner(System.in);
String DestLath = s3.next();// 目标路径
System.out.println("请输入目标路径名:");
Scanner s4 = new Scanner(System.in);
String DestFile = s4.next();// 目标文件
// TODO Auto-generated method stub
File f1 = new File(SonrcePath + File.separator + SonrceFile);
File f2 = new File(DestLath + File.separator + DestFile);
CopyTest.copyFile(f2, f1);
}
/**
* 复制文件。targetFile为目标文件,file为源文件
*
* @param targetFile
* @param file
*/
public static void copyFile(File targetFile, File file) {
if (targetFile.exists()) {
System.out.println("文件" + targetFile.getAbsolutePath()
+ "已经存在,跳过该文件!");
return;
} else {
createFile(targetFile, true);
}
System.out.println("复制文件" + file.getAbsolutePath() + "到"
+ targetFile.getAbsolutePath());
try {
InputStream is = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
fos.write(buffer);
}
is.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void createFile(File file, boolean isFile) {
if (!file.exists()) {
if (!file.getParentFile().exists()) {
createFile(file.getParentFile(), false);
} else {
if (isFile) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
file.mkdir();
}
}
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询