printstream

Writing a formatted string to a file - Java

不想你离开。 提交于 2021-02-05 05:10:14
问题 I have a string that I format with the System.out.format() method, I do something like : System.out.format("I = %3d var = %9.6f", i, myVar); but when I try to write this formatted string into a file, I only get something like "java.io.PrintStream@84fc8d" in it. After looking into the documentation understood that this method is a little like System.out.print() and just return a PrintStream to display (in console for example), so I tried converting it with .toString or String.valueOf() but I

How does Out variable which is defined static and assigned null in System class can access non-static methods of PrintStream class. [duplicate]

杀马特。学长 韩版系。学妹 提交于 2020-12-13 04:20:05
问题 This question already has answers here : System.out is declared as static final and initialized with null? [duplicate] (2 answers) Closed 3 years ago . As to access the methods of PrintStram class an object must be created, so how the out variable is able to access those methods when it is assigned null. public final static PrintStream out = null; This is the declaration in System class. I tried to write the similar code but then it gives NullPointerException. My code is given below. class

Append to text file using PrintStream

狂风中的少年 提交于 2019-12-24 00:54:49
问题 I cant append text to a text file, it only overwrites the previous text. My code: //using JFileChooser to select where to save file PrintStream outputStream = MyFrame.ShowSaveDialog(); if(outputStream!=null){ outputStream.append(input); outputStream.close(); } Edited: The ShowSaveDialog returns a PrintStream. Here is the code for that method: public static PrintStream ShowSaveDialog(){ JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter(

Java Socket OutputStream is not flushing

北慕城南 提交于 2019-12-23 17:37:03
问题 I am writing a socket-based server in java. A client connects to it(a web-browser) and the server sends back a simple html code and sets cookie to recognize next time client connects to it again. I am using PrintStream to write to the socket , but flush is not working. The only way i can flush is to use shutdownoutput or close and both close the socket stream. But i do not want that because i am readin/writing to it several times in several places in the code. What can do? Could't get any

Print() vs Write() method of System.out

眉间皱痕 提交于 2019-12-20 07:29:21
问题 The Javadoc for PrintStream#print(char) states Prints a character. The character is translated into one or more bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method. So this means that following code should print 2 'a' however prints one 'a' not two. System.out.print('a'); System.out.write('a'); Can some one help me understand this behaviour 回答1: As per the java docs of PrintStream#write Writes the specified

PrintWriter autoflush puzzling logic

僤鯓⒐⒋嵵緔 提交于 2019-12-20 06:49:08
问题 public PrintWriter(OutputStream out, boolean autoFlush): out - An output stream autoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer public PrintStream(OutputStream out, boolean autoFlush): out - The output stream to which values and objects will be printed autoFlush - A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written What

How do I catch a java.io.PrintStream place its output in a JEditorPane?

荒凉一梦 提交于 2019-12-13 02:48:53
问题 I am attempting to make a Java program in which a user can select any .class or .jar file from their computer. My program will then pop up a JInternalFrame with a JEditorPane in it as the console, capturing any console output from the user's program. Note that I do not want to capture just System.err or System.out calls, but ALL PrintStream calls that go to the console. (individual question from IDE-Style program running ) 回答1: You can catch everything that is printed through System.out using

Thread Safety of PrintStream in Java

我的梦境 提交于 2019-12-12 13:09:25
问题 I am trying to write to a file. I need to be able to "append" to the file rather than write over it. Also, I need it to be thread safe and efficient. The code I have currently is: private void writeToFile(String data) { File file = new File("/file.out"); try { if (!file.exists()) { //if file doesnt exist, create it file.createNewFile(); } PrintStream out = new PrintStream(new FileOutputStream(file, true)); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date(); out

Java: Why don't the PrintWriter or PrintStream classes throw exception? [duplicate]

此生再无相见时 提交于 2019-12-07 06:47:41
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: PrintWriter and PrintStream never throw IOExceptions Maybe the question is a bit "strange". But i'm curious to know why both PrintWriter and PrintStream don't check automatically runtime exceptions , and they provides a checkError() method. Thanks to all in advance. 回答1: For PrintStream which is often writing to std out or err, these stream might have been closed or discarded but you don't want the program to

Java: Why don't the PrintWriter or PrintStream classes throw exception? [duplicate]

喜欢而已 提交于 2019-12-05 08:51:42
This question already has answers here : Closed 7 years ago . Possible Duplicate: PrintWriter and PrintStream never throw IOExceptions Maybe the question is a bit "strange". But i'm curious to know why both PrintWriter and PrintStream don't check automatically runtime exceptions , and they provides a checkError() method. Thanks to all in advance. For PrintStream which is often writing to std out or err, these stream might have been closed or discarded but you don't want the program to fail unexpectedly as a results. PrintWriter is in many ways the Writer version of PrintStream, though I am not