java 不使用 int onebyte 将字符串写入 OutputStream
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4912398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing a string to an OutputStream without using int onebyte
提问by JMRboosties
This is a total beginner question, I've spent the past hour searching both stackoverflow and Google, but I haven't found what I'm looking for, hopefully someone here can point me in the right direction.
这是一个完全的初学者问题,过去一个小时我一直在搜索 stackoverflow 和谷歌,但我还没有找到我要找的东西,希望这里有人能指出我正确的方向。
I'm trying to write a string to an OutputStream, which I will then use to write data to a MySQL database. I've successfully retrieved data from a MySQL (from a .php, implementing JSON and RESTful), so I have some idea of what I'm doing, I think. I'm creating a method which will take a string and return an output stream, and I'm having trouble writing to an output stream, because when I try to initialize one, it creates an anonymous inner class with the write(int oneByte) method. That's not what I want.
我正在尝试将字符串写入 OutputStream,然后我将使用该字符串将数据写入 MySQL 数据库。我已经成功地从 MySQL(从 .php,实现 JSON 和 RESTful)中检索了数据,所以我对我在做什么有一些想法,我想。我正在创建一个方法,它将接受一个字符串并返回一个输出流,但我在写入输出流时遇到了麻烦,因为当我尝试初始化一个时,它创建了一个匿名内部类,其中包含 write(int oneByte)方法。那不是我想要的。
private static OutputStream convertStringtoStream(String string) {
byte[] stringByte = string.getBytes();
OutputStream os = new OutputStream() {
@Override
public void write(int oneByte) throws IOException {
/** I'd rather this method be something like
public void write(byte[] bytes), but it requires int oneByte*/
}
};
//return os here
}
As you can see, I want to write to my OutputStream with the buffer, not a single byte. I'm sure this is simple question, but I've not been able to find an answer, or even sample code which does what I want. If someone could point me in the right direction I'd really appreciate it. Thanks.
如您所见,我想用缓冲区写入我的 OutputStream,而不是单个字节。我确定这是一个简单的问题,但我无法找到答案,甚至无法找到满足我要求的示例代码。如果有人能指出我正确的方向,我会非常感激。谢谢。
采纳答案by Christoph Seibert
Your method could look like this, but I'm not sure what it would accomplish. How would you use the returned OutputStream
?
你的方法可能看起来像这样,但我不确定它会完成什么。你将如何使用返回的OutputStream
?
private static OutputStream convertStringtoStream(String string) {
byte[] stringByte = string.getBytes();
ByteArrayOutputStream bos = new ByteArrayOutputStream(string.length());
bos.write(stringByte);
return bos;
}
Also, note that using String.getBytes()
might get you into trouble in the long run because it uses the system's default encoding. It's better to choose an explicit encoding and use the String.getBytes(Charset)
method.
另请注意,String.getBytes()
从长远来看,使用可能会给您带来麻烦,因为它使用系统的默认编码。最好选择显式编码并使用该String.getBytes(Charset)
方法。
回答by Rajesh Batheja
Instead of using the abstract OutputStream class, you might want to use ByteArrayOutputStream which allows you to write a buffer. Even better perhaps would be ObjectOutputStream which would allow you to write string directly since string is serializable. Hope that helps.
您可能希望使用 ByteArrayOutputStream 来编写缓冲区,而不是使用抽象的 OutputStream 类。更好的可能是 ObjectOutputStream,它允许您直接编写字符串,因为字符串是可序列化的。希望有帮助。