File Input Stream File Output Stream LAB Simple
File. Input. Stream과 File. Output. Stream ¢ 파일이 입출력 대상이 된다.
LAB: Simple. Pair 클래스 작성하기 public class Copy. File 1 { public static void main(String[] args) throws IOException { File. Input. Stream in = null; File. Output. Stream out = null; try { in = new File. Input. Stream("input. txt"); out = new File. Output. Stream("output. txt"); int c; while ((c = in. read()) != -1) { out. write(c); } } finally { if (in != null) in. close(); if (out != null) out. close(); } } }
LAB: Simple. Pair 클래스 작성하기 input. txt The language of truth is simple. Easier said than done. First think and speak. Translators, traitors. No smoke without fire. output. txt The language of truth is simple. Easier said than done. First think and speak. Translators, traitors. No smoke without fire.
LAB: Simple. Pair 클래스 작성하기 public class Byte. Streams. Lab { public static void main(String[] args) throws IOException { Scanner scan = new Scanner(System. in); System. out. print("원본 파일 이름을 입력하시오: "); String input. File. Name = scan. next(); System. out. print("복사 파일 이름을 입력하시오: "); String output. File. Name = scan. next(); try (Input. Stream input. Stream = new File. Input. Stream(input. File. Name); Output. Stream output. Stream = new File. Output. Stream(output. File. Name)) { int c; while ((c = input. Stream. read()) != -1) { output. Stream. write(c); } } System. out. println(input. File. Name + "을 " + output. File. Name + " 로 복사하였습니다. "); } }
예제 public class Copy. File 2 { public static void main(String[] args) throws IOException { File. Reader input. Stream = null; File. Writer output. Stream = null; try { input. Stream = new File. Reader("input. txt"); output. Stream = new File. Writer("output. txt"); } } int c; while ((c = input. Stream. read()) != -1) { output. Stream. write(c); } } finally { if (input. Stream != null) { input. Stream. close(); } if (output. Stream != null) { output. Stream. close(); } }
예제 File. Input. Stream file. St = new File. Input. Stream("sample. dat"); Data. Input. Stream data. St = new Data. Input. Stream(file. St); int i = data. St. read. Int();
예제 input. Stream = new Buffered. Reader(new File. Reader("input. txt")); output. Stream = new Buffered. Writer(new File. Writer("out put. txt"));
문자 엔코딩 ¢ 자바에서는 Standard. Charsets 클래스 안에 각 엔코딩 방법이 Standard. Charsets. UTF_8, Standard. Charsets. UTF_16과 같이 상수 로 정의되어 있다. ¢ String s = new String(100, Standard. Charsets. UTF_8 ); ¢ 파일에서 읽을 때는 Input. Stream. Reader 클래스를 사용한다.
예제 public class Char. Encoding. Test { public static void main(String[] args) throws IOException { File file. Dir = new File("input. txt"); Buffered. Reader in = new Buffered. Reader(new Input. Stream. Reader( new File. Input. Stream(file. Dir), "UTF 8")); String str; while ((str = in. read. Line()) != null) { System. out. println(str); } } }
Data. Input. Stream 과 Data. Output. Stream ¢ Data. Input. Stream 과 Data. Output. Stream 클래스는 기초 자료형 단 위로 데이터를 읽고 쓸 수 있다. ¢ Data. Input. Stream 클래스는 read. Byte(), read. Int(), read. Double() 과 같은 메소드들을 제공한다. ¢ Data. Output. Stream 클래스는 write. Byte(int v), write. Int(int v), write. Double(double v)와 같은 메소드들을 제공한다.
예제 import java. io. *; public class Data. Stream. Test { public static void main(String[] args) throws IOException { Data. Input. Stream in = null; Data. Output. Stream out = null; try { int c; out = new Data. Output. Stream(new Buffered. Output. Stream( new File. Output. Stream("data. bin"))); out. write. Double(3. 14); out. write. Int(100); out. write. UTF("자신의 생각을 바꾸지 못하는 사람은 결코 현 실을 바꿀 수 없다. "); out. flush(); in = new Data. Input. Stream(new Buffered. Input. Stream( new File. Input. Stream("data. bin")));
예제 System. out. println(in. read. Double()); System. out. println(in. read. Int()); System. out. println(in. read. UTF()); } finally { if (in != null) { in. close(); } if (out != null) { out. close(); } } 3. 14 100 자신의 생각을 바꾸지 못하는 사람은 결코 현실을 바꿀 수 없다.
예제 publ ic class Zip. Test { public static void main(String[] args) throws IOException { File. Input. Stream fin = new File. Input. Stream("test. zip"); Zip. Input. Stream zin = new Zip. Input. Stream(fin); Zip. Entry entry = null; while ((entry = zin. get. Next. Entry()) != null) { System. out. println("압축 해제: " + entry. get. Name()); File. Output. Stream fout = new File. Output. Stream(entry. get. Name()); for (int c = zin. read(); c != -1; c = zin. read()) { fout. write(c); } zin. close. Entry(); fout. close(); } zin. close(); } } 압축 해제: eclipse. ini
예제 import java. io. *; import java. util. Date; public class Object. Stream. Test { public static void main(String[] args) throws IOException { Object. Input. Stream in = null; Object. Output. Stream out = null; 객체를 직렬화하 여서 쓴다. try { int c; out = new Object. Output. Stream(new File. Output. Stream("object. dat")); out. write. Object(new Date()); out. flush(); in = new Object. Input. Stream(new File. Input. Stream("object. dat")); Date d = (Date) in. read. Object(); System. out. println(d);
예제 } catch (Class. Not. Found. Exception e) { } } finally { if (in != null) { in. close(); } if (out != null) { out. close(); } } } Mon Jul 13 13: 39: 47 KST 2015
예제 public clas s File. Test { public static void main(String[] args) throws IOException { String name = "c: /eclipse"; File dir = new File(name); String[] file. Names = dir. list(); // 현재 디렉토리의 전체 파일 리스트 for (String s : file. Names) { File f = new File(name + "/" + s); // 절대 경로로 이름을 주 어야 함 System. out. println("================"); System. out. println("이름: " + f. get. Name()); System. out. println("경로: " + f. get. Path()); System. out. println("부모: " + f. get. Parent()); System. out. println("절대경로: " + f. get. Absolute. Path()); System. out. println("정규경로: " + f. get. Canonical. Path()); System. out. println("디렉토리 여부: " + f. is. Directory()); System. out. println("파일 여부: " + f. is. File()); } System. out. println("================"); } }
예제 ================ 이름: . eclipseproduct 경로: c: eclipse. eclipseproduct 부모: c: eclipse 절대경로: c: eclipse. eclipseproduct 정규경로: C: eclipse. eclipseproduct 디렉토리 여부: false 파일 여부: true ================
예제 public class RGB 2 Gray { Buffered. Image my. Image; int width; int height; public RGB 2 Gray() { File ifile = new File("test. jpg"); try { my. Image = Image. IO. read(ifile); } catch (IOException e) { e. print. Stack. Trace(); } width = my. Image. get. Width(); height = my. Image. get. Height(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) {
예제 + green + blue, red + green + blue); } Color c = new Color(my. Image. get. RGB(x, y)); int red = (int) (c. get. Red() * 0. 299); int green = (int) (c. get. Green() * 0. 587); int blue = (int) (c. get. Blue() * 0. 114); Color gray = new Color(red + green + blue, red my. Image. set. RGB(x, y, gray. get. RGB()); } File ofile = new File("gray. jpg"); try { Image. IO. write(my. Image, "jpg", ofile); } catch (IOException e) { e. print. Stack. Trace(); } } } static public void main(String args[]) throws Exception { RGB 2 Gray obj = new RGB 2 Gray(); }
예제 public class Caesar. Cipher { public static void main(String[] args) throws IOException { File. Reader fr = new File. Reader("input. txt"); Buffered. Reader br = new Buffered. Reader(fr); String plaintext = br. read. Line(); System. out. println(Caesar. Cipher. encode(plaintext, 3)); System. out. println(Caesar. Cipher. decode( Caesar. Cipher. encode(plaintext, 3)); fr. close(); } // 아래 코드는 http: //rosettacode. org/wiki/Caesar_cipher에서 가져왔습니다. public static String decode(String enc, int offset) { return encode(enc, 26 - offset); }
예제 public static String encode(String enc, int offset) { offset = offset % 26 + 26; String. Builder encoded = new String. Builder(); for (char i : enc. to. Char. Array()) { if (Character. is. Letter(i)) { if (Character. is. Upper. Case(i)) { encoded. append((char) ('A' + (i - 'A' + offset) % 26)); } else { encoded. append((char) ('a' + (i - 'a' + offset) % 26)); } } else { encoded. append(i); } } return encoded. to. String(); } }
Wkh odqjxdjh ri wuxwk lv vlpsoh. The language of truth is simple.
예제 public class Test { static String solution; static boolean check(String s, String. Buffer a, char ch) { int i; for (i = 0; i < s. length(); i++) { if (s. char. At(i) == ch) a. set. Char. At(i, ch); } for (i = 0; i < s. length(); i++) if (s. char. At(i) != a. char. At(i)) return false; return true; }
예제 public static void main(String[] args) throws IOException { char ch; Scanner sc = new Scanner(System. in); Buffered. Reader in = null; String[] words = new String[100]; int count = 0; in = new Buffered. Reader(new File. Reader("sample. txt")); for (int i = 0; i < 100; i++) { String s = in. read. Line(); if (s == null) break; words[i] = s; count++; } int index = (new Random()). next. Int(count); solution = words[index]; String. Buffer answer = new String. Buffer(solution. length()); for (int i = 0; i < solution. length(); i++) answer. append(' '); for (int i = 0; i < solution. length(); i++) { if (solution. char. At(i) != ' ') answer. set. Char. At(i, '_'); }
예제 while (true) { // System. out. println("현재의 상태: " + solution); System. out. println("현재의 상태: " + answer); System. out. printf("글자를 추측하시오: "); String c = sc. next(); if (check(solution, answer, c. char. At(0)) == true) break; } System. out. println("현재의 상태: " + answer); } }
- Slides: 46