Java (16) 썸네일형 리스트형 [exam 17] 네트워크 정보 가져오기 public class Sample1 { public static void main(String[] args) throws UnknownHostException { byte[] addr = new byte[] {127, 0, 0, 1}; InetAddress inet1 = InetAddress.getByAddress(addr); System.out.println(Arrays.toString(inet1.getAddress())); //LocalHost 장치의 주소(Loopback :다른외부장치와는 연결불가) 정보 가져오기 InetAddress inet2 = InetAddress.getByName("localhost"); //띄어쓰기하면 오류 System.out.println(Arrays.toString(i.. [exam16] 스레드 상속, Runnable 인터페이스 스레드 상속 class TimeCounter extends Thread{ private int count = 0; private String name; public TimeCounter(int count, String name) { this.count = count; this.name = name; } public void countStart() { while(count > 0) { System.out.println(this.name + " 카운트 - " + this.count); this.pause(1000); this.count--; } } //쓰레드 상속 시 반드시 런 구현 @Override public void run() { //쓰레드로 작업할 코드 작성 this.countStart(); } pr.. [210831] [exam15] 문자 기반 파일 입출력 public class Sample3 { public static void main(String[] args) { // 문자 기반 파일 입출력 // 문자가 저장된 텍스트 파일에 대한 읽기 쓰기 작업에 적합 (이미지 X) try {//여기선 String(문자열)으로 FileWriter fw = new FileWriter("./filewriter.txt"); fw.write("파일 쓰기 작업을 진행합니다."); //write도 문자열로 선택해서 바로 적음 fw.close(); } catch (IOException e) { System.out.println("파일 출력 스트림 생성에 문제가 발생하였습니다."); } try { FileReader fr = new FileReader("./filewriter.tx.. [210831] [exam15] 바이트 기반 파일 입출력 //버퍼드쓰고 싶다면 얘는 bufferedOutputStream, InputStream //문자기반일 때가 bufferWriter, -Reader public class Sample2 { //이미지, 비디오, 텍스트 파일을 읽고 쓰기 위해 사용 //바이트 기반 파일 쓰기 작업 public static void main(String[] args) { try {//선언하면 자동 트라이 캐치 //FileOutputStream fos = new FileOutputStream("./fileoutput.txt"); File f = new File("./fileoutput.txt");//파일객체 생성해서 쓰는 방법 FileOutputStream fos = new FileOutputStream(f, true); //기.. [exam15] try ~ catch문 public class Sample1 { public static void main(String[] args) { /* * 상대 경로 : ./ -> 현재 (프로그램이 동작하는) 위치를 의미한다. *../-> 현재 위치의 상위 디렉터리를 의미한다. *./ , ../ 생략하고 바로 디렉터리명/파일명을 사용 * 절대 경로 : Windows -> C:\ 부터 모든 경로를 작성하는 것 * MacOs/Linux -> / 부터 모든 경로를 작성하는 것 */ File f = new File("config/");//경로에 파일 생성(상대경로 생략한 것) System.out.println(f.getAbsolutePath());//전체 경로 메서드 출력 // 경로.exists() -> 경로에 file/directory(fo.. [exam12] 문자열 (String) 관련 메서드 public class Sample1 { public static void main(String[] args) { // 문자열 관련 메서드 String str = "동일한 문자열인지 비교"; boolean resBool = str.equals("동일한 문자열인지 비교"); System.out.println(resBool); str = "문자열에서 문자를 추출하기 위한 메서드"; char resChar = str.charAt(0); System.out.println(resChar); str = "문자열의 길이를 구하는 메서드"; int resInt = str.length(); System.out.println(resInt); str = "문자열에 특정 문자열이 포함되어 있는지 확인하는 메서드"; resBo.. [exam14] Map public class Sample4 { public static void main(String[] args) { /* 컬랙션 * Map : 키와 값의 쌍으로 데이터를 관리하는 구조 * 키의 경우 중복을 허용하지 않음 (동일한 키가 있으면 덮어씀) * 값은 중복을 허용함 * 키와 값의 쌍을 Entry 라고 한다. */ //동일한 키가 있고 값은 다르다 그러면 이 값이 덮어씌워짐 키와 값은 한 쌍이므로 K1 키, value1 값, //에추가적으로 k1 키, value2, 를 하면 중복은 안되고 값이 덮어씌워짐 value1은 결국 안 써지는것 Map hMap = new HashMap(); //제네릭엔 2가지 타입 String은 키(객체)의 자료형 Integer는 값(객체)의 자료형 //추가 hMap.put(.. [exam14] Set public class Sample3 { public static void main(String[] args) { // TODO Auto-generated method stub Set hSet = new HashSet();//어레이리스트와 거의동일 //추가 hSet.add(10); hSet.add(20);hSet.add(30); System.out.println(hSet);//순서달라져 //중복 안됨 hSet.add(10); System.out.println(hSet); //검색 boolean resBool = hSet.contains(10); System.out.println(resBool); resBool = hSet.contains(40); System.out.println(resBool); //갯.. 이전 1 2 다음