java.io.File
는 파일의 paths를 관리하기 위한 클래스이다
File 객체를 생성하기 위하여 사용되는 path는 절대 경로와 상대 경로 모든 형태로 나타낼 수 있다
file.separator
라는 시스템 속성에 의하여 정의된다separator
와 separatorChar
를 통하여 사용 가능하다
// WinNTFileSystem.class
class WinNTFileSystem extends FileSystem {
private final char slash;
private final char altSlash;
private final char semicolon;
public WinNTFileSystem() {
slash = AccessController.doPrivileged(new GetPropertyAction("file.separator")).charAt(0);
semicolon = AccessController.doPrivileged(new GetPropertyAction("path.separator")).charAt(0);
altSlash = (this.slash == '\\') ? '/' : '\\';
}
// 생략
}
package io;
import java.io.File;
public class Remove {
public static void main(String[] args) {
if (args.length == 0) {
System.err.println("Usage: Remove <file|directory>...");
}
for (int i = 0; i < args.length; i++) {
remove(new File(args[i]));
}
}
private static void remove(File file) {
if (!file.exists()) {
System.err.println("No such file or directory: " + file.getAbsolutePath());
} else if (file.isDirectory() && file.list().length > 0) {
System.err.println("Directory contains files: " + file.getAbsolutePath());
} else if (!file.delete()) {
System.err.println("Could not remove: " + file.getAbsolutePath());
}
}
}
java.io
패키지는 입력(input)과 출력(output)을 위한 많은 클래스들을 포함하고 있다
InputStream
그리고 OutputStream
Reader
그리고 Writer
InputStream.read()
메서드는 int 값(읽은 byte의 수)을 리턴한다. 만약 스트림의 끝에 다다르면 -1을 리턴한다
int b = 0;
while ((b == fis.read()) != -1) {
System.out.write(b);
}
private static void read(File file) {
try {
FileInputStream fis = new FileInputStream(file);
try {
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
System.out.write(buffer, 0, len);
}
} finally {
fis.close();
}
} catch (FileNotFoundException e) {
System.err.println("No such file exists: " + file.getAbsolutePath());
} catch (IOException e) {
System.err.println("Cannot read file: " + file.getAbsolutePath() + ": " + e.getMessage());
}
}
ReaderReader.read()
메서드는 int 값(읽은 char의 수)을 리턴한다. 만약 스트림의 끝에 다다르면 -1을 리턴한다
private static void readDefault(File file) {
try {
FileReader reader = new FileReader(file);
try {
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch);
}
System.out.flush();
} finally {
reader.close();
}
} catch (FileNotFoundException e) {
System.err.println("No such file exists: " + file.getAbsolutePath());
} catch (IOException e) {
System.err.println("Cannot read file: " + file.getAbsolutePath() + ": " + e.getMessage());
}
}
java.io.IOException
이다
java.io
패키지에는 파일 I/O 처리를 위해 구현된 클래스들이 포함되어 있다
true
로 지정하면 파일 데이터의 끝에 이어서 쓰게 된다
private static void readUTF8(File file) {
try {
FileInputStream fis = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
try {
OutputStreamWriter writer = new OutputStreamWriter(System.out, "UTF-8");
int ch;
while ((ch = reader.read()) != -1) {
writer.write(ch);
}
writer.flush();
} finally {
reader.close();
}
} catch (FileNotFoundException e) {
System.err.println("No such file exists: " + file.getAbsolutePath());
} catch (IOException e) {
System.err.println("Cannot read file: " + file.getAbsolutePath() + ": " + e.getMessage());
}
}
BufferedReader input = new BufferedReader(new InputStreamReader(System.in) );
String line = input.readLine();
package com.marakana.demo;
import java.io.*;
public class HelloYou {
public static void main(String[] args) {
System.out.print("What's your name? ");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in) );
try {
String name = input.readLine();
System.out.println("Hello, " + name + "!");
} catch (IOException e) {
e.printStackTrace();
}
}
}