โ 1. java.nio ํจํค์ง
- NIO(New Input Output)๋ ๊ธฐ์กด java.io ํจํค์ง๋ฅผ ๊ฐ์ ํ ์๋ก์ด ์ ์ถ๋ ฅ ํจํค์ง์ด๋ค.
- JDK 7๋ถํฐ๋ ํ์ผ I/O๋ฅผ ๊ฐ์ ํ NIO2๋ ๋ฑ์ฅ์ ํจ( ์ด๋ java.nio.file ํจํค์ง๊ฐ ๋ฑ์ฅํจ )
- java.nio.file, java.nio.channels, java.nio.charset ๋ฑ์ด ์์
- File ํด๋์ค๋ณด๋ค ๋ค์ํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ Path ํด๋์ค๋ฅผ ์ ๊ณตํจ. java.nio.file ์์ Path ํด๋์ค๊ฐ ์์.
- Files ํด๋์ค๋ฅผ ์ ๊ณตํ๋ฉฐ, ํ์ผ/๋๋ ํฐ๋ฆฌ์ ์กฐ์, ํ์ผ์ ์ฝ๊ธฐ/์ฐ๊ธฐ ๊ธฐ๋ฅ์ ์ ๊ณตํจ
- ํ์ผ ์ ์ถ๋ ฅ ๋ชจ๋ ๊ฐ๋ฅํ FileChannel ํด๋์ค๊ฐ ์์ ( ๋ฒํผ๋ง ๊ธฐ๋ฅ, ๋ฉํฐ์ค๋ ๋์ ์์ ํจ )
- ์ ์ถ๋ ฅ์ด ๊ธฐ์กด์๋ ๊ตฌ๋ถ์ด ๋๋ ๊ฒฝํฅ์ด ์์์ง๋ง, FileChannel ํด๋์ค์์๋ ์ ์ถ๋ ฅ ๋์์ ์ํ์ด ๊ฐ๋ฅํ ํด๋์ค
- ๋น๋๊ธฐ์ ํ์ผ ์ ์ถ๋ ฅ์ ์ํ AsynchronousFileChannel ํด๋์ค๋ฅผ ์ ๊ณตํจ ( non-blocking ๋ฐฉ์ ํ์ผ ์ ์ถ๋ ฅ )
โ 2. Path ์ธํฐํ์ด์ค
import java.nio.file.*;
import java.io.IOException;
public class SimpleNioExample {
public static void main(String[] args) throws IOException {
Path path = Paths.get("hello.txt"); // ํ์ผ ๊ฒฝ๋ก ์์ฑ
Files.writeString(path, "์๋
ํ์ธ์!"); // ํ์ผ์ ๋ฌธ์์ด ์ฐ๊ธฐ
String text = Files.readString(path); // ํ์ผ์์ ๋ฌธ์์ด ์ฝ๊ธฐ
System.out.println("์ฝ์ ๋ด์ฉ: " + text); // ์ถ๋ ฅ: ์๋
ํ์ธ์!
Files.deleteIfExists(path); // ํ์ผ ์ญ์
}
}
- java.nio.file ํจํค์ง์ ์กด์ฌํ๋ฉฐ java.io.File ํด๋์ค๋ฅผ ๋์ฒดํ๋ ์ธํฐํ์ด์ค๋ก ๋ณผ ์ ์์.
- ํ์ผ์์คํ ์ ์กด์ฌํ๋ ํ์ผ์ด๋ ๋๋ ํฐ๋ฆฌ์ ํด๋นํ๋ ๊ฒฝ๋ก๋ฅผ ํํํจ ( ์ ๋ ๊ฒฝ๋ก, ์๋ ๊ฒฝ๋ก๋ก ํํ๋จ )
- ๊ฒฝ๋ก์ ์์ฑ, ๊ฒฝ๋ก์ ๋น๊ต, ๊ฒฝ๋ก ์ ๋ณด ์ถ์ถ, ๊ฒฝ๋ก ์์ ์กฐ์ ๊ธฐ๋ฅ ๋ฑ์ ์ ๊ณตํจ.
- java.nio.file.Files ํด๋์ค๊ฐ ์ค์ ํ์ผ์ ๋ํ ์์ฑ, ์ฝ๊ธฐ, ์ฐ๊ธฐ, ๋ณต์ฌ, ์ด๋ ๋ฑ์ ์์ ์ด ๊ฐ๋ฅํ ํด๋์ค์.
- ์ฆ, InputStream, OutputStream ๋ฑ์ ๋ด๋ถ์ ์ผ๋ก ํ์ฉํ๋ค๊ณ ๋ณผ ์ ์์.
- ํด๋น Files ํด๋์ค์ Path ๊ฐ์ฒด์ ์ ๋ณด๋ฅผ ํ ๋๋ก ๊ฐ๋ฅํ ์์ ์. ์ฆ, Path ๊ฐ์ฒด๊ฐ ์ธ์๋ก ์ ๊ณต์ด ๋์ด์ผ ํจ.
- ๊ทธ๋ฆฌ๊ณ , ๋ด๋ถ์ ์ผ๋ก InputStream ๋ฑ์ ํ์ฉํ๊ณ ์์ด, nio๋ ๊ธฐ์กด์ ioํจํค์ง๋ฅผ ํ์ฅํ ๊ฐ๋ ์ด๋ผ๊ณ ๋ณผ ์ ์์.
โ 3. FileSystem ํด๋์ค & FileStore ํด๋์ค
(1) FileSystem
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.io.IOException;
public class DiskInfoExample {
public static void main(String[] args) throws Exception {
FileSystem fs = FileSystems.getDefault();
for (FileStore store : fs.getFileStores()) {
System.out.println("๋๋ผ์ด๋ธ ์ด๋ฆ : " + store.name());
System.out.println("ํ์ผ์์คํ
: " + store.type());
System.out.println("์ ์ฒด ๊ณต๊ฐ: " + store.getTotalSpace() + " bytes");
System.out.println("์ฌ์ฉ ์ค์ธ ๊ณต๊ฐ: " + (stroe.getTotalSpace() - store.getUsableSpace()) + " bytes");
}
}
}
- FileSystem์ ํ์ผ ์์คํ ์ ๋ํ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํจ
- FileSystems.getDefault()์ ๊ธฐ๋ณธ ํ์ผ ์์คํ ์ ๋ฆฌํด์ ํด์ค๋ค.
- lterable <FileStore> getFileStores() ํธ์ถ ํ๋ฉด, FileStore ๊ตฌํ์ฒด๋ฅผ ๋ฆฌํดํจ. ์ด์์ฒด์ ์ ๋ง๋ ๊ตฌํ์ฒด์.
- FileSystem ํด๋์ค์์ WatchService new WatchService() ๊ฐ์ฒด๋ฅผ ๋ฆฌํด ๋ฐ์ ์ ์์.
- ํ์ฌ ์ด์์ฒด์ ์์ ์ฌ์ฉํ๋ ํ์ผ ์์คํ ์ ๋ํ ์ ๋ณด๋ฅผ ์ ๊ณตํ๊ณ , ํ์ผ๊ณผ ๋๋ ํ ๋ฆฌ ์ ๊ทผ ๋ฐฉ๋ฒ์ ์ ์ดํจ.
- ์ฆ, ์ด์์ฒด์ ๋ง๋ค ๋ค๋ฅธ ๊ตฌํ ํด๋์ค๋ฅผ ๊ฐ์ง๊ณ ์๊ณ , Window์ฉ JRE์๋ WindowsFileSystem๊ณผ ๊ฐ์ ๊ตฌํ์ด์์.
- ์ฃผ์ ์ญํ : ๊ฒฝ๋ก(Path) ๊ฐ์ฒด ์์ฑ, ํ์ผ ์์คํ ์ ๋ฃจํธ ๋๋ ํ ๋ฆฌ ๋ชฉ๋ก ์กฐํ ๋ฑ์ด ์์.
(2) FileStore
- ํ์ผ ์์คํ ๋ด์์ ์ค์ ์คํ ๋ฆฌ์ง ์ฅ์น(๋์คํฌ ํํฐ์ , ๋คํธ์ํฌ ๋๋ผ์ด๋ธ ๋ฑ) ํ๋๋ฅผ ์ถ์ํํ ํด๋์ค์.
- ์ ์ฅ ์ฅ์น์ ์ด๋ฆ, ์ด ๊ณต๊ฐ, ์ฌ์ฉ ๊ฐ๋ฅ ๊ณต๊ฐ, ํ์ผ ์์คํ ํ์ ๋ฑ์ ์ ๋ณด๋ฅผ ์ ๊ณตํจ.
- ์ฆ, ์ด์์ฒด์ ์ ๋ฐ๋ผ ์ด๊ฒ๋ ๋ค๋ฅด๋ค๋ ๊ฒ์. FileStroe๋ ์ด์์ฒด์ ๋ง๋ค ๊ตฌํ ํด๋์ค๋ฅผ ๋ค๋ฅด๊ฒ ์ ๊ณตํจ.
โ 4. Files ํด๋์ค
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
public class FilesPathExample {
public static void main(String[] args) throws IOException {
Path path = Paths.get("example.txt");
if (Files.exists(path)) {
String firstLine = Files.readAllLines(path).get(0);
System.out.println("First line: " + firstLine);
} else {
System.out.println("File does not exist.");
}
}
}
- ํ์ผ ์กฐ์ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ static ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค.
- ๋ฉ์๋๋ Path ๊ฐ์ฒด๋ก ์ธ์๋ก ๊ฐ์ง๊ณ ์์ ์ ํจ.
- ์ฝ๊ฒ๋งํด, ํ์ผ ์ ์ถ๋ ฅ I/O ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ํด๋์ค๋ฅผ ์๋ฏธํ๋ฉฐ, Path ๊ฐ์ฒด์ ์ธ์๋ฅผ ๊ฐ์ง๊ณ ์์ด์ผ ๊ฐ๋ฅํจ.
(1) ์ฃผ์ ๋ฉ์๋
โ 5. java.nio.file ์ ๋ฆฌํ๋ฉด..
- ์ ๋ฆฌํ๋ฉด, Path๋ฅผ ์ด์ฉํ๋ ค๋ฉด ์ด์์ฒด์ ์ ํ์ผ ์ ๋ณด๋ฅผ ์ ์ ์๋ FileSystem ํด๋์ค์ ๊ตฌํ์ฒด๋ฅผ ํตํด ๋ด๋ถ์ ์ผ๋ก ์ ์ ์์ผ๋ฉฐ, Path ์ ๋ณด ์ฆ, ํด๋น ํด๋์ ๊ฒฝ๋ก๋ฅผ ํตํด ํ์ผ์ ์ ๋ณด๋ฅผ ์ ์ ์์ผ๋ฉฐ, ๊ทธ๋ฆฌ๊ณ Path ๊ฐ์ฒด๋ฅผ Files์ ์ค์ผ๋ก์จ, Files์ ํ์ผ์ ๋ํ ์ ์ถ๋ ฅ ์ฆ, I/O๊ฐ ๊ฐ๋ฅํด์ง๋ค๋ ์๋ฏธ์ด๋ค.
โ 6. ๋ฒํผ
- ๋ฒํผ๋ ๋ฐ์ดํฐ๋ฅผ ์์๋ก ์ ์ฅํ๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์๋ฏธํ๋ฉฐ, ์ฃผ๋ก I/O ์์ ์์ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์๋๋ฅผ ๋ง์ถ๊ธฐ ์ํด ์ฌ์ฉ๋จ.
- ์ฝ๊ฒ๋งํด, CPU๋ ์ ์ถ๋ ฅ ๋ง๊ณ ๋ ๋ง์ ์์ ์ ์ฒ๋ฆฌ๋ฅผ ํจ. ์ด๋, I/O ๊ณผ์ ์์ ์ ๋ ฅ์ด ๋ค์ด์ค๋ฉด ์ ๋ ฅ์ด ๋๋ฆฌ๋ค๊ณ ๊ฐ์ ํ๋ฉด CPU๋ ์ ๋ ฅ์ ๋ค ๊ธฐ๋ค๋ ค์ผ ํ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํจ. ์ด๋, ๋ฒํผ๋ฅผ ์ด์ฉํ๋ฉด ์ ๋ ฅ๊ณผ์ ์์ ๋ฒํผ์ ์์ ์ ์ฅ์ ํด๋๊ณ , CPU๋ ๊ทธ ์ฌ์ด์ ๋ค๋ฅธ ์ผ์ ์ฒ๋ฆฌํ๋ ๋๋์. ๊ทธ๋ฆฌ๊ณ ์ ๋ ฅ์ด ๋๋๋ฉด ๋ฒํผ์์ ์ ๋ ฅ์ ๋ฐ์ ์ถ๋ ฅ์ ๋ฐ๋ก ํด์ฃผ๋ ๋ฐฉ์
< ๋ฒํผ ์ฌ์ฉ o >
[์ฌ๋ ์
๋ ฅ] →→→ [๋ฒํผ] →→→ [์๋ฐ ํ๋ก๊ทธ๋จ]
(๋๋ฆผ) (์์ ์ ์ฅ) (๋น ๋ฆ)
< ๋ฒํผ ์ฌ์ฉ x >
[์ฌ๋ ์
๋ ฅ] →→→ [์๋ฐ ํ๋ก๊ทธ๋จ]
→ ๋๋ ค์ ์๋ฐ๊ฐ ๊ณ์ ๊ธฐ๋ค๋ ค์ผ ํจ
- ๋ฒํผ๋ฅผ ์ฌ์ฉํ๋ฉด ์ง์ฐ ํ์์ ๋ฐฉ์งํ ์ ์์
- ํ๋ก๊ทธ๋จ์ ๋ฒํผ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ์(์ ๋ ฅ)
- ํ๋ก๊ทธ๋จ์ ๋ฒํผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํจ(์ถ๋ ฅ)
โ 7. Buffer ํด๋์ค
import java.nio.ByteBuffer;
public class ByteBufferExample {
public static void main(String[] args) {
// 1. ๋ฒํผ ์์ฑ (์ฉ๋ 10๋ฐ์ดํธ)
ByteBuffer buffer = ByteBuffer.allocate(10);
// 2. ๋ฐ์ดํฐ ์ฐ๊ธฐ (put)
buffer.put((byte) 10);
buffer.put((byte) 20);
buffer.put((byte) 30);
// 3. ์ฝ๊ธฐ ์ ์ ๋ฒํผ๋ฅผ '์ฝ๊ธฐ ๋ชจ๋'๋ก ์ ํ (flip)
buffer.flip();
// 4. ๋ฐ์ดํฐ ์ฝ๊ธฐ (get)
while (buffer.hasRemaining()) {
byte b = buffer.get();
System.out.println("์ฝ์ ๊ฐ: " + b);
}
}
}
- ๋ฒํผ๋ ๊ธฐ๋ณธํ ๊ฐ์ ์ ์ฅํ๋ ๋ฐ์ดํฐ ๋ณด๊ด์๋ฅผ ์๋ฏธํ๋ฉฐ, ์ฑ๋ ์ ์ถ๋ ฅ์ ์ฌ์ฉ๋๋ฉฐ ๋ฒํผ ๋จ์๋ก ์ ์ถ๋ ฅํ ์ ์์.
- ๊ธฐ๋ณธ Stream IO๋ 1๋ฐ์ดํธ ๋๋ 1๋ฌธ์์ฉ ์ ์ถ๋ ฅ์ ํ๋๋ฐ, Channel IO๋ ๋ฒํผ ๋จ์๋ก ์ ์ถ๋ ฅ์ ํจ.
- Buffer๋ ์ถ์ ํด๋์ค๋ก java.nio ํจํค์ง์ ์กด์ฌํจ ( ์์ ํด๋์ค์์ ๊ตฌํํด์ผ ํ ๊ณตํต์ ๋ฉ์๋๋ฅผ ์ ์ธํจ )
- ์ค์ ์ฌ์ฉ์ ์ํด boolean์ ์ ์ธํ ๋ชจ๋ ๊ธฐ๋ณธํ์ ๋ํด Buffer์ ์๋ธ ํด๋์ค๊ฐ ์กด์ฌํจ
- ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer
- ๊ฐ์ฅ ์ผ๋ฐ์ ์ธ๊ฑด ByteBufferํด๋์ค์.
(1) Buffer ํด๋์ค ์์ฑ ๋ฐฉ๋ฒ ์ข ๋ฅ
// ํ ๋ฉ๋ชจ๋ฆฌ์ ๋ฒํผ ์์ฑ (์๋ฐ ๋ด๋ถ ๋ฉ๋ชจ๋ฆฌ)
ByteBuffer buffer = ByteBuffer.allocate(1024);
// ๋ค์ด๋ ํธ ๋ฉ๋ชจ๋ฆฌ์ ๋ฒํผ ์์ฑ (OS ๋ค์ดํฐ๋ธ ๋ฉ๋ชจ๋ฆฌ)
ByteBuffer directBuffer = ByteBuffer.allocateDirect(1024);
// ๊ธฐ์กด ๋ฐ์ดํธ ๋ฐฐ์ด์ ๊ฐ์ธ์ ๋ฒํผ๋ก ์ฌ์ฉ
byte[] data = new byte[10];
ByteBuffer buffer = ByteBuffer.wrap(data);
(2) Buffer ํด๋์ค ๋ด๋ถ์ ์ธ 4๊ฐ์ง ์ฃผ์ ์์ฑ
- ํด๋์ค ๋ด๋ถ ์ํ๊ฐ ์ฆ, ํ๋์ ๊ฐ์ ์๋ฏธํ๋ฉฐ, ๋ฒํผ ๊ฐ์ฒด๊ฐ ์์ฒด์ ์ผ๋ก ๊ด๋ฆฌํ๋ ์ํ ์ ๋ณด์ด๋ค.
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.capacity() // 10
buffer.position() // 0
buffer.limit() // 10
- ์ด๋ฐ์์ผ๋ก ํ๋๊ฐ์ ๋ด๊ธฐ๊ฒ ๋จ
(3) Buffer ํด๋์ค ์ฃผ์ ๋ฉ์๋
โ 8. FileChannel ํด๋์ค
try (FileChannel channel = FileChannel.open(Paths.get("data.txt"), StandardOpenOption.READ)) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
channel.read(buffer);
buffer.flip();
while (buffer.hasRemaining()) {
System.out.print((char) buffer.get());
}
}
- java.io ํจํค์ง์ ํ์ผ ๊ด๋ จ ์ ์ถ๋ ฅ ์คํธ๋ฆผ์ ๋์ฒดํจ.
- java.nio.channels ํจํค์ง์ ์กด์ฌํจ.
- ํ์ผ์ ๋ํ ์ฝ๊ธฐ์ ์ฐ๊ธฐ๋ฅผ ๋ชจ๋ ์ ๊ณตํจ.
- ๋ฉํฐ ์ค๋ ๋ ํ๊ฒฝ์์๋ ์์ ํ๊ฒ ์ฌ์ฉํ ์ ์์.
- FileInputStream, FileOutputStream ๋ณด๋ค ํจ์จ์ ์ผ๋ก ํ์ผ์ ๋ค๋ฃฐ ์ ์๋๋ก ์ค๊ณ๊ฐ ๋์ด์์.
- FileChannel์ Java์์ ๋์ฉ๋ ํ์ผ ์ฒ๋ฆฌ์ ๋ฉ๋ชจ๋ฆฌ ๋งคํ์ ์ํ ํจ์จ์ ์ธ API.
- ByteBuffer์ ํจ๊ป ์ฌ์ฉ๋๋ฉฐ, ๊ธฐ์กด ์คํธ๋ฆผ๋ณด๋ค ํจ์ฌ ์ ์์ค I/O ์ ์ด๊ฐ ๊ฐ๋ฅํจ.
- ์ฃผ๋ก ํ์ผ ๋ณต์ฌ, ๋ก๊ทธ ์ฒ๋ฆฌ, ๋์ฉ๋ ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๋ฑ์ ๋ง์ด ์ฌ์ฉ๋จ.
(1) ๊ฐ์ฒด ์์ฑ ๋ฐฉ๋ฒ
// ์ฝ๊ธฐ ์ ์ฉ
FileChannel channel = FileChannel.open(Paths.get("example.txt"), StandardOpenOption.READ);
// ์ฝ๊ธฐ ๋ฐ ์ฐ๊ธฐ
FileChannel channel = FileChannel.open(Paths.get("example.txt"), StandardOpenOption.READ, StandardOpenOption.WRITE);
(2) ์ฃผ์ ๋ฉ์๋
(3) FileChannel ํด๋์ค ์ฌ์ฉ ์์
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
public class FileChannelReadExample {
public static void main(String[] args) throws Exception { // JVM์๊ฒ ์์ธ ์์
Path path = Path.of("example.txt"); // ์ฝ์ ํ์ผ ๊ฒฝ๋ก
try (FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.READ)) {
ByteBuffer buffer = ByteBuffer.allocate(1024); // ๋ฒํผ ์์ฑ
int bytesRead = fileChannel.read(buffer);
while (bytesRead != -1) {
buffer.flip(); // ๋ ์ด์์ ์์
์ด ์๊ณ ์ฝ๊ธฐ ์์
๋ง ์๋ค๋ฉด ๊ทธ๋ ์ฐ๋ ๋ฉ์๋
while (buffer.hasRemaining()) {
System.out.print((char) buffer.get());
}
buffer.clear();
bytesRead = fileChannel.read(buffer);
}
}
}
}
(4) ์ ๋ฆฌ
- ๊ฒฐ๋ก ์ FileChannel์ java.nio.file.Path์ java.nio.file.Files ํด๋์ค๋ฅผ ํ์ฉํ์ฌ, ๋ ์ ๋ฐํ๊ณ ํจ์จ์ ์ธ ํ์ผ ์ ์ถ๋ ฅ์ ๊ฐ๋ฅํ๊ฒ ๋ง๋ ๊ณ ๊ธ ํ์ผ ์ ๊ทผ ๋๊ตฌ๋ค.
- java.io.File ๊ธฐ๋ฐ์ด ์๋, NIO ๊ธฐ๋ฐ์ Path/Files๋ฅผ ํ์ฉ
- ์ ์ถ๋ ฅ๋ฟ ์๋๋ผ ๋๋ค ์ ๊ทผ, ๋ฉ๋ชจ๋ฆฌ ๋งคํ, ๊ณ ์ฑ๋ฅ ๋ณต์ฌ ๋ฑ๋ ๊ฐ๋ฅ
- ์คํธ๋ฆผ๋ณด๋ค ๋ ์ ๋ฐํ๊ณ ๋ฎ์ ์์ค์ ์ ์ด ๊ฐ๋ฅ
- ์ฝ๊ธฐ, ์ฐ๊ธฐ ๋ ๋ค ๊ฐ๋ฅํ๋ฏ๋ก, Input Output์ด ๋๋์ง ์์ ํด๋์ค์์ฒด๋..
โ 9. WatchService ์ธํฐํ์ด์ค
public class WatchTest {
public static void main(String[] args) throws Exception {
Path dir = Paths.get("watched"); // ๊ฐ์ํ ํด๋ (ํ๋ก์ ํธ ๋ด๋ถ)
WatchService watchService = FileSystems.getDefault().newWatchService();
// watchService์ ์ด๋ฒคํธ ๊ฐ์ ๋์์ผ๋ก ๋ฑ๋ก
// ENTRY_CREATE: ํ์ผ ๋๋ ๋๋ ํ ๋ฆฌ ์์ฑ ์ด๋ฒคํธ ๊ฐ์ง
// ENTRY_DELETE: ์ญ์ ์ด๋ฒคํธ ๊ฐ์ง MODIFY: ์์ ์ด๋ฒคํธ๊ฐ์ง
dir.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
System.out.println("Watching folder: " + dir);
WatchKey key = watchService.take(); // ์ด๋ฒคํธ ๊ธฐ๋ค๋ฆฌ๊ธฐ (ํ ๋ฒ๋ง ๊ฐ์ง)
for (WatchEvent<?> event : key.pollEvents()) {
System.out.println("Event: " + event.kind() + ", File: " + event.context());
}
key.reset();
}
}
- java.nio.file ํจํค์ง์์ ์ ๊ณตํ๋ ํ์ผ ์์คํ ์ ๋ณ๊ฒฝ ์ฌํญ์ ๊ฐ์งํ๋ ์ธํฐํ์ด์ค์ด๋ค.
- ์ฆ, ๋๋ ํ ๋ฆฌ ๋ด ํ์ผ์ ์์ฑ, ์ญ์ , ์์ ๊ฐ์ ์ด๋ฒคํธ๋ฅผ ์ค์๊ฐ์ผ๋ก ๊ฐ์งํ ์ ์๊ฒ ๋์์ฃผ๋ API์ด๋ค.
- ์ด ๊ธฐ๋ฅ์ ํตํด ํ๋ง ์์ด๋ ํ์ผ ์์คํ ์ ๊ฐ์ํ๋ ์ด๋ฒคํธ ๊ธฐ๋ฐ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํจ.
- ๋๋ ํฐ๋ฆฌ๋ฅผ ํํํ๋ Path ๊ฐ์ฒด์ ๋ณํ๋ฅผ ๊ฐ์ง
- ๋๋ ํฐ๋ฆฌ ๋ด์ ํ์ผ ๋๋ ์๋ธ ๋๋ ํฐ๋ฆฌ์ ์์ฑ, ์ญ์ , ์์ ์ ๋ํ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํจ์ ๊ฐ์งํ๋ค๋ ์๋ฏธ์.
- ์์ ์ฝ๋๋ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ๋๊น์ง ๋ธ๋กํน ์ํ๋ก ๋๊ธฐ๋ฅผ ํจ. ( WatchKey key = watchService.take(); )
- ์ด๋ฒคํธ๊ฐ ๊ฐ์ง๋๋ฉด WatchKey๋ฅผ ๋ฐํ
- ์ฆ, ์ด๋ฒคํธ ๋ฐ์ ์ ๊น์ง ํ๋ก๊ทธ๋จ์ด ๋ฉ์ถฐ์ ๊ธฐ๋ค๋ฆฌ๊ฒ ๋จ. ( ๊ฒฐ๋ก ์ ๋ฑ ํ ๋ฒ์ ์ด๋ฒคํธ ๊ฐ์ง ์ฝ๋์. )
- WatchKey๋ ์ด๋ค ๋๋ ํ ๋ฆฌ์์ ์ด๋ค ์ด๋ฒคํธ๋ค์ด ๋ฐ์ํ๋์ง๋ฅผ ๋ด๊ณ ์๋ ์ด์ ๋ก ๋ณผ ์ ์์.
- ๋ฆฌํด์ ํ๋ฉด์ ์ด๋ค ๋๋ ํ ๋ฆฌ, ์ด๋ค ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋์ง๋ฅผ ๋ด์์ ๋ฆฌํดํด์ค์ผ๋ก์จ ์ ์ ์๊ฒ ๋จ.
- ๋ํ, key.pollEvents() ๋ฉ์๋๋ฅผ ์ด์ฉํ๋ฉด, ์ด๋ฒคํธ ํ๋ํ๋์ ๋ํด์ ์์ธํ ๋ฆฌ์คํธ๋ก ๋ฐ์ ์ ์์
- ์ด๋ฒคํธ๋ฅผ ๊ณ์ ์งํํ๊ธฐ ์ํด์๋ boolean valid = key.reset(); ์ ํด ์ค ์ ์์.
์ ๋ฆฌํ๋ฉด
- take() ๋ฉ์๋๋ WatchService๊ฐ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ๋๊น์ง ๋๊ธฐํ๋ ๋ธ๋กํน ๋ฉ์๋์ ๋๋ค. register()์ ๋ช ์ํ ์ด๋ฒคํธ(์์ฑ, ์ญ์ , ์์ ๋ฑ) ์ค ํ๋๋ผ๋ ๋ฐ์ํ๋ฉด, ํด๋น ์ด๋ฒคํธ๋ค์ด WatchKey์ ๋ด๊ฒจ ๋ฐํ๋ฉ๋๋ค. ์ด๋ ๊ฒ ๋ฐํ๋ WatchKey๋ฅผ ํตํด ๋ฐ์ํ ๋ชจ๋ ์ด๋ฒคํธ ์ ๋ณด๋ฅผ ์กฐํํ ์ ์์ต๋๋ค.
- ๊ทธ๋ฆฌ๊ณ , key.reset() ๋ฉ์๋๋ ๋ฐ๋์ ์ํ์ ์์ผ์ค์ผ, ๋ค์ ์ด๋ฒคํธ๋ฅผ ๊ฐ์ํ ์ ์๊ฒ๋จ.