JAVA chapter18. IO 기반 입출력 및 네트워킹. 18.1 IO 패키지 소개, 18.2 입력 스트림과 출력 스트림
JAVA/CONCEPT 2018. 1. 16. 16:36 |JAVA chapter18. IO 기반 입출력 및 네트워킹.
18.1 IO 패키지 소개
18.2 입력 스트림과 출력 스트림
18.2.1 InputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package sec02.exam01_inputstream_read; import java.io.FileInputStream; import java.io.InputStream; public class ReadExample1 { public static void main(String[] args) throws Exception { InputStream is = new FileInputStream("C:/Temp/test.txt"); int readByte; while(true) { readByte = is.read(); if(readByte == -1) break; System.out.println((char)readByte); } is.close(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package sec02.exam01_inputstream_read; import java.io.FileInputStream; import java.io.InputStream; public class ReadExample2 { public static void main(String[] args) throws Exception { InputStream is = new FileInputStream("C:/Temp/test.txt"); int readByteNo; byte[] readBytes = new byte[3]; String data = ""; while( true ) { readByteNo = is.read(readBytes); if(readByteNo == -1) break; data += new String(readBytes, 0, readByteNo); } System.out.println(data); is.close(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package sec02.exam01_inputstream_read; import java.io.FileInputStream; import java.io.InputStream; public class ReadExample3 { public static void main(String[] args) throws Exception { InputStream is = new FileInputStream("C:/Temp/test.txt"); int readByteNo; byte[] readBytes = new byte[8]; readByteNo = is.read(readBytes, 2, 3); for(int i=0; i<readBytes.length; i++) { System.out.println(readBytes[i]); } is.close(); } } | cs |
18.2.2 OutputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package sec02.exam02_outputstream_write; import java.io.FileOutputStream; import java.io.OutputStream; public class WriteExample1 { public static void main(String[] args) throws Exception { OutputStream os = new FileOutputStream("C:/Temp/test.txt"); byte[] data = "ABC".getBytes(); for(int i=0; i<data.length; i++) { os.write(data[i]); } os.flush(); os.close(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package sec02.exam02_outputstream_write; import java.io.FileOutputStream; import java.io.OutputStream; public class WriteExample2 { public static void main(String[] args) throws Exception { OutputStream os = new FileOutputStream("C:/Temp/test.txt"); byte[] data = "ABC".getBytes(); os.write(data); os.flush(); os.close(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package sec02.exam02_outputstream_write; import java.io.FileOutputStream; import java.io.OutputStream; public class WriteExample3 { public static void main(String[] args) throws Exception { OutputStream os = new FileOutputStream("C:/Temp/test.txt"); byte[] data = "ABC".getBytes(); os.write(data, 1, 2); os.flush(); os.close(); } } | cs |
18.2.3 Reader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package sec02.exam03_reader_read; import java.io.FileReader; import java.io.Reader; public class ReadExample1 { public static void main(String[] args) throws Exception { Reader reader = new FileReader("C:/Temp/test.txt"); int readData; while( true ) { readData = reader.read(); if(readData == -1) break; System.out.print((char)readData); } reader.close(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package sec02.exam03_reader_read; import java.io.FileReader; import java.io.Reader; public class ReadExample2 { public static void main(String[] args) throws Exception { Reader reader = new FileReader("C:/Temp/test.txt"); int readCharNo; char[] cbuf = new char[2]; String data = ""; while( true ) { readCharNo = reader.read(cbuf); if(readCharNo == -1) break; data += new String(cbuf, 0, readCharNo); } System.out.println(data); reader.close(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package sec02.exam03_reader_read; import java.io.FileReader; import java.io.Reader; public class ReadExample3 { public static void main(String[] args) throws Exception { Reader reader = new FileReader("C:/Temp/test.txt"); int readCharNo; char[] cbuf = new char[4]; readCharNo = reader.read(cbuf, 1, 2); for(int i=0; i<cbuf.length; i++) { System.out.println(cbuf[i]); } reader.close(); } } | cs |
18.2.4 Writer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package sec02.exam04_writer_write; import java.io.FileWriter; import java.io.Writer; public class WriteExample1 { public static void main(String[] args) throws Exception { Writer writer = new FileWriter("C:/Temp/test.txt"); char[] data = "홍길동".toCharArray(); for(int i=0; i<data.length; i++) { writer.write(data[i]); } writer.flush(); writer.close(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package sec02.exam04_writer_write; import java.io.FileWriter; import java.io.Writer; public class WriteExample2 { public static void main(String[] args) throws Exception { Writer writer = new FileWriter("C:/Temp/test.txt"); char[] data = "홍길동".toCharArray(); writer.write(data); writer.flush(); writer.close(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package sec02.exam04_writer_write; import java.io.FileWriter; import java.io.Writer; public class WriteExample3 { public static void main(String[] args) throws Exception { Writer writer = new FileWriter("C:/Temp/test.txt"); char[] data = "홍길동".toCharArray(); writer.write(data, 1, 2); writer.flush(); writer.close(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package sec02.exam04_writer_write; import java.io.FileWriter; import java.io.Writer; public class WriteExample4 { public static void main(String[] args) throws Exception { Writer writer = new FileWriter("C:/Temp/test.txt"); String data = "안녕 자바 프로그램"; //writer.write(data); writer.write(data, 3, 2); writer.flush(); writer.close(); } } | cs |
'JAVA > CONCEPT' 카테고리의 다른 글
JAVA chapter18. IO 기반 입출력 및 네트워킹. 18.4 파일 입출력 (0) | 2018.01.17 |
---|---|
JAVA chapter18. IO 기반 입출력 및 네트워킹. 18.3 콘솔 입출력 (0) | 2018.01.16 |
JAVA chapter17. javaFX. 17.12 화면 이동과 애니메이션 (0) | 2018.01.16 |
JAVA chapter17. javaFX. 17.11 JavaFX 스레드 동시성 (0) | 2018.01.16 |
JAVA chapter17. javaFX. 17.10 JavaFX CSS 스타일(1) (0) | 2018.01.16 |