public void print(PrintStream target) {
target.println(this.title);
target.println(this.firstName);
target.print(this.lastName);
}
Given this method, what should the main pass to the method as target? Also, what type of variable is target? What does it mean that it is of type PrintStream?
PrintStream is a Java Object Type used to specify an output stream to which data can be written. In this case, you would probably pass the method the System.out print stream, as this will cause it to print to the main system console. This is the same PrintStream that you would write to when you call System.out.println(). In some cases, a different PrintStream could be used if the output was to go to a file or be uploaded to the web or similar purposes other than simply printing to the console.
Related
I have printed the data by using PrintStream object as below.
PrintStream ps = new PrintStream(System.out);
ps.println("Printing");
And I have also printed the data using System.out.println();
System.out.println(("Printing"));
I see no difference between these two statements. Do anyone clarify about this?
In your example it would work in a similar way, but System.out.println("") is generally used as an easy way to log to the console, while the new PrintStream() could also be used to redirect to a different stream, such as a remote endpoint. Generally the System.out.println is the more commonly used way to print things to the console.
Btw, you can actually redirect the System.out to something else by using the System.setOut(stream), so you wouldn't lose flexibility if you would System.out.println.
I see no difference between these two statements.
I agree, functionally they're the same.
System.out returns a PrintStream, so this is fine:
PrintStream out = System.out;
out.println("Printing");
Creating a new PrintStream using the constructor where you pass in an OutputStream – as you did with this: new PrintStream(System.out) – will create a new PrintStream object wrapped around the constructor argument.
So when you did the code below, you're simply putting a wrapper around the same underlying output stream.
PrintStream ps = new PrintStream(System.out);
ps.println("Printing");
Either way, when you call println("Printing"), it's resolving to the same println() method.
How in Servlets, out.println writes the html text to container's response object where normally in System.out.println() , it writes the text to the console??
This is creating a problem for me conceptually.
Neither PrintStream nor PrintWriter is specified in terms of a console.
A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. PrintStream
Prints formatted representations of objects to a text-output stream. PrintWriter
They are both about formatting data and passing the formatted data to an output stream. Any output stream. By default, in certain environments, the PrintStream objects initially referenced by each of System.out and System.err send their data to some sort of console, but that is just those particular instances.
Even those can be changed. For example, it can be useful in some types of testing to replace System.out with a PrintStream that sends its data to a ByteArrayOutputStream.
It uses a ServletOutputStream from the Javadoc,
Provides an output stream for sending binary data to the client. A ServletOutputStream object is normally retrieved via the ServletResponse.getOutputStream() method.
If you look at System.out you can see that it's a PrintStream which is a different implementation of OutputStream.
If someplace in a servlet an invocation of out.println(...) writes to the response object, then out must be a reference to the response object's OutputStream, or a wrapper stream and it. If System.out sends output someplace else (as normally it would do) then it is a reference to a different OutputStream.
A servlet method's (or any other method's) local variable named out is completely unrelated to System.out, barring you initializing the one as a reference to the other.
I need to write characters to a file or to standard output. And I am curious if it could be done with one method.
Now I have something like this:
OutputStream out;
if(toConsole)
out = System.out;
else
out = new FileOutputStream(file);
write(out);
}
void write (OutputStream str){
....
str.write(string);
But it is a problem that I am using (in case when "str" is System.out) write instead print?
(print java doc: "string's characters are converted into bytes according to the platform's default character encoding")
In case if I would use PrintWriter(or PrintStream) as a parameter then i cannot use BufferedWriter and writing to the file would be slower.
It is possible to use a same code (and same methods) for writing to a file and to System.out?
(This is for my school project so I want it to be a "pure" and fully correct)
What you're trying to accomplish, is to treat the fileoutput and the consoleoutput the same. This is possible, because System.out is a PrintStream, and you can create a PrintStream for a file like this
new PrintStream(yourFile)
or insert a BufferedOutputStream in between
new PrintStream(new BufferedOutputStream(new FileOutputStream(yourFile))).
Note that this is not needed, because PrintStream does buffer its output itself.
I would create a variable (global or not), representing the current output.
This might be a PrintStream, either System.out, or a PrintStream around a FileOutputStream, whatever you desire. You would then pass this stream to the write method or call the print methods on it directly.
The advantage is that you can easily switch this without much code modification, you can redirect it wherever you wan't. It's no problem to redirect it to a file and System.out! You wouldn't get that pure flexibility with the way you're writing the method currently.
You could (not saying you should), also redirect System.out directly, using System.setOut. This however is bad style, because it is quite uncommon and might confuse everyone else, if they have not seen the call to System.setOut.
System.out is an object of type PrintStream. So yes, you can write to
System.out and/or to another file using exactly the same methods. Just
construct a PrintStream object and direct it to your file. So declare
your out variable as PrintStream to start with.
See also:
http://docs.oracle.com/javase/7/docs/api/java/lang/System.html
http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html
If i want to print a line
This is Java programming language
to the console using System.out.print(), which and how many process gets used to print this line, and how we can compare it with writing the same line to file using FileWriter or BufferedWriter.
out is a static member of class System having type PrintStream http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html. You can create PrintStream objects for files and use them similarly. http://docs.oracle.com/javase/1.5.0/docs/api/java/io/PrintStream.html
Check PrintStream.java file to know the process of print method.
Check here for detailed flow of process
Different implementations of Java may have different ways of doing the thing in question. You can check out their source if you're interested in how it's being done.
For example in HotSpot VM, out is a java.io.PrintStream reference which internally uses a java.io.BufferedWriter to write to the standard output.
Hello,
I want to know the difference between println() and write() methods used in java servlet.
out.println("Hello");
out.write("Hello");
How the above code will be stored??
Why can we use both the methods for the writing the same text as above..
The out variable in your case is most likely refers to a PrintWriter, so the answer to your question lies in the API documentation of that class.
Just compare the description of write...
public void write(String s)
Write a string. This method cannot be inherited from the Writer class because it must suppress I/O exceptions.
... with the description of println ...
public void println(String x)
Print a String and then terminate the line. This method behaves as though it invokes print(String) and then println().
... and print ...
public void print(String s)
Print a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
All in all I'd say that the print methods work on a higher level of abstraction and is the one I prefer to work with when writing servlets.
The only plausible difference with PrintWriter.println has to with flushing which is invoked in case of using println but not print("\n") in all other respects it would behave same as print()
Plus the PrintWriter class also doent throw any Exception.
1.out.write("") method is for java.io.Writer it is used to write the any file like text,csv.
2.out.println("Hello") this one is servlet method and it is use for write data on browser.