新手求救,好像陷入死循环样,但是不知道错在哪了

lzc1989925 2012-11-01 09:19:11
缠绕指尖__
沝2package IO;
import java.io.*;
public class Io {
public static void main(String[] args) throws Exception{
File file = new File("e:/java/src");
file.mkdirs();
File file1 = new File("e:/java/src/in.txt");
file1.createNewFile();
byte[] bytes = new byte[]{97,98,99,100};
FileOutputStream out = new FileOutputStream(file1);
out.write(bytes);
byte[] bytess = new byte[1];
FileInputStream in =new FileInputStream(file1);
File file2 = new File("e:/java/src/out.txt");
file2.createNewFile();
FileOutputStream out1 = new FileOutputStream(file2);
int x = in.read(bytess);
while(x!=-1){

out1.write(bytess);
in.read(bytess);
}
out.close();
out1.close();
in.close();



}
}
...全文
252 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Iam_magicHu 2012-11-09
  • 打赏
  • 举报
回复
刚把代码跑了下,发现out.txt一直在增大,想到必然是在写文件时死循环了,看了下代码,果然。大侠们都发帖找出了错误,在此也学习一下。楼主注意控制代码中的开关
水补水 2012-11-09
  • 打赏
  • 举报
回复
int x = in.read(bytess); while(x!=-1){ out1.write(bytess); in.read(bytess); //这里忘记把x赋上了吧? }
失落夏天 2012-11-08
  • 打赏
  • 举报
回复
int x = in.read(bytess); while(x!=-1){ out1.write(bytess); in.read(bytess); } 这不是好像,而就是死循环。x永远都不会等于-1的。 顶下楼上,代码就不重复发了
油焖菠菜 2012-11-08
  • 打赏
  • 举报
回复

int x = 0;
 while((x=in.read(bytess))!=-1){
 out1.write(bytess);
 }
刚发错了 多了一行代码。。。
油焖菠菜 2012-11-08
  • 打赏
  • 举报
回复
int x = in.read(bytess);
 while(x!=-1){

 out1.write(bytess);
 in.read(bytess);
 }
改成

int x = 0;
 while((x=in.read(bytess))!=-1){
 out1.write(bytess);
 in.read(bytess);
 }


LS1firesoar 2012-11-01
  • 打赏
  • 举报
回复
楼上正解
nmyangym 2012-11-01
  • 打赏
  • 举报
回复
这段代码:

int x = in.read(bytess);
while(x!=-1)
{
out1.write(bytess);
in.read(bytess);
}

改一下:

int x = in.read(bytess);
while(x!=-1)
{
out1.write(bytess);
x=in.read(bytess);
}
lzc1989925 2012-11-01
  • 打赏
  • 举报
回复
我只存了几个数啊
dengsilinming 2012-11-01
  • 打赏
  • 举报
回复
byte[] bytess = new byte[1];
你这个也太小了吧,如果一个文件很大那得读很久的

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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