본문 바로가기

프로그램/Java64

[Java] 시스템 command 실행하기, Runtime.getRuntime.exec() 자바프로그램이 돌아가는 시스템(윈도우즈, 리눅스, 유닉스, 등)의 명령어를 java에서 실행시킨 후에 그 결과를 방아올 수 있다. Runtime.getRuntime().exec("시스템 명령어"); 시스템 명령어를 실행하고 그 결과를 받아서 화면에 표시하는 예제이다. try{ Process p = Runtime.getRuntime().exec("시스템 명령어"); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while((line = br.readLine()) != null){ System.out.println(line); } }catch(Exception e){ Sy.. 2013. 12. 17.
[Java] UUID UUID.randomUUID().toString(); * jdk1.5이후부터 사용가능 2013. 11. 15.
[Java] String -> byte, byte -> String 변환 String str1 = “Hello World!”;// 변수 str1의 바이트 값// 72101108108111328711111410810033bytes[] buffers = str1.getBytes(); …// 바이트 배열 자체의 문자열 값// [B@ca0b6String buffersArrayString = buffers.toString();…// 바이트 배열을 문자열로 변환한 값// Hello World!String str2 = new String(buffers); 2013. 11. 15.
[Java] java.text.SimpleDateFormat SimpleDateFormat을 이용하면 현재 날짜 및 시간을 원하는 포맷으로 가져올 수 있다.…Format 기호의미보기G연대(BC, AD)ADy년도2009M월(1~12월 또는 1월~12월)10또는 10월, OCTw년의 몇 번째 주(1~53)50W월의 몇 번째 주(1~5)4D년의 몇 번재 일(1~366)100d월의 몇 번째 일(1~31)15F월의 몇 번째 요일(1~5)1E요일월a오전/오후(AM, PM)PMH시간(0~23) 20k시간(1~24) 12K시간(0~11) 10h시간(1~12) 11m분(0~59) 35s초(0~59) 55S천분의 1초(0~999) 253zTime zone(General time zone) GMT+9:00ZTime zone(RFC 822 time zone) +0900‘excape문자.. 2013. 11. 15.
[Java] java.util.ResourceBundle java에서 사용되는 properties 파일의 내용을 읽어올수 있다. ResourceBundle클래스는 국제화를 지원하기 Locale 파일 규칙을 이용해서 파일명을 사용할 수 있다.properties 파일은 getBundle 메소드에 의해 객체로 전환이 된다. 따라서,크랠스 파일과 같이 위치를 추적할 수 있다. 만약 “server.properties” 라는 파일이 classes 폴더 밑에 conf 라는 폴더 아래 있다면 “conf.server.properties” 로 해서 찾을 수 있다.… 위치 : web-inf/classes/conf/server.properties ResourceBundle bundle = ResourceBundle.getBundle(“conf.server.properties”); .. 2013. 11. 15.
[Java] properties 파일을 읽어 들이고 저장하기 java.util.Properties 클래스를 사용하면 쉽게 키와 값으로 이루어진 properties 파일을 읽어들이고 작성할 수 있다.…1. properties 파일 작성 Properties prop = new Properties(); prop.setProperty(“SERVER_IP”, “127.0.0.1″); prop.setProperty(“SERVER_PORT”, “5000″); try{ OutputStream stream = new FileOutputStream(“파일명”); prop.store(stream, “Server Info”); stream.close(); } catch(IOException ex){ ex.printStackTrace(); }…2. properties 파일 읽어들이기 P.. 2013. 11. 15.
[Java] JDom – SAXBuilder를 이용한 Document 객체 생성 XML 소스(소켓, 파일, 입력, 등)을 입력 받아서 각 항목에 접근하기 위해서는 먼저 Document 객체를 생성해야 한다. 다음은 다양한 입력 소스로 부터 Document 객체를 생성하는 방법이다.…// 바이트배열에 담김 데이터를 이용해서 XML Document 객체를 생성한다. byte[] responseBody = null;if (responseBody != null) { SAXBuilder saxBuilder = new SAXBuilder(); Document doc = saxBuilder.build(new ByteArrayInputStream(new String(responseBody).getBytes())); root = doc.getRootElement(); resultMode = root.. 2013. 11. 15.