How do I convert a String to an InputStream in Java?

This question already has an answer here: How do I turn a String into a InputStreamReader in java? 6 answers Like this: InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8)); Note that this assumes that you want an InputStream that is a stream of bytes that represent your original string encoded as UTF-8. For versions of Java less than 7, replace S

如何在Java中将字符串转换为InputStream?

这个问题在这里已经有了答案: 如何在Java中将字符串转换为InputStreamReader? 6个答案 喜欢这个: InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8)); 请注意,这假定您需要一个InputStream,它是一个代表您原始字符串编码为UTF-8的字节流。 对于Java版本小于7的版本,请将StandardCharsets.UTF_8替换为"UTF-8" 。 我发现使用Apache Commons IO可以让我的

Get an OutputStream into a String

What's the best way to pipe the output from an java.io.OutputStream to a String in Java? Say I have the method: writeToStream(Object o, OutputStream out) Which writes certain data from the object to the given stream. However, I want to get this output into a String as easily as possible. I'm considering writing a class like this (untested): class StringOutputStream extends Outpu

将一个OutputStream获取到一个字符串中

将输出从java.io.OutputStream传递到Java中的字符串的最佳方式是什么? 说我有方法: writeToStream(Object o, OutputStream out) 它将某些数据从对象写入给定的流。 但是,我希望尽可能轻松地将此输出转换为字符串。 我正在考虑写这样的课(未经测试): class StringOutputStream extends OutputStream { StringBuilder mBuf; public void write(int byte) throws IOException { mBuf.append((char) byte);

How do I use a delimiter in Java Scanner?

sc = new Scanner(new File(dataFile)); sc.useDelimiter(",|rn"); 我不明白分隔符是如何工作的,有人能用通俗的话来解释这个吗? The scanner can also use delimiters other than whitespace. Easy example from Scanner API : String input = "1 fish 2 fish red fish blue fish"; // \s* means 0 or more repetitions of any whitespace character // fish is the pattern to find Scanner s = new Scanner(input).u

如何在Java扫描器中使用分隔符?

sc = new Scanner(new File(dataFile)); sc.useDelimiter(",|rn"); 我不明白分隔符是如何工作的,有人能用通俗的话来解释这个吗? 扫描器也可以使用除空白以外的分隔符。 Scanner API的简单示例: String input = "1 fish 2 fish red fish blue fish"; // \s* means 0 or more repetitions of any whitespace character // fish is the pattern to find Scanner s = new Scanner(input).useDelimiter("\s*fish\s*"); S

when does it block and why?

I'm trying to read commands via a Scanner Object. For checking the Input Syntax I use sc.hasNext() (for the case of missing commands). It did work fine for many cases already, but now I have the case that's described in the JavaAPI as "MAY block and wait for Input". When does the hasNext() method block and how can I control it? The funny Thing is that it work's perfectl

什么时候阻止,为什么?

我正尝试通过Scanner对象读取命令。 为了检查输入语法我使用sc.hasNext() (对于缺少命令的情况)。 它在许多情况下都能正常工作,但现在我已将JavaAPI中描述的情况描述为“可能会阻止并等待输入”。 hasNext()方法块何时可以控制? 有趣的事情是,它的工作完全正常,前3块的情况下。 此外,JavaAPI将hasNext()描述为检查是否存在另一个Input的正确方法,以便Method next()不会生成Exception 。 以下是我到目前为止制作的

How to avoid Java code in JSP files?

I'm new to Java EE and I know that something like the following three lines <%= x+1 %> <%= request.getParameter("name") %> <%! counter++; %> is an old school way of coding and in JSP version 2 there exists a method to avoid Java code in JSP files. Can someone please tell me the alternative JSP 2 lines, and what this technique is called? The use of scriptlets (those <

如何避免JSP文件中的Java代码?

我是Java EE的新手,我知道类似于以下三行 <%= x+1 %> <%= request.getParameter("name") %> <%! counter++; %> 是一种古老的编码方式,在JSP版本2中存在一种避免JSP文件中的Java代码的方法。 有人可以告诉我可选的JSP 2行,以及这种技术被称为什么? 自从十年前taglibs(如JSTL)和EL(表达式语言,这些${}东西)的诞生以来,JSP中使用scriptlets(那些<% %>东西)的确非常令人沮丧。 scriptlet

Is null check needed before calling instanceof?

将null instanceof SomeClass返回false或抛出一个NullPointerException ? No, a null check is not needed before using instanceof. The expression x instanceof SomeClass is false if x is null . From the Java Language Specification, section 15.2.2, "Type comparison operator instanceof": "At run time, the result of the instanceof operator is true if the value of the RelationalExpress

在调用instanceof之前是否需要空值检查?

将null instanceof SomeClass返回false或抛出一个NullPointerException ? 不,在使用instanceof之前不需要空的检查。 如果x为null则表达式x instanceof SomeClass为false 。 从Java语言规范的第15.2.2节“类型比较运算符instanceof”中: “在运行时,结果instanceof运算符是true ,如果RelationalExpression的值不null ,并引用可以转换为引用类型不提高一个ClassCastException 。否则,结果是false 。” 所以如果操作数为

Avoid synchronized(this) in Java?

Whenever a question pops up on SO about Java synchronization, some people are very eager to point out that synchronized(this) should be avoided. Instead, they claim, a lock on a private reference is to be preferred. Some of the given reasons are: some evil code may steal your lock (very popular this one, also has an "accidentally" variant) all synchronized methods within the same

避免在Java中同步(this)?

每当有关于Java同步的SO问题出现时,有些人就非常希望指出应该避免synchronized(this) 。 相反,他们声称,锁定私人参考是首选。 一些给定的原因是: 一些邪恶的代码可能会偷你的锁(非常流行这一个,也有一个“意外”的变体) 同一类中的所有同步方法使用完全相同的锁,这会降低吞吐量 你(不必要地)暴露太多的信息 包括我在内的其他人认为synchronized(this)是一个习惯用法(在Java库中也是如此),它是安全的并且很

IllegalArgumentException or NullPointerException for a null parameter?

I have a simple setter method for a property and null is not appropriate for this particular property. I have always been torn in this situation: should I throw an IllegalArgumentException , or a NullPointerException ? From the javadocs, both seem appropriate. Is there some kind of an understood standard? Or is this just one of those things that you should do whatever you prefer and both are

对于null参数,IllegalArgumentException或NullPointerException?

对于一个属性,我有一个简单的setter方法, null不适合这个特定的属性。 我一直在这种情况下被撕毁:我应该抛出一个IllegalArgumentException或一个NullPointerException ? 从javadocs来看,两者似乎都适合。 是否有某种被理解的标准? 或者,这只是其中一件事,你应该做任何你喜欢的事情,而且两者都是正确的? 如果你不想让null成为一个允许的值,那么就会调用IllegalArgumentException ,如果你试图使用一个变量为null的

Why is Java Vector class considered obsolete or deprecated?

Why is Java Vector considered a legacy class, obsolete or deprecated? Isn't its use valid when working with concurrency? And if I don't want to manually synchronize objects and just want to use a thread-safe collection without needing to make fresh copies of the underlying array (as CopyOnWriteArrayList does), then is it fine to use Vector ? What about Stack , which is a subclass of

为什么Java Vector类被认为已经过时或被弃用?

为什么Java Vector被认为是遗留类,已经过时或已被弃用? 使用并发时,它的使用是否有效? 如果我不想手动同步对象,只想使用线程安全的集合,而不需要创建底层数组的新副本(就像CopyOnWriteArrayList一样),那么使用Vector吗? 怎么样Stack ,这是Vector一个子类,我应该用什么来代替它? Vector在每个单独的操作上同步。 这几乎从来没有你想要做的。 通常你想同步整个操作序列。 同步单个操作不太安全(如果您遍

Is it okay to throw NullPointerException programmatically?

When there is a post-condition, that return value of a method must not be null, what can be done? I could do assert returnValue != null : "Not acceptable null value"; but assertions could be turned off! So is it okay to do if(returnValue==null) { throw new NullPointerException("return value is null at method AAA"); } ? Or is it better to use a user-defined excepti

以编程方式抛出NullPointerException可以吗?

当存在后置条件时,方法的返回值不能为空,可以做些什么? 我可以 assert returnValue != null : "Not acceptable null value"; 但断言可能被关闭! 那么可以这样做 if(returnValue==null) { throw new NullPointerException("return value is null at method AAA"); } ? 或者,对于这种情况使用用户定义的异常(如NullReturnValueException)会更好吗? 我认为在JVM为你做这件事之前尽可能