import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class NioFileExample {
public static void main(String[] args) {
// 파일에 쓰기
String data = "Hello, NIO!";
Path path = Paths.get("nio_example.txt");
try (FileChannel fileChannel = FileChannel.open(path,
StandardOpenOption.CREATE,
StandardOpenOption.WRITE)) {
// 데이터를 바이트 버퍼에 넣기
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put(data.getBytes());
// 버퍼를 플립 (읽기 모드로 전환)
buffer.flip();
// 채널을 통해 버퍼를 파일에 쓰기
fileChannel.write(buffer);
System.out.println("파일에 데이터가 성공적으로 쓰였습니다.");
} catch (IOException e) {
e.printStackTrace();
}
// 파일에서 읽기
try (FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.READ)) {
// 읽을 데이터를 위한 버퍼 할당
ByteBuffer buffer = ByteBuffer.allocate(1024);
// 파일에서 데이터를 버퍼로 읽기
int bytesRead = fileChannel.read(buffer);
// 버퍼를 읽기 모드로 플립
buffer.flip();
// 버퍼에서 데이터를 읽어서 출력
byte[] byteArray = new byte[bytesRead];
buffer.get(byteArray);
System.out.println("파일에서 읽은 데이터: " + new String(byteArray));
} catch (IOException e) {
e.printStackTrace();
}
}
}
...