ByteInputStream
FileInputStream
FilterInputStream(보조스트림)
OutputStream - 1 byte 단위로 쓰는놈
ByteOutputStream
FileOutputStream
FilterOutputStream(보조스트림)
Reader - char(2byte, 자바에서는 3byte(UTF-8)) 단위로 읽는놈
FileReader(InputStreamReader을 상속)
BufferedReader
Writer - char(2byte, 자바에서는 3byte(UTF-8)) 단위로 쓰는놈
FileWriter
BufferedWriter
사용방법
1.주 스트림을 소스에 꽂는다.
2.보조스트림은 주 스트림에 꽂는다.
3.프로그램에서 보조스트림을 사용해서 읽는다.
IOExample.java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class IOExample { public static void main(String[] args) { byte[] src = {0, 1, 2, 3}; byte[] dest = null;
try { InputStream is = new ByteArrayInputStream( src ); OutputStream os = new ByteArrayOutputStream(); int data = -1;
while( (data = is.read()) != -1 ) { os.write(data); }
dest = ((ByteArrayOutputStream)os).toByteArray();
System.out.println( Arrays.toString( src )); System.out.println( Arrays.toString( dest ));
} catch( IOException e) { e.printStackTrace();
} } }
|
FileCopy.java1 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 32
| public class FileCopy { public static void main(String[] args) { InputStream is = null; OutputStream os = null;
try { is = new FileInputStream( "./dooly.png" );
os = new FileOutputStream( "./dooly2.png ");
int data = 1;
while( (data = is.read()) !=-1 ) { os.write( data ); } } catch (FileNotFoundException e) { System.out.println("파일없음" + e); } catch (IOException e) { System.out.println("I/O 에러" + e); } finally { try{ if( is != null) { is.close(); } if( os != null) os.close(); }catch(IOException e) {
} } } }
|
123.txt를 UTF-8로 만들고(직접만들자)
FileReader로 1바이트씩 읽어보고
FileInputStream으로 3(UTF-8이므로..) 바이트씩 읽어보자
FileReaderTest.java1 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| public class FileReaderTest { public static void main(String[] args) { Reader reader = null; InputStream is = null; try { reader = new FileReader( "./hello.txt" ); is = new FileInputStream( "./hello.txt ");
int count = 0; int data = -1; while( (data = reader.read()) != -1 ) { count++; System.out.println( (char)data ); }
System.out.println( "\n읽은 회수: " + count );
System.out.println("=========");
count = 0; data = -1;
while( (data = is.read() )!= -1) { count++; System.out.println( (char) data);
}
} catch (FileNotFoundException e) { System.out.println( "파일 없음 " + e ); } catch (IOException e) { System.out.println("I/O 에러:" + e); } finally { try { if( reader != null) { reader.close(); } if( is != null) { is.close(); } }catch(IOException e) { e.printStackTrace();
} } } }
|
보조스트림을 사용하여 보자
BufferedOutputStreamTest.java1 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
| public class BufferedOutputStreamTest {
public static void main(String[] args) { BufferedOutputStream bos = null; try { bos = new BufferedOutputStream( new FileOutputStream( "./123.txt" ), 5 );
for(int i='1' ; i <= '9' ; i++) { bos.write( i ); }
} catch (FileNotFoundException e) { System.out.println("파일 없음: " + e); } catch (IOException e) { System.out.println("I/O 에러" + e); } finally { try { if( bos!= null) { bos.close(); } } catch( IOException e) { } } } }
|
주스트림 FileReader을 꽂고
보조스트림 BufferedReader을 꽂음.
BufferedOutputStreamTest.java1 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
| public class BufferedReaderTest {
public static void main(String[] args) { BufferedReader br = null; try { br = new BufferedReader( new FileReader("./src/io/BufferedReaderTest.java"));
int index = 0; String line = null;
while((line = br.readLine())!= null) { System.out.println(++index + " : " + line); } } catch( FileNotFoundException e) { System.out.println("파일없음: " + e); } catch( IOException e) { System.out.println("I/O 에러" + e); } finally { try { if(br != null) { br.close(); }
}catch(IOException e) { System.out.println("파일없음"+e); } } } }
|
MS 949로 텍스트 파일을 하나만들자.
FileInputStream 주 스트림을 바이트단위로 꽂고
InputStreamReaderTest.java1 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
| public class InputStreamReaderTest {
public static void main(String[] args) { Reader reader = null; try { reader = new InputStreamReader( new FileInputStream("./ms949.txt"), "MS949" );
int data = -1;
while( ( data = reader.read()) != -1 ) { System.out.println( (char)data ); } } catch (UnsupportedEncodingException e) { System.out.println("지원하지 않는 charset"); } catch (FileNotFoundException e) { System.out.println("파일이 없음:" + e); } catch (IOException e) { System.out.println("IOException:"+e); } finally { try { if( reader!=null) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
|
System.in으로 부터 스트림을 생성해보자
KeyboardTest.java1 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 32 33 34 35 36 37 38 39 40
| public class KeyboardTest {
public static void main(String[] args) { BufferedReader br = null;
try { br = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
while(true) { System.out.print( ">>" ); String line = br.readLine();
if (line == null ) { break; }
if( "exit".equals( line )) { break; } System.out.println(line); }
} catch (UnsupportedEncodingException e) { System.out.println("지원하지 않는 charset : "+e); } catch (IOException e) { System.out.println("I/O 에러" + e); } finally { try { if(br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
|
파일 객체사용해보자.
tokenizer도 사용해보자
PhoneList01.java1 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| public class PhoneList01 { public static void main(String[] args) { BufferedReader br = null; try {
File file = new File( "./phone.txt" );
if( file.exists() == false ) { System.out.println( "파일이 존재하지 않습니다" ); return; }
System.out.println( "========== 파일정보 =========" ); System.out.println( "경로" + file.getAbsolutePath() ); System.out.println( "크기: "+ file.length()+"Bytes" );
Date date = new Date( file.lastModified() ); SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss"); System.out.println( "마지막 수정일:" + sdf.format(date));
System.out.println( "전화번호"); br = new BufferedReader( new InputStreamReader( new FileInputStream(file),"UTF-8") );
String line = null;
while((line = br.readLine())!=null) { StringTokenizer st = new StringTokenizer(line, "\t ");
int index = 0; while( st.hasMoreTokens() ) { String s = st.nextToken();
if(index == 0 ) { System.out.print( s + ":"); } else if( index ==1 ) { System.out.print( s+ "-"); } else if( index == 2) { System.out.print( s+ "-"); } else { System.out.print( s); }
index++;
} System.out.print("\n"); }
} catch (FileNotFoundException e) { e.printStackTrace(); }
catch ( UnsupportedEncodingException e) { System.out.println("지원하지 않는 charset"); } catch(IOException e) {
} finally { if( br !=null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } }
}
} }
|
위에서 했던 작업을 Scanner로 간편하게 해보자
PhoneList02.java1 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 32 33 34 35 36 37 38 39 40 41 42 43 44
| public class PhoneList02 {
public static void main(String[] args) { Scanner scanner = null; try { File file = new File("./phone.txt");
if (file.exists() == false) { System.out.println("파일이 존재하지 않습니다"); return; }
System.out.println("========== 파일정보 ========="); System.out.println("경로" + file.getAbsolutePath()); System.out.println("크기: " + file.length() + "Bytes");
Date date = new Date(file.lastModified()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println("마지막 수정일:" + sdf.format(date));
System.out.println("전화번호");
scanner = new Scanner(file); while( scanner.hasNext() ) { String name = scanner.next(); String phone1 = scanner.next(); String phone2 = scanner.next(); String phone3 = scanner.next();
System.out.println(name + ":" + phone1 + "-" + phone2 + "-" + phone3); }
} catch (FileNotFoundException e) { e.printStackTrace(); } finally { if ( scanner != null ) { scanner.close(); } } } }
|