웹개발 교육/Java
[38일] Java (51) - type
ewok
2022. 9. 20. 18:19
명령 프롬프트의 type 명령어로 파일 내용을 보자
명령 프롬프트로 컴파일 하는 명령어
javac TypeTest.java
자바 클래스를 실행하는 명령어
java 대상파일
MainTest.java를 복사하여 새로운 java 파일을 만드는 실습을 해보자
MainTest.java.
public class MainTest {
public static void main(String[] args) { //argument string
// oop0907.Test06_main.java 참조
// 주의사항 : 한글이 포함되어 있으면 컴파일 시 에러가 나는 경우가 발생한다
// 이 경우 주석을 포함한 한글을 삭제 후 테스트
// 혹은 컴파일 시 javac MainTest.java -encoding utf-8 이 방법 사용
for(int i=0; i<args.length; i++) {
System.out.println(args[i]);
}//for end
}//main() end
}//class end
CopyTest.java (복사를 수행하기 위한 파일)
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
public class CopyTest {
public static void main(String[] args) {
//
String inName = args[0];
String outName = args[1];
FileReader fr = null;
FileWriter fw = null;
PrintWriter out = null;
try {
fr = new FileReader(inName);
fw = new FileWriter(outName, false);
out = new PrintWriter(fw, true);
while(true) {
int data = fr.read();
if(data==-1) {
break;
}//if end
out.printf("%c", data);
}//while end
System.out.println("1 file copied");
} catch (Exception e) {
System.out.println("copy failure : " + e);
} finally {
try {
if(fr!=null) { fr.close(); }
} catch (Exception e) {}
try {
if(out!=null) { out.close(); }
} catch (Exception e) {}
try {
if(fw!=null) { fw.close(); }
} catch (Exception e) {}
}//end
}//main() end
}//class end
명령 프롬프트에서 복사 대상 파일과 복사가 될 파일의 이름을 입력해주고 그것을 args[0]과 args[1]로 받아 실행한다.
파일을 삭제하는 명령어
del 파일명
이번에는 파일을 삭제하는 실습을 해보자
DelTest.java
import java.io.File;
public class DelTest {
public static void main(String[] args) {
try {
File file = new File(args[0]);
if(file.exists()) {
if(file.delete()) {
System.out.println("1 file deleted");
} else {
System.out.println("deletion failure");
}//if end
} else {
System.out.println("File Not Found!!");
}//if end
} catch (Exception e) {
System.out.println("deletion failure" + e);
}//end
}//main() end
}//class end