레이블이 JSP 채팅인 게시물을 표시합니다. 모든 게시물 표시
레이블이 JSP 채팅인 게시물을 표시합니다. 모든 게시물 표시

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년 8월 1일 목요일

(Java PolyMorphism)자바 다형성

주어진 유형의 변수 하나로 여러 유형의 객체를 참조하고 , 변수가 참조하는 객체 유형에 맞는 메소드를 자동으로 호출할 수 있는것
 즉 다형성을 이용하면 메소드 호출 하나가 호출이 적용되는 객체의 유형에 따라 서로 다르게 작동하도록 할 수 있다.
  다형성이 구현되기 위한 조건
    - 서브클래스의 객체의 메소드 호출은 수퍼클래스 유형의 변수를 통해서
        Book selectedBook;
  Random select = new Random();

for(int i=0;i<10;i++) {
selectedBook = theBooks[select.nextInt(theBooks.length)];
System.out.println("\n ***** 선택한 책 ***** \n" + selectedBook);
selectedBook.display();
}         
 호출된 메소드는 수퍼클래스의 메소드
 수퍼클래스와 서브클래스에서 메소드 유형및 리턴값이 같아야 한다.
 메소드 접근제한은 서브클래스가 수퍼클래스 보다 덜 제한적이어야 한다.

import java.util.Random;
class Book {
public Book(String aType) { type = new String(aType); }
public String toString() { return "이것은 " + type; }
public void display() { }
private String type;
}

class EngBook extends Book {
public EngBook(String aName) {
super("영어책...");
name = aName;
}
public void display() { System.out.println("이 책은 영어를 공부하는데 쓰입니다...");}
public String toString() { return "책 제목은 : " + name ; }
private String name;
}


class JavaBook extends Book {
public JavaBook(String aName) {
super("자바책...");
name = aName;
}
public void display() {
System.out.println("이 책은 자바언어를 공부하는데 쓰입니다...");
}
public String toString() {
return "책 제목은 : " + name ;
}
private String name;
}

public class MyPolymorphism {
public static void main(String[] args) {


Book[] theBooks = {
              new EngBook("English Alive..."),
              new JavaBook("자바정복하기..."),
new JavaBook("자바30일완성..."),
new EngBook("영어박살내기...")
};
Book selectedBook;
Random select = new Random();

for(int i=0;i<10;i++) {
selectedBook = theBooks[select.nextInt(theBooks.length)];
System.out.println("\n ***** 선택한 책 ***** \n" + selectedBook);
selectedBook.display();
}
        }
}


2013년 7월 29일 월요일

JSP Chatting

참고하세요~


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





소스 파일은 


http://blog.naver.com/PostView.nhn?blogId=julymorning4&Redirect=View&logNo=100192997244&categoryNo=23&isAfterWrite=true 


에서 참고하세요~