在我的Maven项目中,我在main方法中有以下代码:
FileInputStream in = new FileInputStream("database.properties");
Run Code Online (Sandbox Code Playgroud)
但总是得到一个文件未找到错误.
我已将文件放在src/main/resources中,并将其正确复制到target/classes目录(我相信这是Maven资源的预期行为),但在实际运行程序时,它似乎永远无法找到该文件.我尝试了其他各种途径:
FileInputStream in = new FileInputStream("./database.properties");
FileInputStream in = new FileInputStream("resources/database.properties");
Run Code Online (Sandbox Code Playgroud)
等似乎没什么用.
那么正确的使用途径是什么?
根据下面的"disown's"答案,我需要的是:
InputStream in = TestDB.class.getResourceAsStream("/database.properties")
Run Code Online (Sandbox Code Playgroud)
哪个TestDB是类的名称.
谢谢你的帮助,谢绝!
我刚刚完成"关于JVM并发编程",由Venkat廉读,在这本书中,作者以作为他的一个例子,在一个目录树计数的文件大小.他展示了不使用并发,使用队列,使用锁存器和使用scala actor的实现.在我的系统上,当遍历我的/ usr目录(OSX 10.6.8,Core Duo 2 Ghz,Intel G1 ssd 160GB)时,所有并发实现(队列,latch和scala actor)都能在9秒内运行.
我正在学习Clojure,并决定使用代理将Scala actor版本移植到Clojure.不幸的是,我的平均时间是11-12秒,这明显慢于其他人.花了DAYS把我的头发拉出来之后,我发现下面的代码是罪魁祸首(processFile是我发送给文件处理代理的一个函数:
(defn processFile
[fileProcessor collectorAgent ^String fileName]
(let [^File file-obj (File. ^String fileName)
fileTotals (transient {:files 0, :bytes 0})]
(cond
(.isDirectory file-obj)
(do
(doseq [^File dir (.listFiles file-obj) :when (.isDirectory dir)]
(send collectorAgent addFileToProcess (.getPath dir)))
(send collectorAgent tallyResult *agent*)
(reduce (fn [currentTotal newItem] (assoc! currentTotal :files (inc (:files currentTotal))
:bytes (+ (:bytes currentTotal) newItem)))
fileTotals
(map #(.length ^File %) (filter #(.isFile ^File %) (.listFiles file-obj))))
(persistent! fileTotals)) …Run Code Online (Sandbox Code Playgroud) 我一直在考虑使用C的基本数据类型(例如使用CERT C编码标准)更安全地使用数学运算的方法.到目前为止,我想出了类似的东西:
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#define safe_add(x, y) _Generic((x + y), \
unsigned int: safe_add_uint, \
unsigned long: safe_add_ulong \
)(x, y, __FILE__, __LINE__)
unsigned long
safe_add_ulong(unsigned long x, unsigned long y,
const char* filename, int line_num)
{
if (x < ULONG_MAX - y)
return x + y;
else {
fprintf(stderr,
"Integer wrap-around occurred, File: %s, Line: %d\n",
filename, line_num);
exit(EXIT_FAILURE);
}
}
unsigned int
safe_add_uint(unsigned int x, unsigned int y,
const char* filename, int …Run Code Online (Sandbox Code Playgroud) 刚开始学习Groovy,得到了PragProg的书"Programming Groovy",并且在编译其中一个示例脚本时遇到了问题:
class GCar2 {
final miles = 0
def getMiles() {
println "getMiles called"
miles
}
def drive(dist) {
if (dist > 0) {
miles += dist
}
}
}
def car = new GCar2()
println "Miles: $car.miles"
println 'Driving'
car.drive(10)
println "Miles: $car.miles"
try {
print 'Can I see the miles? '
car.miles = 12
} catch (groovy.lang.ReadOnlyPropertyException ex) {
println ex.message
Run Code Online (Sandbox Code Playgroud)
GroovyCar2.groovy: 20: cannnot access final field or property outside of constructor.
@ line 20, column 35. …Run Code Online (Sandbox Code Playgroud)