1

如何从 Eclipse 中的条件 JAVA 断点表达式中读取文件的内容?

当我的字符串变量的值包含“收益”时,我的断点条件为真:

myVariable != null && myVariable.contains("earnings")

我想要一个条件断点来在为真时替换字符串的内容;但是,我只能通过等待 JVM 中断(在断点处)然后显示以下块(Ctrl+Shift+D)来做到这一点。该块将文件的内容读入myVariable.

String sCurrentLine, fileName = "modified-earnings.xml";

java.lang.StringBuffer sb = new java.lang.StringBuffer();
java.io.FileReader fr = new java.io.FileReader(fileName);

java.io.BufferedReader br = new java.io.BufferedReader(fr);
while ((sCurrentLine = br.readLine()) != null) {
    sb.append(sCurrentLine + System.getProperty("line.separator"));
}
fr.close();
// this is where my variable gets the new value from a file
myVariable = sb.toString();
br.close();

如您所见,每次我想要替换字符串的值时都必须评估行有点麻烦,所以我希望调试器为我做这件事,使用与我的断点完全相同的条件性质。注意附加的(最后一个)布尔表达式(myVariable = magicCode("modified-earnings.xml")) != null

myVariable != null && myVariable.contains("earnings") && (myVariable = magicCode("modified-earnings.xml")) != null

我缺少的是不能编码的 magicCode 函数,因为我不能(重复不能)更改代码或将新的 JARS 添加到类路径中。理想情况下,系统 API 会这样做:

myVariable = StreamToString.do(System.getResourceAsStream("modified-earnings.xml"))
4

1 回答 1

0

对于Java 8
这个 eclipse 断点条件代码检查hello文件中的内容c:/temp/hello.txt

"hello".equals(new String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get("c:/temp/hello.txt"))))
于 2013-05-23T13:29:21.660 回答