FileInputStream
및 을 사용하여 파일 작업
FileOutputStream
.
나는 Schildt의 책을 읽고 파일 작업을 하는 단계에 이르렀습니다. 이 예에서는 명령줄에서 프로그램을 실행하고 파일 이름을 입력해야 한다고 나와 있습니다. 하지만 저는 IntelliJ에서 배우고 있어요. 파일 경로를 지정하여 파일 작업을 수행하는 방법은 무엇입니까? 예는 다음과 같습니다.
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) {
int i;
FileInputStream fin;
FileOutputStream fout;
if ( args. length != 2 ) {
System . out. println ( "Usage: CopyFile From To." ) ;
return ;
}
try {
fin = new FileInputStream ( args[ 0 ] ) ;
} catch ( FileNotFoundException exc) {
System . out. println ( "Input file not found." ) ;
return ;
}
try {
fout = new FileOutputStream ( args[ 1 ] ) ;
} catch ( FileNotFoundException exc) {
System . out. println ( "Error opening output file." ) ;
try {
fin. close ( ) ;
} catch ( IOException exc2) {
System . out. println ( "Error closing input file." ) ;
}
return ;
}
try {
do {
i = fin. read ( ) ;
if ( i != - 1 ) fout. write ( i) ;
} while ( i != - 1 ) ;
} catch ( IOException exc) {
System . out. println ( "Fi;e error." ) ;
}
try {
fin. close ( ) ;
} catch ( IOException exc) {
System . out. println ( "Error closing input file." ) ;
}
try {
fout. close ( ) ;
} catch ( IOException exc) {
System . out. println ( "Error closing output file." ) ;
}
}
}
GO TO FULL VERSION