java.util.Scanner读取剩余内容 [英] java.util.Scanner read remaining content

查看:150
本文介绍了java.util.Scanner读取剩余内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 java.util.Scanner 实例,是否可以返回所有剩余内容?我的情况是,一旦我在 String 中读取了许多字段,我只想得到剩下的东西并将其存储在其他地方。例如,我的内容可能包括:

Using a java.util.Scanner instance, is it possible for it to return all remaining content? My scenario is that once I have read a number of fields within a String, I just want to get whatever is left and store that somewhere else. For example my content could include:

1,2,Some Garbage with \' special characters and so on

我会使用构建扫描程序,作为分隔符,调用 getInt()两次,然后想要获得一些带有\'特殊字符的垃圾等等返回一个电话。

I would construct the scanner using , as the delimiter, call getInt() twice and then want to get "Some Garbage with \' special characters and so on" back in one call.

推荐答案

使用扫描仪没有好的方法。您可以尝试
String next(模式模式)传递给它 multiline 模式,如 $

There is no "good" way to do this with Scanner. You can try String next(Pattern pattern) passing to it multiline pattern like $:

模式p = Pattern.compile($,Pattern.MULTILINE);

我没试过,但它可能会奏效。只要不要忘记在输入中剩余几行时检查它。

I have not tried this but it will probably work. Just do not forget to check it when several lines are remaining in your input.

但是我认为更好的方法就是使用普通的读取(byte [])来读取包装的输入流循环中的方法。这是因为扫描仪没有按原样读取流。它被用于阅读单独的字段。它做得很好。在您的情况下,您只想阅读blob,所以只需使用有效负载流来阅读它。

BUT I think that better way is just to read from wrapped input stream using ordinary read(byte[]) method in loop. It is because Scanner is not attended to read stream as is. It is attended to read separate fields. And it does it very well. In your case you just want to read "blob", so just read it using payload stream.

这篇关于java.util.Scanner读取剩余内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆