레이블이 JNDI인 게시물을 표시합니다. 모든 게시물 표시
레이블이 JNDI인 게시물을 표시합니다. 모든 게시물 표시

2013년 8월 2일 금요일

JAVA RandomAccessFile (자바 랜덤파일처리)

임의의 바이트, 텍스트등을 파일내 임의의 위치에 쓸수 있도록 허락
InputStream이나 OutputStream의 서브 클래스는 아니지만 파일의 읽기, 쓰기를 위한 독립적인 메소드를 제공한다. 


 오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
(오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷 실무전문 강의) 

length() : 파일의 길이 
getFilePointer() : 현재 포인터의 위치 
seek() : 포인터 위치 설정 
readBoolean(), readByte(),readChar(), readShort(), readLong(), readInt(), readFloat(),readDouble(), readLine(), readUTF() : 해당타입의 데이터 읽기 
writeBoolean(), writeByte(),writeChar(), writeShort(), writeLong(), writeInt(), writeFloat(), writeDouble(), writeUTF() : 해당타입의 데이터 쓰기 
close() : 파일 닫기 



import java.io.*; class RandomTest { public static void main(String[] args) throws IOException {      RandomAccessFile raf=new 
                            RandomAccessFile("test.txt","rw");      raf.seek(raf.length());      raf.writeUTF("The end");      raf.close(); } }



----------------
메모장 –  예제
----------------
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class NotePad extends Frame implements ActionListener {
TextArea text = null;
String filename = null;

public NotePad(String title) {
    super(title); // set title
    MenuBar menuBar=new MenuBar();
    Menu fileMenu = new Menu("File");
    menuBar.add(fileMenu);
    MenuItem openItem = new MenuItem("Open...", new MenuShortcut('O'));
    openItem.setActionCommand("Open"); // for getActionCommand
    openItem.addActionListener(this);
    fileMenu.add(openItem);


    MenuItem saveItem = new MenuItem("Save", new MenuShortcut('S'));
    saveItem.setActionCommand("Save"); // for getActionCommand
    saveItem.addActionListener(this);
    fileMenu.add(saveItem);
    MenuItem saveasItem = new MenuItem("Save As...", new MenuShortcut('V'));
    saveasItem.setActionCommand("SaveAs"); // for getActionCommand
    saveasItem.addActionListener(this);
    fileMenu.add(saveasItem);
    fileMenu.addSeparator();
    MenuItem exitItem = new MenuItem("Exit", new MenuShortcut('X'));
    exitItem.setActionCommand("Exit"); // for getActionCommand
    exitItem.addActionListener(this);
    fileMenu.add(exitItem);
    setMenuBar(menuBar);
    text = new TextArea();   


    add(text, "Center");
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent we) {
            dispose();
            System.exit(0);
        }
    });
    setSize(500, 400);    setVisible(true);
}
public static void main(String args[]) throws Exception {
    new NotePad("Simple NotePad...");
}
public void actionPerformed(ActionEvent e) {
    String cmd=e.getActionCommand();
    if (cmd.equals("Exit"))
        System.exit(0);
    else if (cmd.equals("Open")) {
        FileDialog dialog=new FileDialog(this, "Text File Open", FileDialog.LOAD);
        dialog.show(); // file dialog is modal
        String filename=dialog.getFile();


      if (filename != null) {
            String directory=dialog.getDirectory();
            if (directory != null)
                filename=directory+filename;
            loadFile(filename);
        }
    }
    else if (cmd.equals("Save")) {
        if (this.filename != null) saveFile(this.filename);
        else {
            FileDialog dialog=new FileDialog(this, "Text File Save", FileDialog.SAVE);
            dialog.show(); // file dialog is modal
            String filename=dialog.getFile();
            if (filename != null) {
                String directory=dialog.getDirectory();
                if (directory != null)
                    filename=directory+filename;
                saveFile(filename);
            }
        }
    }


else if (cmd.equals("SaveAs")) {
        FileDialog dialog=new FileDialog(this, "Text File Save", FileDialog.SAVE);
        dialog.show(); // file dialog is modal
        String filename=dialog.getFile();
        if (filename != null) {
            String directory=dialog.getDirectory();
            if (directory != null)
                filename=directory+filename;
            saveFile(filename);
        }
    }
}


private void loadFile(String filename) {
    BufferedReader in = null; 
    text.setText("");

    try {
        in = new BufferedReader(new FileReader(filename),1024); 
        String string=null;


      while ((string=in.readLine()) != null) {  text.append(string+'\n');    }
        in.close();
    } 
    catch (IOException ie) {System.err.println("File Read Error : "+ie.getMessage()); }
    setTitle("FileName:"+filename);
    this.filename=filename;
}
private void saveFile(String filename) {
    BufferedWriter out = null; 
    try {
        out = new BufferedWriter(new FileWriter(filename));
      String string=text.getText();
        out.write(string); 
      out.close();
  }
    catch(IOException e) { System.err.println("File Read Error : "+e.getMessage()); }
    setTitle("FileName:"+filename);
    this.filename=filename;
}
}

2013년 7월 29일 월요일

JAVA JNDI를 이용한 DNS 서비스 구현(JAVA JNDI DNS)

JNDI를 이용한 DNS 서비스 구현
ㅓ


오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
(오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷  실무전문 강의)



즉 자바 응용 프로그램이 어느 위치에 있든지 필요한 자바 객체들을 검색할 수 있는 것입니다. EJB 환경에서는 JNDI를 이용해 EJB Home 객체를 얻어낸 후 이를 이용해 Java Beans 객체를 생성하거나 접근하게 됩니다.

JNDI는 DNS(Domain Name System), COS(Common Object Services) 등의 네이밍 서비스 표준과 LDAP(Lightweight Directory Access Protocol), NDS(NetWare Directory Service), NIS(Netware Information System) 등 API를 제공 합니다.

참고로 JDK1.4 부터는 JNDI 서비스 프로바이더에 DNS Service Provoder를 제공하여 DNS 서비스를 통해 네이밍 서비스를 받을 수 있도록 지원 했는데 아래는 그 예제 입니다.

/*
 * Created on 2005. 1. 9
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author 이종철
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import java.util.Hashtable;
import javax.naming.directory.*;
import javax.naming.NamingEnumeration;


public class SimpleDNS {

             public static void main(String[] args) {
                           Hashtable h = new Hashtable();
                           h.put("java.naming.factory.initial","com.sun.jndi.dns.DnsContextFactory");
                           h.put("java.naming.provider.url","dns://ns.dacom.co.kr");
                          
                           try {
                                        //DirContext 초기화
                                        DirContext context = new InitialDirContext(h);
                                       
                                        //DNS질의 결과를 받아 오자...
                                        Attributes attribute = context.getAttributes("onjprogramming.co.kr");
                                       
                                        //질의 결과 출력
                                        NamingEnumeration ne = attribute.getAll();
                                        System.out.println("onjprogramming.co.kr --> ");
                                        while(ne.hasMoreElements()) {
                                                     System.out.println(ne.next());
                                        }
                           }
                           catch(Exception e) {
                                        e.printStackTrace();
                           }
                          
             }
}

[결과]
onjprogramming.co.kr -->
NS: ns1.gabia.co.kr., ns.gabia.net., ns.gabia.co.kr.


[출처]오라클자바커뮤니티



www.oraclejavacommunity.cop.kr