outputstream

Get Command Prompt Output to String In Java

核能气质少年 提交于 2019-11-29 04:22:27
I need a java method that will read command prompt output and store it into a String to be read into Java. This is what I have so far but isn't working right. public void testGetOutput() { System.out.println("\n\n****This is the testGetOutput Method!****"); String s = null; String query = "dir " + this.desktop; try { Runtime runtime = Runtime.getRuntime(); InputStream input = runtime.exec("cmd /c " + query).getInputStream(); BufferedInputStream buffer = new BufferedInputStream(input); BufferedReader commandResult = new BufferedReader(new InputStreamReader(buffer)); String line = ""; try {

Log4J: How do I redirect an OutputStream or Writer to logger's writer(s)?

人盡茶涼 提交于 2019-11-29 01:14:48
I have a method which runs asynchronously after start, using OutputStream or Writer as parameter. It acts as a recording adapter for an OutputStream or Writer ( it's a third party API I can't change ). How could I pass Log4J's internal OutputStream or Writer to that method? ...because Log4J swallows System.out and System.err, I was using before. Arthur Neves My suggestion is, why dont you write your OutputStream then?! I was about to write one for you, but I found this good example on the net, check it out! LogOutputStream.java /* * Jacareto Copyright (c) 2002-2005 * Applied Computer Science

Do I need to flush the servlet outputstream?

隐身守侯 提交于 2019-11-28 19:11:54
Do I need to "flush" the OutputStream from the HttpServletResponse? I already saw from to Should I close the servlet outputstream? that I don't need to close it, but it's not clear if I need to flush it. Should I expect it from the container as well? protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { byte[] response = getResponse(); String responseType = getResponseType(); response.setContentLength(response.length); response.setContentType(responseType); response.getOutputStream().write(response); response.getOutputStream()

Why is it good to close() an inputstream?

随声附和 提交于 2019-11-28 17:05:26
问题 I have a sound experience in Java programming language. But one thing that has always been in my mind is that why is it necessary to close() java.io.InputStream or its subclasses? Now with java.io.OutputStream , say FileOutputStream , after writing to a file, if we don't close() the output stream, the data that we intended to write in the file remains in the buffer and is not written to the file. So it becomes necessary to close() an OutputStream . But i never had any bitter experiences after

Java Networking: Explain InputStream and OutputStream in Socket

扶醉桌前 提交于 2019-11-28 16:45:35
I have created a server by using ServerSocket . After that, I have created Client using Socket , and connect to this server. After that, I do "some stuff" with InputStream and OutputStream is taken from Socket Object. But, I don't really understand inputStream and outputStream so much. Here is my simple code : private Socket sock = null; private InputStream sockInput = null; private OutputStream sockOutput = null; ... String msg = "Hello World"; byte[] buffer = null; try { sockOutput.write(msg.getBytes(), 0, test.length()); sockOutput.write("Hello StackOverFlow".getBytes(), 0, test.length());

Byte[] to InputStream or OutputStream

孤街醉人 提交于 2019-11-28 15:12:51
问题 I have a blob column in my database table, for which I have to use byte[] in my Java program as a mapping and to use this data I have to convert it to InputStream or OutputStream . But I don't know what happens internally when I do so. Can anyone briefly explain me what's happening when I do this conversion? 回答1: You create and use byte array I/O streams as follows: byte[] source = ...; ByteArrayInputStream bis = new ByteArrayInputStream(source); // read bytes from bis ...

Spring: getOutputStream() has already been called for this response

爷,独闯天下 提交于 2019-11-28 13:03:25
I know that there are many other posts dealing with the very same error, but all of them are either about JSP / GSP pages or for any other reason not very helpful in my case. I'm using Spring MVC with Thymeleaf. The following function is for downloading a file. @RequestMapping(value = "/test/download/*", method = RequestMethod.GET) public String getFile(HttpServletResponse response) { ServletOutputStream stream = null; try { stream = response.getOutputStream(); MultipartFile f = test.getFile(); InputStream is = f.getInputStream(); IOUtils.copy(is, stream); response.flushBuffer(); stream.flush(

JAVA : Handling socket disconnection

痴心易碎 提交于 2019-11-28 11:51:06
Two computers are connected by socket connection. If the server/client closes the connection from their end(i.e closes the InputStream , OutputStream and Socket ) then how can I inform the other end about the disconnection? There is one way I know of - trying to read from the InputStream , which throws an IOException if connection is closed, but is there any other way to detect this? Another question, I looked the problem up on the internet and saw inputStream.available() does not solve this problem. Why is that? Additional Information : I'm asking for another way because my project becomes

How to check whether an OutputStream is closed

耗尽温柔 提交于 2019-11-28 10:42:15
Is there anyway to check whether an OutputStream is closed without attempting to write to it and catching the IOException ? For example, consider the following contrived method: public boolean isStreamClosed(OutputStream out){ if( /* stream isn't closed */){ return true; }else{ return false; } } What could you replace /* stream isn't closed */ with? The underlying stream may not know it its closed until you attempt to write to it (e.g. if the other end of a socket closes it) The simplest approach is to use it and handle what happens if it closed then, rather than testing it first as well. No

How do I write to a .txt file in Android?

你。 提交于 2019-11-28 10:00:45
I have an app that needs to read and write to a text file. I have it reading, but I don't have it writing. The idea is that when I click the save button on the screen, its going to save all the info that has been put into the textviews into an array and the write each segment of the array into the text file. This is my code for the writing portion: public class AddOrModify extends Activity { private Button Savebtn; private Button Cancelbtn; private EditText NameofRoute; private EditText Address1; private EditText City1; private EditText State1; private EditText Zip1; private EditText Address2;