일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- Python
- post
- sqldeveloper
- algorithms
- Real MySQL
- 뇌정리
- 스터디
- Til
- 2020년 제4회 정보처리기사 필기 문제 분석
- 2020년 정보처리기사 4회
- 책리뷰
- java
- 정보처리기사
- git
- jsp
- 코드숨
- 알고리즘
- If
- 함수형 코딩
- 필기
- 회고
- 항해99
- hackerrank
- 주간회고
- 미니프로젝트
- 2020년 일정
- 서평
- Jackson
- LeetCode
- 성적프로그램
- Today
- Total
조컴퓨터
200910 OOP 15 - Java 객체지향 프로그래밍 (Ⅱ) 본문
DOS 명령어
※ 주의 사항 : 소스 코드 작성시 package 생략
1) 파일 내용 보기
형식) type 파일명
- 위의 도스 명령어처럼 실행될 수 있도록 Typing.java 파일 만들기
java Typing data.txt
------ ----------- ----------
실행 .class파일 파일명
args[0]
import java.io.*;
public class Typing {
public static void main(String[] args) {
//*
//>java Typing data.txt
String fileName=args[0];
FileReader fr=null;
BufferedReader br=null;
try {
fr=new FileReader(fileName);
br=new BufferedReader(fr);
while(true) {
String line=br.readLine();
if(line==null) {
break;
}
System.out.println(line);
}//while end
} catch (Exception e) {
System.out.println("File Not Found");
}finally {
try {
if(br!=null) {br.close();}
} catch (Exception e) {}//end
try {
if(fr!=null) {fr.close();}
} catch (Exception e) {}//end
}//end
}//main() end
}//class end
명령프롬프트
> cd java0812
> cd workspace
> cd Java
> cd src
> javac Typing.java
> java Typing c:\java0812\00\data.txt //경로 설정한 후 불러오기
결과값
무궁화 꽃이 피었습니다~
Gone With The Wind!!
soldesk.com
점심 맛있게 드세용:)
2) 파일 복사
형식) copy 원본파일명 대상파일명
- 위의 도스 명령어처럼 실행될 수 있도록 Copying.java 파일 만들기
java Copying data.txt data2.txt
------ ----------- ------------ -----------
실행 .class파일 원본파일 대상파일
args[0] args[1]
import java.io.*;
import java.io.PrintWriter;
public class Copying {
public static void main(String[] args) {
//
String indata=args[0];
String outdata=args[1];
FileReader fr=null;
FileWriter fw=null;
PrintWriter out=null;
try {
fr=new FileReader(indata);
fw=new FileWriter(outdata, false);
out=new PrintWriter(fw, true);
int data=0;
while(true) {
data=fr.read();
if(data==-1) {
break;
}
out.printf("%c", (char)data);
}//while end
System.out.println("1 copied...");
} catch (Exception e) {
System.out.println("Failed...");
}finally {
try {
if(out!=null) {out.close();}
} catch (Exception e) {}//end
try {
if(fw!=null) {fw.close();}
} catch (Exception e) {}//end
try {
if(fr!=null) {fr.close();}
} catch (Exception e) {}//end
}//end
}//main() end
}//class end
명령프롬프트
...
> javac Copying.java
> java Typing c:\java0812\00\data.txt data2.txt
결과값
1 copied...

3) 파일 삭제
형식) del 파일명
- 위의 도스 명령어처럼 실행될 수 있도록 DelTest.java 파일 만들기
java DelTest data2.txt
------ ----------- ------------
실행 .class파일 파일명
args[0]
import java.io.File;
public class DelTest {
public static void main(String[] args) {
String fileName=args[0];
try {
File file=new File(fileName);
if(file.exists()) {
if(file.delete()) {
System.out.println(fileName + "Deleted...");
}else {
System.out.println(fileName + "Not deleted");
}//if end
}else {
System.out.println("File Not Found!");
}//if end
} catch (Exception e) {
System.out.println("Failed...");
}//end
}//main() end
}//class end
명령프롬프트
...
> javac DelTest.java
> java DelTest data2.txt
결과값
data2.txtDeleted...

Properties 클래스
- 확장명을 .properties로 저장한다.
- = 또는 : 기호로 저장된 내용을 기준으로 key와 value값으로 가져올 수 있다.
- 주로 환경 구축 설정 파일 용도로 사용한다.

package oop0910;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
public class Test01_Properties {
public static void main(String[] args) {
//Properties 클래스
//-> = 또는 : 기호로 저장된 내용을 기준으로
//-> key와 value값으로 가져올 수 있다
//-> 주로 환경 구축 설정 파일 용도로 사용한다
try {
String fileName="C:/java0812/workspace/Java/src/oop0910/command.properties";
//파일 가져오기
FileInputStream fis=new FileInputStream(fileName);
Properties pr=new Properties();
//pr에 파일 담기
pr.load(fis);
//pr값을 key, value형태로 저장
HashMap<String, String> map=new HashMap<String, String>();
//
Iterator iter=pr.keySet().iterator();
while(iter.hasNext()) {
String key=(String) iter.next(); //=앞의 값 list.do <-HashMap은 순서x
String value=pr.getProperty(key);//=뒤의 값 가져오기
//System.out.println(key);
//System.out.println(value);
map.put(key, value);
}//while end
System.out.println(map.get("list.do"));
System.out.println(map.get("read.do"));
System.out.println(map.get("write.do"));
} catch (Exception e) {
System.out.println("Properties 파일 읽기 실패 : "+e);
}//end
System.out.println("END");
}//main() end
}//class end
결과값
net.bbs.List
net.bbs.Read
net.bbs.Write
END
Singleton 싱글톤
- 6.10.5 싱글톤(Singleton) p.243
- 가끔 전체 프로그램에서 단 하나의 객체만 만들도록 보장해야 하는 경우가 있다.
- Singleton 클래스를 생성한 후 테스트한다.
예제1)
package oop0910;
public class Test02_Singleton {
public static void main(String[] args) {
//6.10.5 싱글톤(Singleton)
//p.243
//가끔 전체 프로그램에서 단 하나의 객체만 만들도록 보장해야 하는 경우가 있다
//Singleton 클래스 생성한 후 테스트합니다
//에러. new 사용 불가 -> new를 사용 못한다는 이야기는 메모리 할당을 못한다는 이야기
//->생성자 함수가 private이므로 new 연산자로 객체를 생성할 수 없다
//Singleton sg=new Singleton();
//new Singleton()를 반환해 주는 메소드
Singleton obj1=Singleton.getInstance();
Singleton obj2=Singleton.getInstance();
//객체가 생성된 주소값이 동일함
System.out.println(obj1.hashCode()); //1521118594
System.out.println(obj2.hashCode()); //1521118594
//싱글톤 패턴의 객체가 생성되지 않고 각각의 메모리 할당이 이루어져서
//각 객체의 주소값이 다름
Test01_Properties tp1=new Test01_Properties();
Test01_Properties tp2=new Test01_Properties();
System.out.println(tp1.hashCode()); //1869997857
System.out.println(tp2.hashCode()); //1763847188
}//main() end
}//class end
예제2) 싱글톤 클래스 설계하기
LeeSoonShin 클래스
package oop0910;
public class LeeSoonShin {
private String name;
private int age;
private String address;
//싱글톤팬의 세상 유일의 객체 생성 후 사용할 수 있도록 한다
//1)객체를 외부에서 만들지 못하게 잠그기 -> private 속성
private LeeSoonShin() {}
private LeeSoonShin(String name, int age, String address) {
this.name=name;
this.age=age;
this.address=address;
}//end
//각 멤버 변수에 대항 getter와 setter 만들기(외부 연결 통로)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void info() {
System.out.println(name);
System.out.println(age);
System.out.println(address);
}
//2)내부에서 객체 하나를 만들어서 상수화와 Static
private final static LeeSoonShin lss=new LeeSoonShin("이순신",100,"조선");
//3) 2)에서 생성된 참조 변수(lss)에 대한 getter함수 만들기
public static LeeSoonShin getLss() {
return lss;
}//gettLss() end
}//class end
테스트 클래스
package oop0910;
public class Test {
public void disp() {
LeeSoonShin kim=LeeSoonShin.getLss();
System.out.println(kim.hashCode());
}//disp() end
}//class end
메인 클래스
package oop0910;
public class Test03_singleton {
public static void main(String[] args) {
//싱글톤 패턴(세상 유일한 객체)
//LeeSoonShin 클래스와 Test 클래스 생성 후 테스트 합니다
LeeSoonShin lee=LeeSoonShin.getLss();
System.out.println(lee.hashCode());
lee.info();
Test test=new Test();
test.disp();
}//main() end
}//class end
'자바 웹개발자 과정 > JAVA' 카테고리의 다른 글
200909 OOP 14 - Java 객체지향 프로그래밍 (Ⅱ) (0) | 2020.09.09 |
---|---|
200908 OOP 13 - Java 객체지향 프로그래밍 (Ⅱ) (0) | 2020.09.09 |
200831 ~ 0904, 0907 - 코로나 2.5단계에 의한 휴강 (0) | 2020.09.07 |
200828 OOP 12 - Java 객체지향 프로그래밍 (Ⅱ) (0) | 2020.09.07 |
200827 OOP 11 - Java 객체지향 프로그래밍(Ⅰ, Ⅱ) (0) | 2020.08.27 |