import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.io.*;
class TestCopy
{
public void copy(File source,File target) throws Exception
{
File tarpath = new File(target,source.getPath());
if(source.isDirectory())
{
File [] fi = source.listFiles();
for(int i=0;i<fi.length;i++)
{
copy(fi[i],tarpath);
}}
else{
InputStream is = new FileInputStream(source);
OutputStream os = new FileOutputStream(tarpath);
byte [] buf = new byte[1024];
int len;
while((len = is.read(buf))!=-1)
{
os.write(buf,0,len);
}
is.close();
os.close();
}
}
public static void main (String[] args) {
//new TestCopy().copy("G:\\1.txt","G:\\2.txt");
try
{
File file1 = new File("F:\\上网帐号.txt");
File file2 = new File("F:\\2.txt");
new TestCopy().copy(file1,file2);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}}
}
为什么编译的时候没报错,运行的时候说 ? G:\2.txt\G:\1.txt (系统找不到指定的路径。) 会报这样的错?
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.io.*;
class TestCopy
{
public void copy(File source,File target) throws Exception
{
File tarpath = new File(target,source.getPath());
if(source.isDirectory())
{
File [] fi = source.listFiles();
for(int i=0;i<fi.length;i++)
{
copy(fi[i],tarpath);
}}
else{
InputStream is = new FileInputStream(source);
OutputStream os = new FileOutputStream(tarpath);
byte [] buf = new byte[1024];
int len;
while((len = is.read(buf))!=-1)
{
os.write(buf,0,len);
}
is.close();
os.close();
}
}
public static void main (String[] args) {
//new TestCopy().copy("G:\\1.txt","G:\\2.txt");
try
{
File file1 = new File("F:\\上网帐号.txt");
File file2 = new File("F:\\2.txt");
new TestCopy().copy(file1,file2);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}}
}
为什么编译的时候没报错,运行的时候说 ? G:\2.txt\G:\1.txt (系统找不到指定的路径。) 会报这样的错?
是报这个错,上面的那个打错了
有问题吧。
看看api,你得两个参数有问题。
@param parent The parent abstract pathname
@param child The child pathname string