#####################################
#  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