关于java类文件读取 的问题

changewang 2004-05-01 03:42:19
我想把一个java类文件读出,然后拷贝到另外一个文件:
public void copy(String sourcePath){
int b;
try{
String filename=this.getSourceFileName(sourcePath);
copyFile=new File(copyPath+fileFullName);
FileInputStream in= new FileInputStream(sourcePath);
FileOutputStream out=new FileOutputStream(copyFile);
while((b=in.read())!=-1)
{
out.write(in.read());
}
}
catch(Exception e){
System.out.println(e.toString());
}
}
结果写入的文件中只是把远文件的偶数字符输入了,还没有换行,请问是什么问题?
在问一个,读的方法与原文件是什么类型与关系吗?比如说 .java和 .ddl?谢谢
...全文
31 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
changewang 2004-05-01
  • 打赏
  • 举报
回复
哈哈~~~多谢~~~太感激了,呵呵~~~~
sagittarius1979 2004-05-01
  • 打赏
  • 举报
回复
就是楼上说的
stars_of_leo 2004-05-01
  • 打赏
  • 举报
回复

public void copy(String sourcePath){
int b;
try{
String filename=this.getSourceFileName(sourcePath);
copyFile=new File(copyPath+fileFullName);
FileInputStream in= new FileInputStream(sourcePath);
FileOutputStream out=new FileOutputStream(copyFile);
while((b=in.read())!=-1) //你这里读了一次(奇数字符),这个没被写入
{
out.write(in.read()); //这里又读了一次文件,只写了这一次读的内容
}
}
catch(Exception e){
System.out.println(e.toString());
}
}


给你改了一下,测试过了,没问题。


public void copy(String sourcePath){
int b = 0;
byte[] data = new byte[500];

try{
String filename=this.getSourceFileName(sourcePath);
copyFile=new File(copyPath+fileFullName);

FileInputStream in= new FileInputStream(sourcePath);
DataInputStream is = new DataInputStream(in);

FileOutputStream out=new FileOutputStream(copyFile);
DataOutputStream os = new DataOutputStream(out);

while((b=is.read(data))!= -1)
{
out.write(data);
}
}
catch(Exception e){
System.out.println(e.toString());
}
}

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧