简体   繁体   English

在不创建变量的情况下,从Eclipse中的条件JAVA断点的表达式中读取文件的内容

[英]Read a file's content from the expression of a conditional JAVA breakpoint in Eclipse without creating variables

How do you read the content of a file from the expression of a conditional JAVA breakpoint in Eclipse? 您如何从Eclipse中的条件JAVA断点的表达式中读取文件的内容?

My breakpoint's condition is true when the value of my string variable contains 'earnings': 当我的字符串变量的值包含“收入”时,我的断点条件为true:

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

I want a conditional breakpoint to replace the content of a string when true; 我希望有条件断点在true时替换字符串的内容; however, I have only been able to do so by waiting the JVM to break (at the breakpoint) and then having the following block displayed (Ctrl+Shift+D). 但是,我只能通过等待JVM中断(在断点处)然后显示以下代码块(Ctrl + Shift + D)来做到这一点。 The block reads the content of the file into myVariable . 该块将文件的内容读取到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();

As you see, having to evaluate the lines every time that I want to replace the value of the string is a little bit of a hassle, so I want the debugger to do it for me, using the very same conditional nature of my breakpoint. 如您所见,每次要替换字符串的值时都必须评估行,这有点麻烦,因此我希望调试器使用断点的相同条件为我自己做。 Note the additional (last) boolean expression (myVariable = magicCode("modified-earnings.xml")) != null 注意附加的(最后一个)布尔表达式(myVariable = magicCode("modified-earnings.xml")) != null

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

What I'm missing is the magicCode function that can not code, since I can not (repeat can not) change the code or add new JARS to the classpath. 我缺少的是无法编码的magicCode函数,因为我无法(重复无法)更改代码或将新的JARS添加到类路径。 Ideally, a System API would do it: 理想情况下,系统API可以做到:

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

For Java 8 , 对于Java 8
this eclipse breakpoint conditional code checks for content hello in file c:/temp/hello.txt . 该蚀断点条件代码检查文件c:/temp/hello.txt内容hello

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM