1. ASCII vs Unicode 

 1) xxx stream : 8비트

 2) xxx Reader, xxx Writer : 16비트 


2. Stream방식 vs RandomAccess 방식

 1) Stream : 파일을 순차적으로 나열하기 때문에 접근속도의 문자가있을 수 있다.

 2) RandomAccess : 파일을 랜덤하게 나열하고 키 로 접근할 수 있다.


3. File의 내용 vs Meta data

 1) 파일의 내용을 처리 할 것인가..

 2) 파일에 대한 정보를 처리 할 것인가..

----------------------------------------------------------------------------------------------

 

4. Stream

 1) Sesqence : 순차적이다

 2) Concurrency : 동시성이 없다.

 3) 동시성을 주기위해 Randeom Access 방식을 사용한다.


5. File~~stream : 모두 String 형태로 저장/로드 된다

 1) 파일 스트림에서 한글을 읽을때.

 FileInputStream fis = new FileInputStream(file);
  int readCount =0;
  byte[] buffer = new byte [1024]; // 한글을 읽어야 할 수 있기 때문에 바이트로 받는다. 배열의 크기는 OS 시스템에 맞게 1024정도를 준다. (한번에 1024바이트를 읽는다)
  while((readCount=fis.read(buffer))>=0){
   area.append(new String(buffer,0,readCount));
  }


6. Data~~stream : 데이터형이 유지된다 (int, double...)

 1) 데이터에서 한글을 읽을때

  DataOutputStream dos = null;
//  FileOutputStream fos = null;
  try{
  // fos = new FileOutputStream("D:\\data.dat");
   dos = new DataOutputStream(new FileOutputStream("D:\\data.dat")); //생성자 안에 생성자를 넣어 간소화 (하나만 close()할 수 있다)
   dos.writeBoolean(false);
   dos.writeInt(100);
   dos.writeDouble(89.0);
   dos.writeUTF("hello world");
   dos.writeUTF("안뇽하세염");  // 파일을 열어보면 글자가 깨져서 보이게된다.
  // dos.writeUTF("ㅋㅋㅋ");  //UTF를 지원하기때문에 한글은 UTF를 사용한다.
   System.out.println("파일 저장 완료~!");

 

7. InputStreamReader : 캐릭터형 문자를 바이트형으로 변환하여 받는다 (한글을 받을 수 있다)

 

 System.out.print("당신은 어떤 계절을 좋아하시나요?");
    BufferedReader br = null;
  String season = null;
  try{
   br = new BufferedReader(new InputStreamReader(System.in));  // 
   season =br.readLine();
   System.out.println(season);
   try{
    br.close();  //반드시 닫아야한다
    }catch(IOException ex){}
  }catch(IOException ex){
   
  }

 

8. RandomAccessFile : 파일을 동시에 읽으며 쓰는게 가능하지만 한글처리가 힘들다.

 1) long getFilePointer() : 파일의 위치를 가져온다.

 2) void seek(long pos) : 파일의 원하는 부위에 포인터를 가져다 놓을 수 있다.

 3) long length() : 파일의 길이를 가져온다. End of Point 를 알 수 있다.

 

 4) 한글과 영문을 변환 할 수 있도록 하는 메소드를 만든다.

 package StreamDemo;
import java.io.UnsupportedEncodingException;
public class CharacterConversion {
 public static String entoko(String en){
  String ko = null;
  try{
   ko = new String(en.getBytes("ISO8859_1"),"KSC5601"); //KSC5601 은 ANCI 형 이고, UTF-8은 바꿔 쓰면된다.
  }catch(UnsupportedEncodingException ex){
  }
  return ko;
 }
 public static String kotoen(String ko){
  String en = null;
  try{
   ko = new String(ko.getBytes("KSC5601"),"ISO8859_1");
  }catch(UnsupportedEncodingException ex){
  }
  return en;
 }
}

 

 

 

9. 문자를 파싱(Parsing)하는 3가지방법

 import java.util.Scanner;
// 문자 파싱하는 방법 3가지
public class ScannerDemo {
 public static void main(String[] args) {
  String line = "1101    조성모    45  78  23  100";
  //1. String class's split()
  String [] array = line.split("\\s+");
  //2. java.util.StringTokenizer 
  java.util.StringTokenizer st = new java.util.StringTokenizer(line);
  String [] array1 = new String[st.countTokens()];
  int i = 0;
  while(st.hasMoreElements()){
   array1[i++] = st.nextToken();
  }  
  //3. Scanner 쓰자.
  Scanner scan = new Scanner(line).useDelimiter("\\s+");
  while(scan.hasNext()){
   System.out.println(scan.next());
  }
  
 }
}

 

 

 

10. 직렬화 (Serializtion)

 1) 데이터를 Object형 째로 읽고 쓰는게 가능하다.

 2) 프리미티브 타입은 모두 직렬화가 가능하다. (모든 객체가 Serialization 가능한 것은 아니다.)

 3) Serializable 인터페이스를 상속받은 클래스만 가능하다. (Flag Interface) (String, Vector, ArrayList 등등...)

 4) transient private String author; 로 직렬화에 제외시킬 수 도 있다.

 5) Externalizable 인터페이스를 상속하여 직렬화 할 것만 선택 할 수도 있다.

 

 

11. JAVA NIO

 1) ByteBuffer

  - allocateDirect() : 직접 OS의 메모리에 접근 할 수 있는 입/출력 방식.

 

 

 

 

ps---------------------------------------------------------------

1. os별 파일과 경로 구분선 알아내는 방법

      System.getProperty("file.separator") // os에 종속적인 파일 구분선
      File.separator // os에 종속적이지 않은 구분선.
  

      System.getProperty("path.separator") // 경로 구분선
      File.pathSeparator

'Programming > JAVA' 카테고리의 다른 글

JDBC 문법  (2) 2011.03.24
JDBC 설정  (2) 2011.03.24
JAVA Threads  (2) 2011.03.24
Java Call by value, Call by reference  (2) 2011.03.24
The AWT Component Library  (1) 2011.03.24

+ Recent posts