본문 바로가기
java

파일 시스템(NIO)

by improve 2024. 2. 20.

●Paths 와 Path 클래스 

package org.example;

import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) {
        Path path = Paths.get("c:\\javastudy\\PathDemo.java");

        System.out.println(path.getRoot());
        System.out.println(path.getParent());
        System.out.println(path.getFileName());


    }
}

 

Root =  C:

Parent = C:\\ javastudy

FileName = PathDemo.java

 

● 현재경로 

// 현재경로를 상태경로로 작성
Path curpath = Paths.get("");
System.out.println(curpath);

if (curpath.isAbsolute())
    System.out.println(curpath);
else {
    String temp = curpath.toAbsolutePath().toString();
    System.out.println(temp);
}

 

 

                                                         // 이 주소는 절대경로                    
try(FileOutputStream fos = new FileOutputStream("D:\\java_work\\java_work\\ex0220\\aa.dat ");) {
    fos.write(10);                                    // aa.dat 만 적으면 상대 경로       
    fos.write(20);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

 

● 파일 및 디렉토리 의 생성 과 소멸

createFile   윗파일이 없으면 안된다.  중복이 안된다.

createDirectory(파일을 만든다) 윗파일이 없으면 안된다. 중복이 안된다.

createDirectories  없으면 생성해준다. 

 

package org.example;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main2 {
    public static void main(String[] args) {
        Path path = Paths.get("C:\\javastudy\\AA.java");
        Path dp = Paths.get("C:\\javastudy\\EMPTY");
        Path aaa = Paths.get("C\\aa\\bb\\cc");

        // 파일이 없으면 만들어라

        try {
            if (!Files.exists(path)){
                Files.createFile(path);  // 만들어라
            }
            if (!Files.exists(dp)){
                Files.createDirectory(dp); // 파일을 만들어라
            }
            if (!Files.exists(aaa)) {
                Files.createDirectories(aaa); // 모든걸 만들어라 
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

● 바이트 스트림의 생성

package org.example;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main5 {
    public static void main(String[] args) {
        Path fp = Paths.get("nio2.dat");

        try(DataOutputStream dos = new DataOutputStream(Files.newOutputStream(fp))){
            dos.writeInt(10);
            dos.writeDouble(20.2);
        }catch (Exception e){
            e.printStackTrace();
        }

        try(DataInputStream dos = new DataInputStream(Files.newInputStream(fp))) {
            int temp = dos.readInt();
            System.out.println(temp);
            double temp1 = dos.readDouble();
            System.out.println(temp1);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

구성은 비슷하다.

'java' 카테고리의 다른 글

Thread 더 좋은 생성방법  (0) 2024.02.21
Thread 이해 와 생성  (0) 2024.02.21
문자 스트림  (0) 2024.02.19
입출력 필터 스트림  (0) 2024.02.19
I/O 스트림에 대한 이해  (0) 2024.02.19