달력

5

« 2024/5 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

'FileInputStream'에 해당되는 글 1

  1. 2012.10.18 [ 파일 IO ] 스트림 파일 쓰기 / 읽기 Byte , Buffered

http://suite.tistory.com  fs 2012.10 

 

매번 검색해서 사용하던거 정리 

java.lang.OutOfMemoryError: Java heap space 에러나지 MAX_BUF_BYTE 단위로 loop !


※ Buffered*클래스 사용하면 큰파일에 유리하다고 구글링!~ 

 

1.  InputStream 으로 파일 쓰기

 		private final int MAX_BUF_BYTE = 1024000; 

		FileInputStream in = null;
		FileOutputStream fos = null;
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;

		try {

			
			byte[] buffer = new byte[MAX_BUF_BYTE];

			in = new FileInputStream(new File("file"));
			fos = new FileOutputStream(filename);
			bis = new BufferedInputStream(in);

			bos = new BufferedOutputStream(fos);
			int len = 0;
			while ((len = bis.read(buffer)) >= 0) {
				bos.write(buffer, 0, len);
			}

		} catch (Exception e) {
			LOG.info(e.toString());
		} finally {
			try {

				bos.close();
				bis.close();
				fos.close();
				in.close();
			} catch (Exception e) {
			}
		}


2.   stream을 byte 로 변환  

ByteArrayOutputStream 클래스 활용 


		InputStream in = null;
		BufferedInputStream bis = null;
		ByteArrayOutputStream arrayBuff = new ByteArrayOutputStream();
		try {

			byte[] buffer = new byte[MAX_BUF_BYTE];

			in=new FileInputStream(new File("readFile"));
			bis = new BufferedInputStream(in);
			int len = 0;
			while ((len = bis.read(buffer)) >= 0) {
				arrayBuff.write(buffer, 0, len);
			}

		} catch (Exception e) {
			LOG.info(e.toString());
		} finally {

			try {
		
				in.close();
				bis.close();

			} catch (Exception e) {
			}
		}
		return arrayBuff.toByteArray();


:
Posted by mastar