2013-05-21 30 views
1

如何从Eclipse中的条件JAVA断点表达式读取文件的内容?从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功能,可以不代码,因为我不能(重复也不能)修改代码或添加新的jar添加到类路径。理想情况下,系统API会做到这一点:

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

回答

0

对于的Java 8
这次日食断点条件代码检查文件内容c:/temp/hello.txthello

"hello".equals(new String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get("c:/temp/hello.txt"))))