Equivalent of freopen in Java - java

Please suggest a method to obtain a similar behaviour in Java as when we do freopen("filename","r",stdin) OR freopen("filename","w",stdout) in C.

Assuming that this is to redirect stdin/stdout/stderr, have a look at System.setIn and friends. They allow you to assign a different InputStream and PrintStream to System.in, System.out and System.err.

See the webpage below. There's a solution to what you wanna do.
http://techtipshoge.blogspot.com/2011/01/connect-standard-io-to-files.html

Related

Redirect/hide output of called program

This question is pretty easy I guess but I can't figure out, how to do it best. I have a JAR file which I use in my program and basically I just call the main() method of one of the classes in this JAR file.
The problem is, that I don't want to have the output of this method on my program's output - it should be hidden.
I call the method just that way:
Main.main(new String[] {"some-arg1", "arg2", "some-argument3"});
Is there any possibility to hide/redirect the output of this method? Can I just set a new temporary stdout or something like that?
Any help is greatly appreciated. Thanks in advance!
You can use System.setOut() to redirect stdout. You can also close System.out completely if you want to.

Could I duplicate or intercept an output stream in Java?

I want to intercept the standard output stream, then copy the content to another stream, but I also hope to keep the standard output stream like the original. Could I achieve that in Java?
You can use something like the example of TeeOutputStream explained here Writing Your Own Java I/O Stream Classes
Basically you create a TeeOutputStream, give it your stream and current System.out
then use System.setOut with the new stream.
Anything written to System.out will be written to the original System.out as well as your stream so you can do whatever you want with it
Edit:
Oracle took off this page, It is also possible to use TeeOutputStream from Apache Commons to do the same thing without adding any code.
Take a look at this package: org.apache.commons.io.output. I think that TeeOutputStream is what you're looking for.

Reading already-written text from console

I feel silly asking this but: how can I read a string from a console that is not input but has already been written to the console?
For instance, if I print a line to the console how can I read it after the fact? I know this is atypical.
Except in certain special circumstances, you can't, as Brad pointed out in the comments. As a justification, consider that when you send some text to the console (via standard output or standard error) to be printed, there's no guarantee that the text actually will be printed. Perhaps the user has redirected that output stream to /dev/null, in which case the text is gone for good. Or perhaps the text has scrolled out of view.
Basically, don't count on ever being able to access something that was sent to the console for output. If you need it, keep a copy in your program. And if you're trying to get at something that another program sent to the console, you need to use some sort of different arrangement like shared memory, sockets, or a fifo pipe.
Replace both System.in and System.out with your own streams and
try to create a pipe between System.in and System.out;
I am not sure that, whether it works or not;

How to make System.err write in one block

I'd like to solve following issue: imagine that you are doing lots of outputs in System.out, and from time to time there is an Exception thrown. When you look into the console, you can see that the exception's trace text is mixed up with normal output done by System.out. I understand that these two are different streams (System.out and System.err) and I guess that the output of the exception is done internally by some other thread, that is why it is mixed up.
However, is it somehow possible for the code to stop until output for the exception is done? The only solution I can think of is to put Thread.sleep, but maybe there might be some option I am not aware of.
Just curious really :) It'd be nice for debugging (that's the reason I ask, because reading the output and exceptions mixed up in between is terrible)
If you have multiple threads and their output is getting interleaved, you should think about using a logging facility rather than trying to share the stderr and stdout-
http://download.oracle.com/javase/6/docs/api/java/util/logging/package-summary.html
Or apache's log4j:
http://logging.apache.org/log4j/1.2/
How about redirecting System.err to a file?
System.setErr( ... );
The problem needs to be solved elsewhere, in the process which merges two file streams into the output you see - by only printing out a line when it is done. Since that is most likely not an option to you if you are talking about the "java.exe" output, you need to investigate elsewhere.
I have not tested, but I would start with having a look at invoking flush() on System.out before you send output to System.err.
When java implicitly imports the lang (java.lang.*) package, the System class has 2 standard output streams.
System.err
System.out
And because these both output to the same standard output, you have to choose one and change it from standard output to file output. My recommendation would be to change the System.err output like so:
import java.io.* ......
System.setErr (new PrintWriter (new FileWriter ("Errors.txt")));
Hope this helps!
EDIT
Sorry, can't leave comments yet, but the logging idea above is very good. Depending on what you are doing, logging will be an optimal solution. But I try to avoid logging because it tends to become very memory extensive if it is used too much.
Write to a separate stream in memory to format your output, then write the single string at once. You may still end up with your single string in the middle of an exception's text, though. Fundamentally what you are doing requires synchronization, or a separate stream (think one output file per thread).
I don't see how Thread.sleep will do anything other than complicate the issue.
Create a new PrintStream to file descriptor 2 with autoflush set to false.
System.setErr(new PrintStream(new FileOutputStream(FileDescriptor.err, false, "UTF-8")));
Just make sure to call System.err.flush after important error messages.
If you want to merge stderr into stdout, you can do the above but with FileDescriptor.out.

Is there any method to locate a line with line num in a text file with java api?

I hope there do have an operation for this topic,'cause I don't want to loop the file once again,and hope to read the file from the specific location say a line number,and then I will read the file with much more threads than just one.
Any idea?
Thanks first!!
To my knowledge there isn't anything like this in the standard Java API. You could use LineIterator or (even just a basic BufferedReader) to build a custom class that does what you need, like this guy did.
Note that a RandomAccessFile sounds promising but unfortunately for you, the seek() method takes an offset in bytes and not in lines so unless your lines are all always the same length, this wont' work for you.

Categories

Resources