我正在从Java启动一个外部进程,并通过等方式获取它的stdin、stdout和stderr。我的问题是:当我想将数据写入我的输出流(过程的stdin)时,直到我实际调用它process.getInputStream()时,它才会被发送。close()溪流。我明确地打电话flush()。
我做了一些实验,发现如果我增加发送的字节数,它最终会通过。在我的系统上,神奇数字是4058字节。
为了测试,我将数据发送到 perl 脚本,其内容如下:
#!/usr/bin/perl
use strict;
use warnings;
print "Perl starting";
while(<STDIN>) {
print "Perl here, printing this: $_"
}
Run Code Online (Sandbox Code Playgroud)
现在,这是 java 代码:
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
public class StreamsExecTest {
private static String readInputStream(InputStream is) throws IOException {
int guessSize = is.available();
byte[] bytes = new byte[guessSize];
is.read(bytes); // This call has side effect of filling the array
String output = new String(bytes);
return output;
}
public …Run Code Online (Sandbox Code Playgroud) 我有一段简单的代码,但它的工作方式与我预期的不同:
try (OutputStream ostr = new BufferedOutputStream(System.out)) {
ostr.write("lol".getBytes());
}
System.out.println("hmmm");
Run Code Online (Sandbox Code Playgroud)
结果只是
lol
Run Code Online (Sandbox Code Playgroud)
已打印,但未打印hmm。
我究竟做错了什么?我假设hmm没有打印,因为也ostr关闭了,这是正确的吗?System.out我知道这是一个非常综合的例子,但我仍然期待答案。
我必须向套接字流发送动态缓冲区大小。\n它工作正常,但是当我尝试发送大小大于\n的多个缓冲区时int my_buffer_size =18 * 1024;(这是一个指示值)
我收到错误(对于某些写入):
\n\nJava.net.SocketException: Broken pipe\nat java.net.SocketOutputStream.socketWrite0(Native Method)\nRun Code Online (Sandbox Code Playgroud)\n\n我的代码非常简单:\n例如,如果我想发送一个大文件,我会读取一个文件流
\n\nbyte[] bs = new byte[my_buffer_size];\nwhile (... ){ \nfileInputStream.read(bs);\nbyte[] myBufferToSend = new byte[sizeBuffer];\nDataOutputStream out = new DataOutputStream(cclient.getoutputStream());\nout.writeInt(myBufferToSend.length);\nout.write(myBufferToSend);\nout.flush();\n}\nRun Code Online (Sandbox Code Playgroud)\n\n(该文件只是一个测试,缓冲区大小可以是可变的)
\n\nSendBufferSize 为 146988。
\n\n有没有办法解决管道破裂的错误?我四处阅读,但实际上我没有\xe2\x80\x99解决问题。
\n\n谢谢\n任何帮助表示感谢
\n\n我使用经典的 ServerSocket serverSocket;\n和 Socket cclient
\n我正在编写一个简单的 Swing GUI,其中包含一个打印调试消息和异常的文本字段。我目前已将其设置在写入 PipedOutputStream 的位置,并且我有一个守护线程,它从连接的 PipedInputStream 读取数据并写入文本区域。
当我试图弄清楚如何关闭守护线程中的流时,我遇到了另一个答案,它说守护线程不应持有任何资源。管道流算吗?他们需要关闭吗?
我目前正在为游戏开发 UDP 服务器。在此服务器中,使用 aByteArrayInputStream和ObjectInputStreamevery tick 将序列化字节转换为对象。为流创建一个变量并在程序关闭时关闭它们一次是否更有效?
像这样:
class Main {
private static ByteArrayInputStream byteIn;
private static ObjectInputStream objectIn;
public static void main(String[] args) {
while(true){
receive();
}
//when program is done call close();
}
public static void receive(){
byteIn = new ByteArrayInputStream();
objectIn = new ObjectInputStream(new BufferedInputStream(byteIn));
//do something
}
public static void close(){
objectIn.close();
byteIn.close();
}
}
Run Code Online (Sandbox Code Playgroud)
或者每次创建和关闭新流是否更有效?
像这样:
class Main {
public static void main(String[] args) {
while(true){
receive();
}
}
public static void …Run Code Online (Sandbox Code Playgroud) java inputstream outputstream objectoutputstream bytearrayinputstream
我有以下代码:
\nProcess proc;\ntry\n{\n ProcessBuilder procBuilder = new ProcessBuilder(/* some args */);\n proc = procBuilder.start();\n if (proc.waitFor(30000, TimeUnit.MILLISECONDS))\n {\n //Etc...\n }\n else\n {\n //Handle it\n }\n}\ncatch (InterruptedException ie)\n{\n currentThread().interrupt();\n}\nfinally\n{\n //What goes here?\n}\nRun Code Online (Sandbox Code Playgroud)\n我试图找到一些来源来指示是否需要调用proc.destroy()\xc2\xa0(我应该isAlive()在调用 destroy 之前检查吗?),并手动关闭其输入/输出/错误流,但无济于事。据我所知,甚至官方文档也没有明确说明这一点。
当我完成生成的进程时,是否有必要执行这些操作,或者只是良好的做法?
\n我有一个方法打开一个文件并传递给另一个函数来写一些数据.第二种方法想要使用PrintWriter来写入其数据.但是,我不想要求每个人都使用PrintWriter写入该流.
目前它看起来像这样(消毒的例子......不要批评我选择的方法或变量名称)
public void exportRawDataIntoStream(OutputStream os) {
PrintWriter pr = new PrintWriter(os);
printFirstData(pr);
printSecondData(pr);
}
public void exportToFile(File file) {
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(file));
exportRawDataIntoStream(os);
doMoreWithTimeFile(os);
} finally {
if (os != null) {
try {
os.close();
} catch (Exception e ) {
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
除非我在exportRawDataIntoStream的末尾加上'pr.flush',否则这不起作用.我无法关闭PrintWriter,因为它会关闭整个流.
冲洗是否合法可靠?我是否应该使用其他方法将Writers混合在同一个流上,或者我绝对不应该这样做?
我有一个Bitmap图像,我必须存储在SD卡的文件夹中,我的代码如下所示.它按预期创建文件夹和文件,但图像没有存储到文件中,它仍然是一个空文件...任何人都可以告诉我什么是错的?
Bitmap merged = Bitmap.createBitmap(mDragLayer.getChildAt(0).getWidth(), mDragLayer.getChildAt(0).getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(merged);
// save to folder in sd card
try {
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "folder");
if(!imagesFolder.exists())
imagesFolder.mkdirs();
int imageNum;
if(imagesFolder.list()==null)
imageNum = 1;
else
imageNum = imagesFolder.list().length + 1;
String fileName = "file_" + String.valueOf(imageNum) + ".jpg";
File output = new File(imagesFolder, fileName);
while(output.exists()){
imageNum++;
fileName = "file_" + String.valueOf(imageNum) + ".jpg";
output = new File(imagesFolder, fileName);
}
OutputStream fOut = new FileOutputStream(output);
merged.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close(); …Run Code Online (Sandbox Code Playgroud) 我正在使用JasperReports和DynamicReports与这段java代码创建一个pdf格式的报告,其中包含utf-8字符,生成的问题pdf文件根本不包含utf-8字符,如果它们已被替换为" ".使用OutputStream创建utf-8文件时,我应该注意什么?
public void toPdf(String path){
OutputStream outHtml;
try {
outHtml = new FileOutputStream(path);
jasperBuilder.toPdf(outHtml);
} catch (Exception e1) {
logger.error("failed to create PDF", e1);
}
}
Run Code Online (Sandbox Code Playgroud)
这可能是值得注意的是,创建XLS和HTML文件不会面临这样的问题.
请注意,jasperBuilder.toPdf(outHtml);我已经跟踪了很多代码行,并且没有在这些行中我的utf-8字符被删除.所以我猜魔鬼在outHtml = new FileOutputStream(path);
我想使用JAX-RS从我的服务器端java返回一个压缩文件到客户端.
我尝试了以下代码,
@GET
public Response get() throws Exception {
final String filePath = "C:/MyFolder/My_File.zip";
final File file = new File(filePath);
final ZipOutputStream zop = new ZipOutputStream(new FileOutputStream(file);
ResponseBuilder response = Response.ok(zop);
response.header("Content-Type", "application/zip");
response.header("Content-Disposition", "inline; filename=" + file.getName());
return response.build();
}
Run Code Online (Sandbox Code Playgroud)
但我得到的例外情况如下,
SEVERE: A message body writer for Java class java.util.zip.ZipOutputStream, and Java type class java.util.zip.ZipOutputStream, and MIME media type application/zip was not found
SEVERE: The registered message body writers compatible with the MIME media type are:
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider …Run Code Online (Sandbox Code Playgroud) outputstream ×10
java ×9
inputstream ×3
android ×1
bitmap ×1
broken-pipe ×1
file-io ×1
filestream ×1
jax-rs ×1
jersey ×1
process ×1
sockets ×1
utf-8 ×1