整个文件夹都可以import java.io.*;
import java.util.*;public class fileCopy
{
public static void main(String[] args)
{
fileCopy test = new fileCopy();
test.copyTo(new StringBuffer("F:/AppServer/Tomcat 5.0/conf"),
new StringBuffer("f:/DOCCOPY"));
} public void copyTo (StringBuffer s_path, StringBuffer t_path)
{
File s_file = new File(s_path.toString());
File t_file = new File(t_path.toString());
if (!t_file.exists())
t_file.mkdir();
File[] files = s_file.listFiles();
for (int i = 0; i < files.length; i++)
{
System.out.println(files[i].getName());
if (files[i].isDirectory())
{
StringBuffer s_subPath = new StringBuffer(s_path.toString());
StringBuffer t_subPath = new StringBuffer(t_path.toString());
t_subPath.append("/");
s_subPath.append("/");
t_subPath.append(files[i].getName());
s_subPath.append(files[i].getName());
File subDir = new File(t_subPath.toString());
if (subDir.mkdir())
copyTo(s_subPath, t_subPath);
}
else if (files[i].isFile())
{
File t_subFile = new File(t_path.toString() + "/" + files[i].getName());
File s_subFile = new File(s_path.toString() + "/" + files[i].getName());
try
{
FileInputStream fin = new FileInputStream(files[i]);
FileOutputStream fout = new FileOutputStream(t_subFile);
int length;
while ((length = fin.read()) != -1)
{
byte[] buffer = new byte[1024];
fin.read(buffer, 0, length);
fout.write(buffer, 0, length);
}
}
catch (IOException ioe)
{
}
catch (Exception e)
{
} }
}
}
}
import java.util.*;public class fileCopy
{
public static void main(String[] args)
{
fileCopy test = new fileCopy();
test.copyTo(new StringBuffer("F:/AppServer/Tomcat 5.0/conf"),
new StringBuffer("f:/DOCCOPY"));
} public void copyTo (StringBuffer s_path, StringBuffer t_path)
{
File s_file = new File(s_path.toString());
File t_file = new File(t_path.toString());
if (!t_file.exists())
t_file.mkdir();
File[] files = s_file.listFiles();
for (int i = 0; i < files.length; i++)
{
System.out.println(files[i].getName());
if (files[i].isDirectory())
{
StringBuffer s_subPath = new StringBuffer(s_path.toString());
StringBuffer t_subPath = new StringBuffer(t_path.toString());
t_subPath.append("/");
s_subPath.append("/");
t_subPath.append(files[i].getName());
s_subPath.append(files[i].getName());
File subDir = new File(t_subPath.toString());
if (subDir.mkdir())
copyTo(s_subPath, t_subPath);
}
else if (files[i].isFile())
{
File t_subFile = new File(t_path.toString() + "/" + files[i].getName());
File s_subFile = new File(s_path.toString() + "/" + files[i].getName());
try
{
FileInputStream fin = new FileInputStream(files[i]);
FileOutputStream fout = new FileOutputStream(t_subFile);
int length;
while ((length = fin.read()) != -1)
{
byte[] buffer = new byte[1024];
fin.read(buffer, 0, length);
fout.write(buffer, 0, length);
}
}
catch (IOException ioe)
{
}
catch (Exception e)
{
} }
}
}
}
一个时FileOutputStream("E:\bbb.txt")
这样就OK拉。
你能说得详细一点吗??????真的很急......谢谢各位.......
小弟在线等..................
{
private FileWriter fw;
private BufferedWriter bw;
String filename;
StreamOper()
{
}
public void streamWrite(String filename)
{
this.filename=filename;
try
{
fw=new FileWriter(filenametrue);
bw=new BufferedWriter(fw);
bw.newLine();
}
catch(IOException ex)
{
ex.printStackTrace();
System.err.println("Stream wrong:"+ex.getMessage());
} }
public void streamClose()
{
try
{
fw.close();
bw.close();
}
catch(IOException exp)
{
exp.printStackTrace();
System.err.println("Stream wrong:"+exp.getMessage());
}
}
}
类似这样运行的.......java CreatFile E:\aaa.txt E:\bbb.txt这个应该不是很难,只缘我很菜..............
public class CreatFile
{
public static void main(String args[])
{
try{
FileInputStream filein=new FileInputStream(args[0]);
FileOutputStream fileout= new FileOutputStream(args[1]);
while(filein.available()>0)
{ fileout.write(filein.read());
}
}catch(Exception e)
{System.out.println(e.toString());}}
}
编译可以通过
运行时要注意不是: java CreatFile E:\aaa.txt E:\bbb.txt
而是: java CreatFile E:\\aaa.txt E:\\bbb.txt
在字符串中两个‘\’才算一个‘\’
由于是例子没有注意性能.txt文件应该没问题,要是大文件就要缓冲区了。
我不是高手,高手不回答这样的问题。另外给我点分,我快三个库头了,:).
import java.util.*;public class Copyfile
{
public static void main(String[] args)
{
if(args.length==2)
{
String sourceName=args[0];
String targetName=args[1];
File sfile=new File(sourceName);
if(sfile.isFile())
{
try{
FileInputStream in=new FileInputStream(sfile);
FileOutputStream out=new FileOutputStream(targetName);
int length;
while((length=in.read())!=-1)
{
byte[] buf=new byte[1024];
in.read(buf,0,length);
out.write(buf,0,length);
}
}catch(IOException e)
{
System.out.println(e.toString());
}
}
else
{
System.out.println("Source file doesn't exist!");
}
}
else
{
System.out.println("命格式不对:java Copyfile sfiledir tfiledir");
}
}
}