Java String拆分换行符

时间:2015-04-11 16:50:59

标签: java regex string split

我使用ByteArrayInputStream并使用Stringbuilder创建一个完整的字符串(sb)。

然后我把sb移动到一个字符串字段,然后我尝试在换行符上拆分:Split Java String by New Line

我在

上遇到错误
 String configLines[] = tempL.split("\\r?\\n");

行如下:

depends on configLines is neither defined in the script nor in dependencies."

这是代码中唯一提到configLines的地方(到目前为止)。任何想法这一行代码有什么问题?它应该工作不应该吗?

当我注释掉这一行时代码有效,当我取消注释它时 - 它失败了。

我看过字节代码,字节是13,10,换行,回车。

我做错了什么?提前谢谢。

ByteArrayInputStream str = new ByteArrayInputStream(documentData);
StringBuilder sb = new StringBuilder();

int ch;
while((ch = str.read()) != -1) {
    sb.append(Character.toUpperCase((char) ch));
}
if(debug){dI++; logger.severe(dI+thisTrace+"sb: "+sb.toString());}

 String tempL = sb.toString();
 if(debug){dI++; logger.severe(dI+thisTrace+"tempL: "+tempL);}

 String configLines[] = tempL.split("\\r?\\n");

完整的错误消息如下所示,它来自一个groovy解释器,也是java。

2015-04-11 19:17:39 org.bonitasoft.engine.execution.work.FailureHandlingBonitaWork 
SEVERE: THREAD_ID=377 | HOSTNAME=Mainframe | TENANT_ID=1 | org.bonitasoft.engine.expression.exception.SExpressionEvaluationException : "PROCESS_DEFINITION_ID=7620684800960500232 | PROCESS_NAME=Configure |     PROCESS_VERSION=3.0 | PROCESS_INSTANCE_ID=18001 | ROOT_PROCESS_INSTANCE_ID=18001  | FLOW_NODE_DEFINITION_ID=-7446352707110997787 | FLOW_NODE_INSTANCE_ID=360005 |     FLOW_NODE_NAME=prepareChosenConfigFile | CONNECTOR_DEFINITION_IMPLEMENTATION_CLASS_NAME=prepareChosenConfigFile | CONNECTOR_INSTANCE_ID=340004 | Expression prepareChosenConfigFile with content = </**

This is a print out of the code module as above.

*/> depends on lines is neither defined in the script nor in dependencies."

org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: PROCESS_DEFINITION_ID=7620684800960500232 | PROCESS_NAME=Configure | PROCESS_VERSION=3.0 | PROCESS_INSTANCE_ID=18001 | ROOT_PROCESS_INSTANCE_ID=18001 | FLOW_NODE_DEFINITION_ID=-7446352707110997787 | FLOW_NODE_INSTANCE_ID=360005 | FLOW_NODE_NAME=prepareChosenConfigFile | CONNECTOR_DEFINITION_IMPLEMENTATION_CLASS_NAME=prepareChosenConfigFile | CONNECTOR_INSTANCE_ID=340004 | Expression prepareChosenConfigFile with content = </**

This is a print out of the code module as above.

*/> depends on lines is neither defined in the script nor in dependencies."
at     org.bonitasoft.engine.expression.impl.GroovyScriptExpressionExecutorCacheStrategy.evaluate(GroovyScriptExpressionExecutorCacheStrategy.java:151)
at org.bonitasoft.engine.expression.impl.ExpressionServiceImpl.evaluate(ExpressionServiceImpl.java:86)
at org.bonitasoft.engine.core.expression.control.api.impl.ExpressionResolverServiceImpl.evaluateExpressionWithResolvedDependencies(ExpressionResolverServiceImpl.java:215)
at org.bonitasoft.engine.core.expression.control.api.impl.ExpressionResolverServiceImpl.evaluateExpressionsFlatten(ExpressionResolverServiceImpl.java:120)
at org.bonitasoft.engine.core.expression.control.api.impl.ExpressionResolverServiceImpl.evaluate(ExpressionResolverServiceImpl.java:83)
at org.bonitasoft.engine.core.connector.impl.ConnectorServiceImpl.evaluateInputParameters(ConnectorServiceImpl.java:352)
at org.bonitasoft.engine.connector.ConnectorServiceDecorator.evaluateInputParameters(ConnectorServiceDecorator.java:99)
at org.bonitasoft.engine.execution.work.ExecuteConnectorWork$EvaluateParameterAndGetConnectorInstance.call(ExecuteConnectorWork.java:198)
at org.bonitasoft.engine.execution.work.ExecuteConnectorWork$EvaluateParameterAndGetConnectorInstance.call(ExecuteConnectorWork.java:162)
at org.bonitasoft.engine.transaction.JTATransactionServiceImpl.executeInTransaction(JTATransactionServiceImpl.java:288)
at org.bonitasoft.engine.execution.work.ExecuteConnectorWork.work(ExecuteConnectorWork.java:122)
at org.bonitasoft.engine.execution.work.failurewrapping.TxInHandleFailureWrappingWork.work(TxInHandleFailureWrappingWork.java:42)
at org.bonitasoft.engine.execution.work.failurewrapping.TxInHandleFailureWrappingWork.work(TxInHandleFailureWrappingWork.java:42)
at org.bonitasoft.engine.execution.work.failurewrapping.TxInHandleFailureWrappingWork.work(TxInHandleFailureWrappingWork.java:42)
at org.bonitasoft.engine.execution.work.failurewrapping.TxInHandleFailureWrappingWork.work(TxInHandleFailureWrappingWork.java:42)
at org.bonitasoft.engine.execution.work.FailureHandlingBonitaWork.work(FailureHandlingBonitaWork.java:66)
at org.bonitasoft.engine.work.BonitaWork.run(BonitaWork.java:56)
at org.bonitasoft.engine.work.SequenceRunnableExecutor.innerRun(SequenceRunnableExecutor.java:47)
at org.bonitasoft.engine.work.BonitaRunnable.run(BonitaRunnable.java:35)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: groovy.lang.MissingPropertyException: No such property: lines for class: BScript8
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
at BScript8.run(BScript8.groovy:197)
at org.bonitasoft.engine.expression.impl.GroovyScriptExpressionExecutorCacheStrategy.evaluate(GroovyScriptExpressionExecutorCacheStrategy.java:145)
... 21 more

1 个答案:

答案 0 :(得分:0)

谢谢大家,最后我研究了Google以获得更多帮助。我很惊讶地发现许多网站使用了我曾经使用过的东西:

String configLines[] = tempL.split("\\r?\\n");

我认为应该有效,但仍然......:)

但是我确实找到了split命令的另一个实现,如下所示:

String[]  configLines = tempL.split("\\r?\\n");

经过测试确实有效。

因此,虽然我现在很高兴我的代码正在运行并且我可以继续,但有人可以告诉我为什么前者在这种情况下不起作用,但是后者呢?

引导我这个答案的网站如下:

http://javarevisited.blogspot.com/2011/09/string-split-example-in-java-tutorial.html http://www.java-examples.com/java-string-split-example

非常感谢所有人,问候