我创建了一个简单的文件读写程序。编译时,程序显示没有错误并且运行没有问题,但是当我尝试打开输出文件时,我得到一个"文件已损坏"错误,文件大小为0kb。
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
public class Extention
{
FileInputStream filein;
FileOutputStream fileout;
void asdf() throws IOException
{
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
System.out.print("/**");
System.out.print("\n");
System.out.print("* Created by Arul on 6/15/2016 *");
System.out.print("\n");
System.out.print("**/");
System.out.print("\n");
try {
System.out.print("Enter Name of the file to read : ");
filein = new FileInputStream(br.readLine());
System.out.print("Enter Name of the file to write : ");
fileout = new FileOutputStream(br.readLine());
int i;
do {
i = filein.read();
if (i == -1)
break;
fileout.write(i);
} while (i != -1);
} catch (FileNotFoundException f) {
System.out.println("Exception : File not found!");
} finally {
filein.close();
fileout.close();
}
}
public static void main(String arg[]) throws IOException
{
Extention d = new Extention();
d.asdf();
}
}
答案 0 :(得分:1)
当您说“文件已损坏”时,这是您在执行程序时遇到的Java错误,还是双击创建的文件时出错?
如果是后者,那一切都取决于你要复制的内容。如果您使用它来复制TXT文件,但是您将文件更改为MP3,您会发现,当您双击该文件时,您的媒体播放器将被选中以打开文本文件并且无法理解文本数据