文件拷贝问题 你可以使用java.io.Filepublic boolean renameTo(File dest)方法应该基本上可以解决了,不过这个函数是move还是copy的. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*;public class mytest0 {static int filecopycount=1;public mytest0() {}public static void main(String[] args) throws FileNotFoundException,IOException,Exception {//接收参数并进行相应操作System.out.print("input:");DataInputStream ds=new DataInputStream(System.in);String str=ds.readLine();if(str.equals("copy")) copyfile(args[0],args[1]);else if(str.equals("delete")) deletefile(args[0]);else if(str.equals("write")) writefile(args[0]);else if(str.equals("list")) list(args[0]);else System.err.println("Input error!");ds.close();}private static void writefile(String str) throws IOException {//写入文件 OutputStream os=new FileOutputStream("xiruo_file.txt"); PrintStream ps=new PrintStream(os); ps.println(str); ps.close(); os.close(); System.exit(0);}private static void copyfile(String str,String str1) throws FileNotFoundException {//拷贝文件或文件夹try {File file=new File(str);InputStream is=null;OutputStream os=null;byte[] b;File f=new File(str1);if(!file.isDirectory()) { is=new FileInputStream(str); b=new byte[is.available()]; is.read(b); os=new FileOutputStream(str1); os.write(b); is.close(); os.close(); return;} else if(!f.exists()) f.mkdirs();File[] filename=file.listFiles();for(int i=0;i<filename.length;i++) { copyfile(filename[i].getAbsolutePath(),str1+"/"+filename[i].getName());}} catch(IOException ex) {filecopycount++;System.out.println("filecopycount:"+String.valueOf(filecopycount));if(filecopycount<=5)copyfile(str,str1);System.err.println("err:"+ex.toString());}}private static void deletefile(String delpath) throws FileNotFoundException,IOException {//删除文件或文件夹File file=new File(delpath);if(!file.isDirectory()) { file.delete(); return;} else if(file.isDirectory()) { String[] filelist=file.list(); for(int i=0;i<filelist.length;i++) { File delfile=new File(delpath+"/"+filelist[i]); if(!delfile.isDirectory()) delfile.delete(); else if(delfile.isDirectory()) deletefile(filelist[i]); } file.delete();}System.exit(0);}private static void list(String listPath) throws FileNotFoundException,IOException {//列出一个目录下的所有文件和文件夹 File file=new File(listPath); String[] filelist; if(!file.isDirectory()) { System.out.println("<dir>"+file.getName()); return; } else if(file.isDirectory()) { filelist=file.list(); for(int i=0;i<filelist.length;i++) { File listfile=new File(listPath+"/"+filelist[i]); if(!listfile.isDirectory()) System.out.println(listfile.getName()); else if(listfile.isDirectory()) list(filelist[i]); }}}} 把main方法去掉,加入package包,然后在classpath里加入该包的路径,编译之后就成为可以被其他类调用的bean了我要分 我是什么水平呢 关于import的问题 判断存到BufferReader里的email地址 I/O的哪个类可以查看磁盘剩余空间啊? 如何分割文本,就变成一个字符串数组 求一个算法,数字末尾是99的留下来,如果末尾是999、9999的就将其加3,例如:12599、99、383399、199符合条件,留下来。 文件中汉字读出会成为非法字符 关于sax对xml读写操作!!!!!!!! 请教高手如何控制打印页数 java的数组的绝对值如何写啊? 怎么把JAVA的标题栏换成XP的啊 有谁知道在南京的java程序员的薪水是多少???
static int filecopycount=1;
public mytest0() {}public static void main(String[] args) throws FileNotFoundException,IOException,Exception {//接收参数并进行相应操作
System.out.print("input:");
DataInputStream ds=new DataInputStream(System.in);
String str=ds.readLine();
if(str.equals("copy"))
copyfile(args[0],args[1]);
else if(str.equals("delete"))
deletefile(args[0]);
else if(str.equals("write"))
writefile(args[0]);
else if(str.equals("list"))
list(args[0]);
else
System.err.println("Input error!");
ds.close();
}private static void writefile(String str) throws IOException {//写入文件
OutputStream os=new FileOutputStream("xiruo_file.txt");
PrintStream ps=new PrintStream(os);
ps.println(str);
ps.close();
os.close();
System.exit(0);
}private static void copyfile(String str,String str1) throws FileNotFoundException {//拷贝文件或文件夹
try {
File file=new File(str);
InputStream is=null;
OutputStream os=null;
byte[] b;
File f=new File(str1);
if(!file.isDirectory()) {
is=new FileInputStream(str);
b=new byte[is.available()];
is.read(b);
os=new FileOutputStream(str1);
os.write(b);
is.close();
os.close();
return;
} else if(!f.exists())
f.mkdirs();
File[] filename=file.listFiles();
for(int i=0;i<filename.length;i++) {
copyfile(filename[i].getAbsolutePath(),str1+"/"+filename[i].getName());
}
} catch(IOException ex) {
filecopycount++;
System.out.println("filecopycount:"+String.valueOf(filecopycount));
if(filecopycount<=5)
copyfile(str,str1);
System.err.println("err:"+ex.toString());
}
}private static void deletefile(String delpath) throws FileNotFoundException,IOException {//删除文件或文件夹
File file=new File(delpath);
if(!file.isDirectory()) {
file.delete();
return;
} else if(file.isDirectory()) {
String[] filelist=file.list();
for(int i=0;i<filelist.length;i++) {
File delfile=new File(delpath+"/"+filelist[i]);
if(!delfile.isDirectory())
delfile.delete();
else if(delfile.isDirectory())
deletefile(filelist[i]);
}
file.delete();
}
System.exit(0);
}private static void list(String listPath) throws FileNotFoundException,IOException {//列出一个目录下的所有文件和文件夹
File file=new File(listPath);
String[] filelist;
if(!file.isDirectory()) {
System.out.println("<dir>"+file.getName());
return;
} else if(file.isDirectory()) {
filelist=file.list();
for(int i=0;i<filelist.length;i++) {
File listfile=new File(listPath+"/"+filelist[i]);
if(!listfile.isDirectory())
System.out.println(listfile.getName());
else if(listfile.isDirectory())
list(filelist[i]);
}
}
}
}
我要分