상세 컨텐츠

본문 제목

[입출력] 디렉토리에 폴더만들고 글썼다가 지우는거

자바

by esoesmio 2023. 4. 10. 19:43

본문

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class z31 {
    File directory;

    public static void main(String[] args) {
        try {
            z31 z = new z31();

            z.directory = new File("/Users/eunsukkim/Desktop/copy2");
            File newfile = new File("/Users/eunsukkim/Desktop/kkk.txt");


            if(!z.directory.exists()){
                z.directory.mkdir();
            }

            if(!newfile.exists()){
                newfile.createNewFile();
            }


            String zz = "나는시발\n머하는거냐";
            Path path = Paths.get("/Users/eunsukkim/Desktop/kkk.txt");
            Files.writeString(path,zz,StandardCharsets.UTF_8);


            System.out.println(Files.probeContentType(path));
            System.out.println(Files.size(path));

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }



            String content = Files.readString(path,Charset.defaultCharset());
            System.out.println(content);

            File dir = new File("/Users/eunsukkim/Desktop");
            File deletefile = new File("/Users/eunsukkim/Desktop/kkk.txt");

            File[] filearr = dir.listFiles();

            for(File f : filearr){

                System.out.println(f.getName());
            }

            z.directory.delete();
            boolean isdeleted = deletefile.delete();

            if(isdeleted){
                System.out.println("지워짐");
            }




        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

}

관련글 더보기

댓글 영역