2015-05-18 12:06:23
0
I am facing file write issue ,Actually when i run this below code the while loop iterate infinite times.
package com.demo.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
public static void main(String[] args) {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("C:/Users/s.swain/Desktop/loginissue.txt");
out = new FileOutputStream("C:/Users/s.swain/Desktop/output.txt");
int c = in.read();
while (c != -1) {
System.out.println(c);
out.write(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Anyone can tell me how to write this file.
Thanks
Sitansu
这篇博客讲述了作者在使用Java进行文件复制时遇到的无限循环问题,代码中while循环无法正常结束。通过提供详细代码和寻求帮助,博主寻求如何正确关闭流并避免无限迭代。
368

被折叠的 条评论
为什么被折叠?



