package test.cn.com.hnuc;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;public class TestIOStream {
public void copy(String srcFile,String destPath){
File srcfile = new File(srcFile);
File destfile = new File(destPath+srcfile.getName());
InputStream is = null;
OutputStream os = null;
byte[] b = new byte[280];
int length = 0;
try {
is = new FileInputStream(srcfile);
os = new FileOutputStream(destfile);
try {
while((length = is.read(b))!=-1){
os.write(b,0,length);
}
} catch (IOException e) { e.printStackTrace();
}
} catch (FileNotFoundException e) { e.printStackTrace();
} finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os!=null){
try {
os.close();
} catch (IOException e) { e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
TestIOStream test = new TestIOStream();
test.copy("src\\test\\cn\\com\\hnuc\\TestIOStream.java","check/");
}}
上述是我写的代码,编译时没问题,但运行时就报了下面的错了:
java.io.FileNotFoundException: check\TestIOStream.java (系统找不到指定的路径。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
at test.cn.com.hnuc.TestIOStream.copy(TestIOStream.java:22)
at test.cn.com.hnuc.TestIOStream.main(TestIOStream.java:58)
思考许久也没找到解决的方案,望各位帮小弟解决一下,在此先谢过了~~
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;public class TestIOStream {
public void copy(String srcFile,String destPath){
File srcfile = new File(srcFile);
File destfile = new File(destPath+srcfile.getName());
InputStream is = null;
OutputStream os = null;
byte[] b = new byte[280];
int length = 0;
try {
is = new FileInputStream(srcfile);
os = new FileOutputStream(destfile);
try {
while((length = is.read(b))!=-1){
os.write(b,0,length);
}
} catch (IOException e) { e.printStackTrace();
}
} catch (FileNotFoundException e) { e.printStackTrace();
} finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os!=null){
try {
os.close();
} catch (IOException e) { e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
TestIOStream test = new TestIOStream();
test.copy("src\\test\\cn\\com\\hnuc\\TestIOStream.java","check/");
}}
上述是我写的代码,编译时没问题,但运行时就报了下面的错了:
java.io.FileNotFoundException: check\TestIOStream.java (系统找不到指定的路径。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
at test.cn.com.hnuc.TestIOStream.copy(TestIOStream.java:22)
at test.cn.com.hnuc.TestIOStream.main(TestIOStream.java:58)
思考许久也没找到解决的方案,望各位帮小弟解决一下,在此先谢过了~~
解决方案 »
- 字符转换的问题
- 关于hashtable的简单问题
- 用C++写的二进制文件,用JAVA怎么读取?
- system.in&out 与 java.io 里边定义的输入输出有什么区别?
- 还不快点,我在找你了,关于EJB
- 求求个位好心人啦!!谢谢谢谢帮帮忙我是菜鸟!
- 唉,如何引用package呢?
- 有关工具调查的想法,大家进来说说,特别是那些用纯edit工具的~~~~
- 用jbuilder生成的得exe文件怎样才能在没有安装jdk的机器上运行??还有图标怎么换!!
- 请问 JAVA 为何进不了 catch ?
- 如何不通过key获取LinkedHashMap第一条记录?
- 并发处理的问题,大家来看看这个exception
改成test.copy("D:\\Program Files\\MyEclipse 6.0\\workspace\\Test\\src\\test\\cn\\com\\hnuc\\TestIOStream.java","C:/");
就可以在我的电脑的c盘根目录下找到TestIOStream.java这个文件了
你改成完整的路径试试
while((length = is.read(b))!=-1){
os.write(b,0,length);
}
对is进行读入后,下一次循环来到再读时,是接着原来的位置往后读,还是又从头读?毕竟fileinputstream不像randomaccessfile,人家有文件指针。但是,如真是这样,岂不。。