간단한 파일 읽기 및 쓰기 프로그램을 만들었습니다. 컴파일 할 때 프로그램은 아무런 오류도 보이지 않고 문제없이 실행되지만 출력 파일을 열려고하면 "파일이 손상되었습니다"라는 오류가 발생하고 파일의 크기는 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();
}
}
내 컴퓨터에서 제대로 작동합니다. – Hackerdarshi
모든 범위의 확장을 읽고 쓸 수 있습니까? 내 PC에서 .java를 포함하여 확장이 이루어지지 않고 있습니다. –
'.txt'로 시도했습니다. 이진 파일을 읽으려면 문자 지향 스트림이 아닌 바이트 지향 스트림을 사용하여 이진 파일을 읽어야합니다. – Hackerdarshi