#####################################
#  File Input/Output Stream ����   # 
#####################################

�ۼ��� : ����ȭ(hhjang97@venus.uos.ac.kr)
�ۼ��� : 2004. 10. 22 
������ : 
  2007. 10. 29 
    Input Stream ���� �߰�
  2007. 06. 27 
    ����<->Byte ��ȯ ���� �߰�
  2006. 02. 14
    ���� Copy �߰� 
  2005. 08. 17
    ���Ͽ��� ���پ� �б� �߰�
  2005. 01. 11
    byte ���� ���� �߰�

���� :
���� :


################################# ################################# #################################

#################################
# File <-> Byte ��ȯ  ó��
#################################

���� : http://cafe.naver.com/ArticleRead.nhn?clubid=10153147&boardtype=L&page=1&articleid=22248

  public static byte[] file2Byte(String file) throws IOException {
    InputStream in = null;
    byte[] pix = null;
    try {
      in = new FileInputStream(file);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      byte[] b = new byte[1024];
      int j;
      while ((j = in.read(b)) != -1) {
        baos.write(b, 0, j);
      }
      pix = baos.toByteArray();
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return pix;
  }

  public static void byte2File(byte[] b, String outputFile)
      throws IOException {
    BufferedOutputStream stream = null;
    try {
      File file = new File(outputFile);
      FileOutputStream fstream = new FileOutputStream(file);
      stream = new BufferedOutputStream(fstream);
      stream.write(b);
    } finally {
      if (stream != null) {
        try {
          stream.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
    }
  }




#################################
#  File Copy 
#################################
// errorPath ������ copyPath�� �����Ѵ�.
void fileCopy (String errorPath, String copyPath){

   int bytesRead;
   byte[] buffer = new byte[512];
   FileInputStream fin = null; // �б� ��� ���� ����
   FileOutputStream fout = null; //������� ���� ����

   try{
      fin = new FileInputStream(errorPath);
      fout = new FileOutputStream(copyPath);
      while((bytesRead = fin.read(buffer)) >= 0){ //�о �Ʒ��ٿ��� �ٷ� ����.
         fout.write(buffer, 0, bytesRead); // ������ ���Ͽ� �����Ѵ�.
      } 
   }catch(IOException e){
      e.printStackTrace();
      System.err.println("��Ʈ�����κ��� �����͸� ���� �� �����ϴ�.");
   }finally{
      try{
         if(fin != null) fin.close();
         if(fout != null) fout.close();
      }catch(IOException e){}
   }
}



#################################
#  Input Stream
#################################
String fileName = "savefile.txt";
FileInputStream fis =  new FileInputStream(fileName); 
BufferedInputStream bufin =  new BufferedInputStream(fis); 
byte[] buffer = new byte[1024]; 
int len; 
while (bufin.available() != 0) { 
   len = bufin.read(buffer); 
   dsa.update(buffer, 0, len); 
}; 
bufin.close();


#### �߰� ����, 07.10.29
while((int len = bufin.read(buffer)) {
   dsa.update(buffer, 0, len); 
}; 



#################################
# Output Stream
#################################

	// ���Ͽ� ������ �Ѵ�.
	byte[] wkbByte = new wkbByte[100];

	FileOutputStream foStream = new FileOutputStream(fileName);
	foStream.write(wkbByte);
	foStream.close();

#################################
# Byte�� ���Ͽ� �����ϱ�
#################################
byte[] headerBuf = new byte[100];
String filename = "byteSaveFile.out"; // "c:/network.out";
File file = new File(filename);
//	���� ��� ��Ʈ���� ������ ��� ��Ʈ���� �����Ѵ�.
FileOutputStream fos = new FileOutputStream(file);
DataOutputStream dos = new DataOutputStream(fos);
 
dos.write(headerBuf);
dos.flush();
dos.close();
fos.close();



#################################
# ���Ͽ��� ���پ� �б�
#################################

	// �α� ������ ������ �а�, ������ �߰��ؼ� ��ȯ�Ѵ�.
	public String getFileText(String filePath) throws Exception {
		StringBuffer sb = new StringBuffer();
		
		String buff = null;
		FileReader fr = new FileReader( new File( filePath ) );

		BufferedReader br = new BufferedReader( fr );

		while ((buff = br.readLine()) != null) {
			sb.append(buff).append("\n");
			//System.out.println(buff);
		} // end while

		br.close();
		fr.close();

		
		return sb.toString();
	}


#################################
# 
#################################

#################################
# ���� URL 
#################################

http://php.pe.kr/java_main/lecture/my/file_io.html