新手求救,好像陷入死循环样,但是不知道错在哪了
缠绕指尖__沝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();
}
}
--------------------编程问答-------------------- byte[] bytess = new byte[1];
你这个也太小了吧,如果一个文件很大那得读很久的 --------------------编程问答-------------------- 我只存了几个数啊 --------------------编程问答-------------------- 这段代码:
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);
}
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);
}
int x = 0;
while((x=in.read(bytess))!=-1){
out1.write(bytess);
}
刚发错了 多了一行代码。。。 --------------------编程问答-------------------- int x = in.read(bytess);
while(x!=-1){
out1.write(bytess);
in.read(bytess);
}
这不是好像,而就是死循环。x永远都不会等于-1的。
顶下楼上,代码就不重复发了 --------------------编程问答-------------------- int x = in.read(bytess);
while(x!=-1){
out1.write(bytess);
in.read(bytess); //这里忘记把x赋上了吧?
} --------------------编程问答-------------------- 刚把代码跑了下,发现out.txt一直在增大,想到必然是在写文件时死循环了,看了下代码,果然。大侠们都发帖找出了错误,在此也学习一下。楼主注意控制代码中的开关
补充:Java , Java SE