我最近一直专注于Clojure作为系统脚本的可能功能语言。直到我意识到将JVM置于底层意味着我只能使用Java的能力。
那么,我该如何创建符号链接?还是硬链接?我的意思是没有(sh "ln" ...)
。
答案 0 :(得分:3)
为了现在找到此问题的任何人的利益,在Java SE 7中,您可以使用java.nio.file.Files包来创建链接。我认为这就是MichałMarczyk在his comment中提到的关于未来的内容。
public static Path createLink(Path link,
Path existing)
throws IOException
public static Path createSymbolicLink(Path link,
Path target,
FileAttribute<?>... attrs)
throws IOException
来自java docs教程的This page提供了这些示例。
Path newLink = ...;
Path target = ...;
try {
Files.createSymbolicLink(newLink, target);
} catch (IOException x) {
System.err.println(x);
} catch (UnsupportedOperationException x) {
// Some file systems do not support symbolic links.
System.err.println(x);
}
Path newLink = ...;
Path existingFile = ...;
try {
Files.createLink(newLink, existingFile);
} catch (IOException x) {
System.err.println(x);
} catch (UnsupportedOperationException x) {
// Some file systems do not
// support adding an existing
// file to a directory.
System.err.println(x);
}
答案 1 :(得分:2)
sh
选项是不是很糟糕,实际上......话虽如此,Ant提供了一个符号链接任务,您可以通过Lancet以合理的Clojure友好方式使用它,最初由Ant引入Stuart Halloway在他的“Programming Clojure”一书中,目前由Leiningen在内部使用。如果您想在Leiningen中查看符号链接任务,请查看1.3.0版本中的this line。
答案 2 :(得分:2)
您可以使用Java Native Access访问目标主机上的本机库。
简单示例:
(ns jna-test
(:import
(com.sun.jna Native Function)))
(defn symlink [oldpath newpath]
(-> (Function/getFunction "c" "symlink")
(.invoke Integer (to-array [oldpath newpath]))))
还有可用的Clojure包装: clj-native和 clojure-jna
答案 3 :(得分:0)
这是一个侧面说明:
当使用Clojure进行系统命令时,如果使用cake而不是leiningen,它可以使用持久性JVM,因此每次运行命令时都不必等待三秒钟让JVM启动。
*蛋糕不是很稳定所以有些日子它的效果比其他人好得多。 (截至2010年9月)
答案 4 :(得分:0)
您可以利用这个适合我的库https://github.com/ToBeReplaced/nio.file
$result = $db_content->query()->fetchAll();
$row_array = $result->toArray();
需要注意相关权限。例如。要在Emacs / Cider环境下的Windows 7中运行,需要从管理命令窗口启动emacs,否则会报告“FileSystemException java.nio.file.FileSystemException:c:\ tmp \ myFolder \ test.txt:A客户不持有所需的特权“
答案 5 :(得分:0)
import requests, sys, bs4
print('Googling...')
res = requests.get('http://google.com/search?q=' +' '.join(sys.argv[1:]))
print(res.raise_for_status())
soup = bs4.BeautifulSoup(res.text, 'html5lib')
linkElems = soup.select(".r a")
print(linkElems)
这就是我想出的。