JAVA .properties 설정하고 불러오기

각종설정을 한곳에서 해주고 싶은경우가 있다.
이럴경우에 properties를 만들어서 불러와서 쓸수 있다.


[code]#reikop.properties
#이렇게 주석을 넣을수 있다.
id = reikop
pass = 1234
[/code]

[code]//Config.java
static ResourceBundle res = ResourceBundle.getBundle("reikop");
 public static String getConfig(String value){
  return res.getString(value);
 }
[/code]

[code]//Test페이지
System.out.println(Config.getConfig(id));
System.out.println(Config.getConfig(pass));

//reikop
//1234
[/code]

Posted by reiKop

2008/11/15 11:08 2008/11/15 11:08
, ,
Response
No Trackback , No Comment
RSS :
http://reikop.raony.net/blog/rss/response/54

OPENAPI사용시 보안정책 우회법

우선 이미지 우회법.
url을 get방식으로 받아서 그대로 이미지를 뿌려준다.
[code]<%@page import="java.io.OutputStream"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.net.URLConnection"%>
<%@page import="java.net.URL"%>
<%@ page language="java" contentType="image/jpeg"%>
<%
    URL url = new URL((String) request.getParameter("url"));
    URLConnection connection;
    InputStream is;
    InputStreamReader isr;
    BufferedReader br;
    OutputStream os = response.getOutputStream();
    connection = url.openConnection();
    is = connection.getInputStream();
    isr = new InputStreamReader(is);
    br = new BufferedReader(isr);
    int buf = 0;
    while ((buf = br.read()) != -1) {
        os.write(buf);
    }
    out.clear();
    out = pageContext.pushBody();
%>
[/code]

데이터를 받아오는 XML도 우회시켜서 받아온다.
[code]<%@page import="java.util.Enumeration"%><%@page import="java.net.MalformedURLException"%>
<%@page import="java.io.UnsupportedEncodingException"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="java.net.HttpURLConnection"%>
<%@page import="java.net.URL"%><%@page import="java.io.PrintWriter"%>
<%
    request.setCharacterEncoding("UTF-8");
    PrintWriter outs = response.getWriter();
    String OPEN_API_URL = "http://openapi.naver.com/search?";
    StringBuffer key = new StringBuffer(OPEN_API_URL);
    Enumeration e = request.getParameterNames();
    while(e.hasMoreElements()){
        String param = (String)e.nextElement();
        key.append(param+"="+request.getParameter(param)+"&");
    }
    URL requestUrl = new URL(key.toString());
    HttpURLConnection httpConn = (HttpURLConnection) requestUrl.openConnection();
    httpConn.setRequestMethod("GET");
    BufferedInputStream bis = new BufferedInputStream(httpConn.getInputStream());
    int c;
    while ((c = bis.read()) != -1) {
        outs.write(c);
    }
%>[/code]

Posted by reiKop

2008/10/30 22:31 2008/10/30 22:31
, , , ,
Response
No Trackback , No Comment
RSS :
http://reikop.raony.net/blog/rss/response/46

파일 저장

사용자 삽입 이미지

[code]

package Exam;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class NotePad implements ActionListener{
        Frame f = new Frame("메모장 v0.01");
        Panel p = new Panel();
        Panel p1 = new Panel();
        Button btn1 = new Button("저장");
        Button btn2 = new Button("지우기");
        Button btn3 = new Button("종료");
        TextArea t = new TextArea("",25,25,TextArea.SCROLLBARS_VERTICAL_ONLY);
        void create(){
                btn1.addActionListener(this);
                btn2.addActionListener(this);
                btn3.addActionListener(this);


                p.add(btn1);
                p.add(btn2);
                p.add(btn3);
                p1.add(t);
                f.add(p,BorderLayout.SOUTH);
                f.add(p1,BorderLayout.CENTER);



                f.setVisible(true);
                f.setBounds(500, 500, 200, 300);
        }
        void loader(){
                FileReader fr = null;
                try{
                        fr = new FileReader("c:\\text.txt");
                        char[] cbuf = new char[100];
                        fr.read(cbuf);
                        String str = new String(cbuf);
                        t.setText(str);

                }catch(IOException ioe){
                        try{
                                FileWriter fw = new FileWriter("c:\\text.txt");
                                fw.write("");

                        }catch (IOException io3){
                                io3.printStackTrace();
                        }
                        ioe.printStackTrace();

                }finally{
                        try{
                                if(fr !=null)fr.close();

                        }catch(IOException ioe){
                                ioe.printStackTrace();
                        }
                }


        }
        public static void main(String[] args){
                NotePad n =new NotePad();
                n.loader();
                n.create();
        }
        public void actionPerformed(ActionEvent e) {
                String s = e.getActionCommand();
                FileWriter fw = null;
                if(s.equals("저장")){
                        String a = t.getText();
                        try{
                                fw = new FileWriter("c:\\text.txt");
                                fw.write(a);

                        }catch (IOException ioe){
                                ioe.printStackTrace();
                        }finally{
                                try{
                                        if (fw !=null)fw.close();
                                }catch (IOException ioe){
                                        ioe.printStackTrace();
                                }
                        }

                }
                if(s.equals("지우기")){
                        t.setText(null);
                }
                if(s.equals("종료")){
                        System.exit(0);
                }

        }
}
[/code]

Posted by reiKop

2007/11/21 13:24 2007/11/21 13:24
, , ,
Response
No Trackback , No Comment
RSS :
http://reikop.raony.net/blog/rss/response/15

String클래스를 사용한 개념잡기

[code]
import java.util.*;





public class suk {


        //indexOf 관련예제

        static String userInput=null;



        static void strIndexOf(){
                Scanner in2 = new Scanner(System.in);
                System.out.println("당신이 입력한 문자열은\n"+userInput+"입니다.");
                System.out.println("위치를 검색할 문자를 입력하세요. 검색한 문자의 위치가 반환됩니다.");
                String bigyo=in2.next();
                int pos = userInput.indexOf(bigyo);
                System.out.println(pos);

        }


        //StringBuffer 관련예제2 (append delete insert)


        static void strSB(){
                Scanner in3 = new Scanner(System.in);
                StringBuffer sb=new StringBuffer(userInput);







                System.out.println("당신이 입력한 문자열은\n"+userInput+"입니다.");



                int var=sb.capacity();

                int var2=sb.length();



                System.out.println("문자열의 용량은 "+var+"이며 길이는"+var2+"입니다.");


                System.out.println("뒤에추가하고싶은 문자열을 입력하세요");

                String app=in3.next();
                sb.append(app);
                userInput=sb.toString();
                System.out.println(userInput);



                System.out.println("지우고싶은 문자열의 범위를 입력하세요 (ex=1enter, 2enter)");
                int del=in3.nextInt();
                int del2=in3.nextInt();

                sb.delete(del, del2);
                userInput = sb.toString();
                System.out.println(userInput);


                System.out.println("끼워넣을 문자열을 입력하세요");
                String var3=in3.next();
                System.out.println("끼워넣을 위치의 번호를 입력하세요");
                int var4 = in3.nextInt();
                sb.insert(var4,var3);
                userInput=sb.toString();
                System.out.println(userInput);



        }

        // stringToken 예제

        static void strTk(){

                StringTokenizer st = new StringTokenizer(userInput,".");
                while (st.hasMoreTokens()) {
                        System.out.println(st.nextToken());
                }
        }


        public static void main(String args[]){
                Scanner in4 = new Scanner(System.in);
                Scanner in5= new Scanner(System.in);
                System.out.println("변경할 문자열을 입력하세요 (문자열사이에.를 입력)");
                StringBuffer sb=new StringBuffer(in4.next());

                userInput=sb.toString();





                System.out.println("실행할 메소드를 선택하세요. 1. indexOf  2.StringBuffer  3.StringTokenizer");
                int a = in5.nextInt();
                switch(a){
                        case 1:
                        strIndexOf();
                        break;
                        case 2:
                        strSB();
                        break;
                        case 3:
                        strTk();
                        break;


                }
        }
}
[/code]

Posted by reiKop

2007/11/13 21:31 2007/11/13 21:31
, ,
Response
No Trackback , No Comment
RSS :
http://reikop.raony.net/blog/rss/response/10

가위 바위 보 프로그램

[code]
import java.util.*;


class If {


        static int result(String set){
                int b = 0;
                if(set.equals("가위")){
                        b=0;
                }
                if(set.equals("바위")){
                        b=1;
                }
                if(set.equals"보"){
                        b= 2;
                }
                return b;
        }

        static String out(double a){
                String b = null;
                if(a==0.0){
                        b="가위";
                }
                if(a==1.0){
                        b="바위";
                }
                if(a==2.0){
                        b="보";
                }
                return b;
        }


        public static void main(String args[]){
                Scanner in = new Scanner(System.in);


                String userInput=null;


                int win=0,lose=0,draw=0;
                //승패 결과 누적


                while(true){
                        System.out.println("가위,바위,보중에 입력하세요");


                        double random=Math.floor(Math.random()*3);
                        //0.0~3.0난수를 만들어냄 3이 나올확률은 극히 드물기떄문에 취급을 안함.




                        userInput=in.next();






                        if(random==result(userInput)){
                                System.out.println("컴퓨터는 "+out(random)+" 을냈고 결과는 비겼습니다.");
                                System.out.println("현재성적은 "+win+"승 "+lose+"패 "+draw+"번 비겼습니다.");
                                draw++;
                        }


                        if((int)random-result(userInput)==1||(int)random-result(userInput)==-2){
                                System.out.println("컴퓨터는 "+out(random)+" 을냈고 결과는 졌습니다.");
                                System.out.println("현재성적은 "+win+"승 "+lose+"패 "+draw+"번 비겼습니다.");
                                lose++;

                        }




                        if((int)random-result(userInput)==2||(int)random-result(userInput)==-1){
                                System.out.println("컴퓨터는 "+out(random)+" 을냈고 결과는 이겼습니다!");
                                System.out.println("현재성적은 "+win+"승 "+lose+"패 "+draw+"번 비겼습니다.");
                                win++;
                        }



                }
        }


}
[/code]

Posted by reiKop

2007/11/12 10:08 2007/11/12 10:08
, ,
Response
No Trackback , No Comment
RSS :
http://reikop.raony.net/blog/rss/response/7

성적프로그램

[code]

import java.util.*;

class ResultReturn {
        char result;
        int score;
        public void reTurn(){
                if(score>=90){
                        result='A';
                }else if(score>=80){
                        result='B';
                }else if(score>=70){
                        result='C';
                }else if(score>=60){
                        result='D';
                }else if(score<60){
                        result='F';
                }

        }

        public void input(){
                Scanner in=new Scanner(System.in);
                score=in.nextInt();
        }

        public void prn(){
                System.out.println("Score :"+score+"   Result :"+result);
        }
}
class exam{
        public static void main(String args[]){
                ResultReturn obj=new ResultReturn();
                obj.input();
                obj.reTurn();
                obj.prn();
        }

}
[/code]

Posted by reiKop

2007/11/09 10:11 2007/11/09 10:11
Response
No Trackback , No Comment
RSS :
http://reikop.raony.net/blog/rss/response/6


블로그 이미지

HTML, Javascript 이야기

- reiKop

Notices

  1. 첫글.

Archives

Authors

  1. reiKop

Calendar

«   2010/03   »
  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 31      

Site Stats

Total hits:
26977
Today:
5
Yesterday:
56