import java.io.*;public class Copy {
/** Main method
@param args[0] for sourcefile
@param args[1] for target file
*/
public static void main(String[] args) throws IOException {
// Check command line parameter usage
if (args.length != 2) {
System.out.println(
"Usage: java CopyFile sourceFile targetfile");
System.exit(0);
} // Check if source file exists
File sourceFile = new File(args[0]);
if (!sourceFile.exists()) {
System.out.println("Source file " + args[0] + " not exist");
System.exit(0);
} // Check if target file exists
File targetFile = new File(args[1]);
if (targetFile.exists()) {
System.out.println("Target file " + args[1] + " already exists");
System.exit(0);
} // Create an input stream
BufferedInputStream input =
new BufferedInputStream(new FileInputStream(sourceFile)); // Create an output stream
BufferedOutputStream output =
new BufferedOutputStream(new FileOutputStream(targetFile)); // Display the file size
System.out.println("The file " + args[0] + " has "+
input.available() + " bytes"); // Continuously read a byte from input and write it to output
int r;
while ((r = input.read()) != -1)
output.write((byte)r); // Close streams
input.close();
output.close(); System.out.println("Copy done!");
}
}上面的代码中:
if (args.length != 2) {
System.out.println(
"Usage: java CopyFile sourceFile targetfile");
System.exit(0);
}此段代码的意义何在,很是费解。求解答
/** Main method
@param args[0] for sourcefile
@param args[1] for target file
*/
public static void main(String[] args) throws IOException {
// Check command line parameter usage
if (args.length != 2) {
System.out.println(
"Usage: java CopyFile sourceFile targetfile");
System.exit(0);
} // Check if source file exists
File sourceFile = new File(args[0]);
if (!sourceFile.exists()) {
System.out.println("Source file " + args[0] + " not exist");
System.exit(0);
} // Check if target file exists
File targetFile = new File(args[1]);
if (targetFile.exists()) {
System.out.println("Target file " + args[1] + " already exists");
System.exit(0);
} // Create an input stream
BufferedInputStream input =
new BufferedInputStream(new FileInputStream(sourceFile)); // Create an output stream
BufferedOutputStream output =
new BufferedOutputStream(new FileOutputStream(targetFile)); // Display the file size
System.out.println("The file " + args[0] + " has "+
input.available() + " bytes"); // Continuously read a byte from input and write it to output
int r;
while ((r = input.read()) != -1)
output.write((byte)r); // Close streams
input.close();
output.close(); System.out.println("Copy done!");
}
}上面的代码中:
if (args.length != 2) {
System.out.println(
"Usage: java CopyFile sourceFile targetfile");
System.exit(0);
}此段代码的意义何在,很是费解。求解答
解决方案 »
- 各位好。请教一个问题。
- 求Java绘图效果思路,类似于动态折线报表,有用就给分
- Table中某列定义为boolean, 而查询数据库得到的是0/1,如果直接add,出错!
- 求教一个复杂算法,高手请进
- 怎么使用j2se帮助帮档
- 求教JAVA原始类型编码问题
- 调查:你现在用哪种开发工具,感觉如何?
- 菜鸟问题,请大哥执教,来者有分!
- 哪里可以下载IntelliJ IDEA 2.5/2.6, 最好是免费的,有注册码.多谢
- 提问一个简单问题,在main()函数中的(String[] args),代表什么?应该怎么用呢??谢谢
- 一个秒表但是结束不了,用线程写的。求助啊。。。
- 反射Class类后面的<>是干什么的
因为拷贝起码需要有输入输出文件,2个路径。
import java.util.Scanner;
public class FileCopyDemo {
public static void main(String args[]) throws IOException{
Scanner cin = new Scanner(System.in);
String path1 = null,path2 = null;
while(cin.hasNext()){
path1 = cin.next();
path2 = cin.next();
break;
}
File fs = new File(path1);
File fd = new File(path2);
if(!fd.exists()){
System.out.println("要复制的文件不存在,请重新输入文件的路径");
System.exit(1);
}
OutputStream os = null;
InputStream is = null;
os = new FileOutputStream(fs);
is = new FileInputStream(fd);
if(os!=null && is!=null){
int temp = 0;
while((temp=is.read())!=-1){
os.write((byte)temp);
}
System.out.println("复制成功!");
is.close();
os.close();
}
else{
System.out.println("复制失败");
}
}
}
如果args.length 为 0 or 1 ,那么args[0],args[1]会报数组越界的错误、
如果args.length 大于 2 ,程序不会报错,只不过前2个arg是有效的,后面的是无用的
加了if (args.length != 2)可以保证参数的数目是符合程序要求的
lz可以试试只输一个arg,然后把if (args.length != 2)一段给删了,应该会报错
java copy t1.java t2.java
这样的话,args是自动接收t1和t2的?