在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ java如何根據(jù)路徑生成文件夾呀?

java如何根據(jù)路徑生成文件夾呀?

比如說路徑是/csrSystemXYK/src/com/sunyard/csr/action/
則以此創(chuàng)建文件夾?

回答
編輯回答
筱饞貓

建議使用Path類操作文件,涉及文件監(jiān)聽事件、文件權(quán)限等高級(jí)特性
下面是隨便在博客上找的,東西比較雜,還需要你自己看API

package filespaths;
import org.junit.Test;
import java.io.*;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
/**
 * @Author kingboy
 * @Date 2017/4/13 11:05
 * @Description Path is used to Path Sample
 * @email kingboyworld@163.com
 */
public class PathTest {
  private static String separator = File.separator;
  /**
   * 構(gòu)建Path
   */
  @Test
  public void constructon(){
    //1.Paths
    Path path = Paths.get("/Users/kingboy/Desktop/");
    Path path1 = Paths.get(URI.create("/Users/kingboy/Desktop/"));
    //2.FileSystems
    Path path2 = FileSystems.getDefault().getPath("/Users/kingboy/Desktop/");
    //3.File
    Path path3 = new File("/Users/kingboy/Desktop/").toPath();
  }
  /**
   * 創(chuàng)建一個(gè)空文件/文件夾
   * @throws IOException
   */
  @Test
  public void create() throws IOException {
    //文件夾
    Path path = Paths.get("/Users/kingboy/Desktop/hello");
    if(!Files.exists(path)){
      Files.createDirectory(path);
      //創(chuàng)建多個(gè)目錄
      //Files.createDirectories(path);
    }
    //文件
    Path path1 = Paths.get("/Users/kingboy/Desktop/helloFile");
    if(Files.exists(path1)){
      Files.createFile(path1);
    }
  }
  /**
   * 文件屬性
   */
  @Test
  public void getFileProperties() throws IOException {
    Path path = Paths.get("/Users/kingboy/Desktop/text.txt");
    System.out.println(Files.getLastModifiedTime(path));//最后修改時(shí)間
    System.out.println(Files.getOwner(path));//擁有者
    System.out.println(Files.getPosixFilePermissions(path));//權(quán)限
    System.out.println(Files.size(path));//文件大小
  }
  /**
   * 讀取一個(gè)文本文件
   */
  @Test
  public void readText() throws IOException {
    Path path = Paths.get("/Users/kingboy/Desktop/text.txt");
    //通過bufferedReader讀取
    BufferedReader bufferedReader = Files.newBufferedReader(path, StandardCharsets.UTF_8);//文件編碼
    StringBuilder sb = new StringBuilder();
    String tempString = null;
    while ((tempString = bufferedReader.readLine())!=null){
      sb = sb.append(tempString);
    }
    System.out.println(sb);
    //通過Files方法readAllLines
    List<String> strings = Files.readAllLines(path);
    strings.forEach(s -> System.out.print(s));
    //輸出結(jié)果
    //adsfasdfasdfadsfasdfgsdfsdffsdfsdf
    //adsfasdfasdfadsfasdfgsdfsdffsdfsdf
  }
  /**
   * 拿到文件輸入流
   * @throws IOException
   */
  @Test
  public void getInputStream() throws IOException {
    Path path = Paths.get("/Users/kingboy/Desktop/text.txt");
    InputStream inputStream = Files.newInputStream(path);
  }
  /**
   * 文件寫操作
   */
  @Test
  public void writeFile() throws IOException {
    Path path = Paths.get("/Users/kingboy/Desktop/writeFile");
    BufferedWriter bufferedWriter = Files.newBufferedWriter(path);
    String str = "write file test";
    bufferedWriter.write(str);
    bufferedWriter.flush();
    bufferedWriter.close();
  }
  /**
   * 遍歷一個(gè)文件夾
   */
  @Test
  public void traverseDirectory() throws IOException {
    Path path = Paths.get("/Users/kingboy/Desktop/");
    Stream<Path> list = Files.list(path);
    list.forEach(p -> {
      System.out.println(p.getFileName());
    });
  }
  /**
   * 遍歷文件樹
   */
  @Test
  public void traverseTree() throws IOException {
    Path path = Paths.get("/Users/kingboy/Desktop/");
    Stream<Path> walk = Files.walk(path);
    walk.forEach(path1 -> {
//      System.out.println(path1.getRoot());//根目錄
      System.out.println(path1.getFileName());//文件名
//      System.out.println(path1.getParent());//上級(jí)目錄
//      System.out.println(path1.getFileSystem());//文件系統(tǒng)
    });
    //還有種方式Files.walkFileTree()
  }
  /**
   * 文件復(fù)制
   */
  @Test
  public void copyFile() throws IOException {
    Path path = Paths.get("/Users/kingboy/Desktop/text.txt");
    Path path2 = Paths.get("/Users/kingboy/Desktop/hello.txt");
    Files.copy(path,path2);
  }
  /**
   * 讀取權(quán)限見上面示例,設(shè)置權(quán)限
   */
  @Test
  public void writePermission() throws IOException {
    Path path = Paths.get("/Users/kingboy/Desktop/text.txt");
    Set<PosixFilePermission> permissionSet = new HashSet<>();
    permissionSet.add(PosixFilePermission.GROUP_WRITE);
    permissionSet.add(PosixFilePermission.OWNER_EXECUTE);
    Files.setPosixFilePermissions(path,permissionSet);
  }
  //還有很多其他操作Api,自己查看方法名,很容易就能分辨出功能。
 }
2017年9月30日 20:27
編輯回答
憶往昔
    // 注意路徑為全路徑
    File file = new File("/csrSystemXYK/src/com/sunyard/csr/action/");

    if (!file.exists())
    {
        file.mkdir();
    }
2017年7月6日 16:11