[Day 1] Java 程序员修炼之道学习笔记

  1. 二进制文本
    让 int x 用位模式表示十进制值102

    1
    2
    3
    4
    //Java 7 之前的写法:
    int x = Integer.parseInt("1100110",2);
    //Java 7 的写法:
    int x = 0b1100110;
  2. 数字中的下划线

    1
    long tmp=1_000_000;
  3. final重抛

    1
    2
    3
    4
    5
    6
    7
    try {
    doSomethingWhichMightThrowIOException();
    doSomethingElseWhichMightThrowSQLException();
    } catch (final Exception e) {
    ...
    throw e;
    }
  4. Try-with-resources (TWR)

    1
    2
    3
    4
    5
    6
    7
    8
    try (OutputStream out = new FileOutputStream(file);
    InputStream is = url.openStream() ) {
    byte[] buf = new byte[4096];
    int len;
    while ((len = is.read(buf)) > 0) {
    out.write(buf, 0, len);
    }
    }
  5. Java 7钻石语法

    1
    2
    3
    4
    //不用钻石语法
    Map<Integer, Map<String, String>> usersLists =new HashMap<Integer, Map<String, String>>();
    //使用钻石语法,上面语句少了j近一半字符
    Map<Integer, Map<String, String>> usersLists = new HashMap<>();
  6. Java 7处理符号链接

    1
    2
    3
    4
    java.nio.file.Path path = java.nio.file.Paths.get("/ftp/upload/1.jpg");
    java.nio.file.Path path2 = java.nio.file.Paths.get("/ftp/upload/2.jpg").toRealPath();
    System.out.println(path.toRealPath());
    System.out.println(path2.toString());

其中:

1
2
-rwxrwxrwx 1 lau  lau  191221  8月 16  2014 1.jpg*
lrwxrwxrwx 1 root root 17 8月 11 12:23 2.jpg -> /ftp/upload/1.jpg*

输出:

1
2
/ftp/upload/1.jpg
/ftp/upload/1.jpg

  1. Java 7在目录中查找文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    // Java 7在目录中查找文件
    java.nio.file.Path path = java.nio.file.Paths
    .get("F:\\Pictures\\WeixinPictures");
    try (java.nio.file.DirectoryStream<java.nio.file.Path> stream =
    java.nio.file.Files.newDirectoryStream(path, "*jpg")) {
    for (java.nio.file.Path path2 : stream) {
    System.out.println(path2.getFileName());

    }
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
  2. Java 7遍历目录树

    1
    2
    3
    4
    5
    6
    7
    8
    // Java 7遍历目录树
    java.nio.file.Path startingDir = java.nio.file.Paths.get("F:\\Pictures");

    try {
    java.nio.file.Files.walkFileTree(startingDir, new FindJPGVisitor());
    } catch (IOException e) {
    System.out.println(e.getMessage());
    }
  3. NIO.2的文件系统I/O

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    //创建文件
    Path target = Paths.get("D:\\Backup\\MyStuff.txt");
    Path file = Files.createFile(target);

    //创建文件,带权限
    Path target = Paths.get("D:\\Backup\\MyStuff.txt");
    Set<PosixFilePermission> perms =
    PosixFilePermissions.fromString("rw-rw-rw-");
    FileAttribute<Set<PosixFilePermission>> attr =
    PosixFilePermissions.asFileAttribute(perms);
    Files.createFile(target, attr);

    //复制文件
    Path source = Paths.get("C:\\My Documents\\Stuff.txt");
    Path target = Paths.get("D:\\Backup\\MyStuff.txt");
    Files.copy(source, target);

    //删除文件
    Path target = Paths.get("D:\\Backup\\MyStuff.txt");
    Files.delete(target);

    //符号链接
    Path file = Paths.get("/ftp/upload/2.jpg");
    try{
    if(Files.isSymbolicLink(file)){
    file = Files.readSymbolicLink(file);
    }
    Files.readAttributes(file, BasicFileAttributes.class);
    }
    catch (IOException e){
    System.out.println(e.getMessage());
    }

    //打开文件,注意编码
    Path logFile = Paths.get("/tmp/app.log");
    try (BufferedReader reader =
    Files.newBufferedReader(logFile, StandardCharsets.UTF_8)) {
    String line;
    while ((line = reader.readLine()) != null) {
    //...
    }
    }

    //写入文件
    Path logFile = Paths.get("/tmp/app.log");
    try (BufferedWriter writer =
    Files.newBufferedWrite(logFile, StandardCharsets.UTF_8,
    StandardOpenOption.WRITE)) {
    writer.write("Hello World!");
    //...
    }

    //简化读取和写入,没必要再用while循环把数据从字节数组读到缓冲区里
    Path logFile = Paths.get("/tmp/app.log");
    List<String> lines = Files.readAllLines(logFile, StandardCharsets.UTF_8);
    byte[] bytes = Files.readAllBytes(logFile);