刚写的,已经测试!!
import java.io.*;
public class Demo
{
private String neirong;
public Demo(){
}
//打开文件
public void barkup(){
//打开Demo.txt文件
File f=new File("E:\\Demo");
File f1=new File(f,"Demo.txt");
//生成的备份文件
File f2=new File(f,"Demo.txt.bak");
try
{
f2.createNewFile();
BufferedReader br=new BufferedReader(new FileReader(f1));
PrintWriter pw=new PrintWriter(new FileWriter(f2),true);
neirong=br.readLine();
while(neirong!=null)
{
pw.write(neirong);
pw.write("\n");
neirong=br.readLine();
}
pw.close();
br.close();
System.out.println("备份文件成功"); }
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String arge[]){
Demo d=new Demo();
d.barkup();
}
}请揭帖吧!
import java.io.*;
public class Demo
{
private String neirong;
public Demo(){
}
//打开文件
public void barkup(){
//打开Demo.txt文件
File f=new File("E:\\Demo");
File f1=new File(f,"Demo.txt");
//生成的备份文件
File f2=new File(f,"Demo.txt.bak");
try
{
f2.createNewFile();
BufferedReader br=new BufferedReader(new FileReader(f1));
PrintWriter pw=new PrintWriter(new FileWriter(f2),true);
neirong=br.readLine();
while(neirong!=null)
{
pw.write(neirong);
pw.write("\n");
neirong=br.readLine();
}
pw.close();
br.close();
System.out.println("备份文件成功"); }
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String arge[]){
Demo d=new Demo();
d.barkup();
}
}请揭帖吧!
import java.io.*;
import java.util.*;
/**
目录文件拷贝(jdk)
@version v1.0 2001-9-27
@auther <a href="mailto:[email protected]">Qixy</a>
*/public class Copy {
/**
Null & simple construct .
*/
public Copy()
{
//void to construct for a class ! }
/**
Application main 使用方法
<p> usage:java Copy [-x(包含子目录)] s_dirorfile(源文件夹/文件) t_dirorfile(目标文件夹/文件)
*/ public static void usage()
{
System.out.println("usage:java Copy -x [s_dirorfile] [t_dirorfile]");
System.exit(0);
return;
}
/**
Application main entrance
@see usage
*/
public static void main(String[] args) throws IOException { /** To be corrected ... */
boolean bx=false;
Copy c=new Copy();
//test ...
File ff=new File("e:\\eeeee\\dfe\\s.x");
System.out.println("ff.getAbsolutePath()="+ff.getAbsolutePath());
System.out.println("ff.getName()="+ff.getName());
System.out.println("ff.getPath()="+ff.getPath());
DayTime dt=new DayTime();
for(int ii=0;ii<200;ii++)
{ dt.advance(1); File ww=new File("Q:\\"+dt.getDateString2()+"\\s.x");
File w2=new File("Q:\\"+dt.getDateString2()+"\\misc\\s.x");
File w3=new File("Q:\\"+dt.getDateString2()+"\\source\\s.x");
File w4=new File("Q:\\"+dt.getDateString2()+"\\doc\\s.x");
File w5=new File("Q:\\"+dt.getDateString2()+"\\javadoc\\s.x"); w2.getParentFile().mkdirs();
w3.getParentFile().mkdirs();
w4.getParentFile().mkdirs();
w5.getParentFile().mkdirs();
c.copy("e:\\readme.doc","Q:\\"+dt.getDateString2()+"\\readme.doc");
System.out.print("OK?");
}
try{ System.in.read() ;}catch(Exception w){}
if (args.length<1)
{
usage(); }
if (args.length==2)
{ c.copy(args[0],args[1]);
//c.copy("e:\\Copy.java","e:\\xxx\\s.txt");
try{ System.in.read() ;}catch(Exception w){}
return;
} if (args.length>=3 && args[0].equals("-x"))
c.xcopy(args[1],args[2]);
else
usage();
// c.copy(args[0],args[1]); try{ System.in.read() ;}catch(Exception w){}
} /**
<big>简</big>单拷贝文件 不包含子文件夹及其文件!
<p> 注:1。 如果源为文件夹且目的是文件夹则目的省略掉文件名
<p> 2。 当源文件或文件夹不存在时或目标文件或文件夹不存在时抛出IOException
@param s 源文件/文件夹
@param t 目的文件/文件夹
@throws 当源文件或文件夹不存在时或目标文件或文件夹不存在时抛出IOException
*/
public void copy(String s,String t) throws IOException
{
init(s,t); if(bs_file )
{
copyfile(fs,ft);
return;
} //both directory
}
/**
<big>遍</big>历拷贝!
<P> 注:1。 如果源为文件夹且目的是文件夹则目的省略掉文件名
<p> 2。 当源文件或文件夹不存在时或目标文件或文件夹不存在时抛出IOException
@param s 源文件/文件夹
@param t 目的文件/文件夹
@throws 当源文件或文件夹不存在时或目标文件或文件夹不存在时抛出IOException
*/
public void xcopy(String s,String t) throws IOException
{
init(s,t);
if(bs_file )
copyfile(fs,ft);
//both directory
}
/** init*/
private void init(String s,String t)
{
this.fs=new File(s);
this.ft=new File(t); if(!fs.exists()){
new IOException("Err:file no exists!");
}
//判断是否以 File.pathSeparator 结尾断定
// ft.getAbsolutePath() ft.getParentFile().mkdirs();
// System.out.println("failed");
// new IOException("Err:file/directory create failure!");
this.bt_file=ft.isFile()?true:false;
this.bs_file=fs.isFile()?true:false;
// System.out.println(bt_file);
/*
if ( !ft.exists())
{
ft.createNewFile();
} if (bt_file)
if(!ft.getParentFile().mkdirs()) System.out.println("failed");
else
if(!ft.mkdirs())
new IOException("Err:file/directory create failure!");
else
if (bs_file) ft=new File(ft,fs.getName());
*/
// ft=new File(ft,fs.getName());
if(bt_file && !bs_file)
ft=ft.getParentFile();
} /**
File to File copy
@param fas source file
@param fat target file
*/
public void copyfile(File fas,File fat)throws IOException
{
FileReader in = new FileReader(fas);
FileWriter out = new FileWriter(fat);
int c; while ((c = in.read()) != -1)
out.write(c); in.close();
out.close(); } /** 源文件对象 */
private File fs;
/** 目的文件对象 */
private File ft;
/** 源文件对象是否为文件 */
private boolean bs_file;
/** 目的文件对象是否为文件 */
private boolean bt_file;}
Runtime.getRuntime().exec("c:\\winnt\\system32\\cmd.exe /c copy d:\\1.txt d:\\2.txt");
全是高手教过的代码
你的代码运行时显示“系统找不到指定的路径”??
还有那个demo的目录