Category: virtualization

虚拟化技术

  • java实现move操作

    java实现move操作

    这类操作与文件所在的文件系统息息相关.

    File的renameTo一般只用于同级目录修改文件名。

    想要多级目录移动文件还得用Files.move方法。

    同一个文件系统下会进行move操作

    不同文件系统,则通过拷贝-删除的方式实现move。

    package io.move;
    
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.StandardCopyOption;
    
    /**
     * move-test<br/>
     * ├── dir1<br/>
     * │   └── child<br/>
     * │       └── hui3<br/>
     * └── hui1<br/>
     * <p>
     * move-test<br/>
     * ├── dir1<br/>
     * │   └── child<br/>
     * │       └── hui3<br/>
     * └── hui2<br/>
     */
    public class MoveTest {
        public static void main(String[] args) {
    
            String root = "/Users/ephuizi/move-test";
    
            Path test1 = Paths.get(root, "hui1");
            Path test2 = Paths.get(root, "hui2");
    
            File test1File = test1.toFile();
            File test2File = test2.toFile();
            //同文件系统,同级目录下,文件重命名
            test1File.renameTo(test2File);
    
            Path test3 = Paths.get(root, "dir1");
            Path test4 = Paths.get(root, "dir2");
    
            try {
                //多级目录下文件move
                mvoeDir(test3.toFile(), test4.toFile());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private static void mvoeDir(File from, File to) throws IOException {
    
            File[] children = from.listFiles();
            for (File child : children) {
                Path target = Paths.get(to.getPath(), child.getPath().substring(from.getPath().length()));
                if (child.isDirectory()) {
                    File targetFile = target.toFile();
                    if (!targetFile.exists()) {
                        Files.move(child.toPath(), target);
                    } else {
                        if (targetFile.isFile()) {
                            targetFile.delete();
                        }
                        mvoeDir(child, targetFile);
                    }
                } else if (child.isFile()) {
                    //
                    Files.move(child.toPath(), target, StandardCopyOption.REPLACE_EXISTING);
                }
            }
        }
    
        private static void delDir(File dir) {
            File[] children = dir.listFiles();
            for (File child : children) {
                if (child.isDirectory())
                    delDir(child);
                else
                    child.delete();
            }
        }
    }
    

     

  • LIBRARY_PATH和LD_LIBRARY_PATH

    LIBRARY_PATH和LD_LIBRARY_PATH是Linux下的两个环境变量,二者的含义和作用分别如下

    LIBRARY_PATH环境变量用于在程序编译期间查找动态链接库时指定查找共享库的路径,例如,指定gcc编译需要用到的动态链接库的目录。设置方法如下(其中,LIBDIR1和LIBDIR2为两个库目录):

    export LIBRARY_PATH=LIBDIR1:LIBDIR2:$LIBRARY_PATH

    LD_LIBRARY_PATH环境变量用于在程序加载运行期间查找动态链接库时指定除了系统默认路径之外的其他路径,注意,LD_LIBRARY_PATH中指定的路径会在系统默认路径之前进行查找。设置方法如下(其中,LIBDIR1和LIBDIR2为两个库目录):

    export LD_LIBRARY_PATH=LIBDIR1:LIBDIR2:$LD_LIBRARY_PATH

    来自:LIBRARY_PATH和LD_LIBRARY_PATH环境变量的区别

  • go函数返回数组

    Defining a function that returns a slice of variable size in golang

    First of all, slices are already of “variable size”: [100]int and […]int are array type definitions.
    []int is the correct syntax for a slice, and you could implement the function as:

    func BuildSlice(size int) []int {
        return make([]int, size)
    }
    The built-in function make takes a type T, which must be a slice, map or channel type, optionally followed by a type-specific list of expressions. It returns a value of type T (not *T). The memory is initialized as described in the section on initial values.
    
    Call             Type T     Result
    
    make(T, n)       slice      slice of type T with length n and capacity n
    make(T, n, m)    slice      slice of type T with length n and capacity m
    
    The size arguments n and m must be of integer type or untyped. A constant size argument must be non-negative and representable by a value of type int. If both n and m are provided and are constant, then n must be no larger than m. If n is negative or larger than m at run time, a run-time panic occurs.
    
    s := make([]int, 10, 100)       // slice with len(s) == 10, cap(s) == 100
    s := make([]int, 1e3)           // slice with len(s) == cap(s) == 1000
    s := make([]int, 1<<63) // illegal: len(s) is not representable by a value of type int s := make([]int, 10, 0) // illegal: len(s) > cap(s)
    
    func obtainNext(str string) []int {
       sLen := len(str)
       next := make([]int, sLen)
       next[0] = -1
       k, j := -1, 0
       for j < sLen-1 {
          if k == -1 || str[j] == str[k] {
             k++
             j++
             next[j] = k
          } else {
             k = next[k]
          }
       }
       return next
    }
  • Hello world!

    Welcome to WordPress. This is your first post. Edit or delete it, then start writing!