Redirecting the console output into a file in Java
Hi,
i coded a program that read specific lines from a text file.
It's working well.
Now i want to redirect the output of the console to a text file.
i used your tutorial, but it's not working for me.
Could you please help for this code.
[CODE]
import java.io.*;
import java.io.PrintStream.*;
import java.io.PrintWriter.*;
import java.io.FileOutputStream;
public class ReadSpecificLine2
{
public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader (new FileReader("C://Temp/File.txt"));
String info = "";
int startLine = 15;
int endLine = 20;
int startLine2 = 30;
int endLine2 = 40;
for (int i = 0; i < startLine; i++)
{
info = in.readLine();
}
for (int i = startLine; i < endLine + 1; i++)
{
info = in.readLine();
System.out.println(info);
}
for (int i = 0; i < startLine2; i++)
{
info = in.readLine();
}
for (int i = startLine2; i < endLine2 + 1; i++)
{
info = in.readLine();
System.out.println(info);
//System.setOut(new PrintStream(new FileOutputStream("C://Temp/Target.txt")));
//PrintWriter out = new PrintWriter(new FileOutputStream("c:\\Temp\\Target.txt"));
//System.setOut(out);
}
in.close();
}}
[CODE/]
Thank you
View Answers
January 24, 2011 at 11:59 AM
Hi Friend,
Try the following code:
import java.io.*;
public class ReadSpecificLine {
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer();
String line = "";
int lineNo=0;
try {
LineNumberReader ln = new LineNumberReader(new FileReader("C:/hello.txt"));
int count = 0;
while (ln.readLine() != null) {
count++;
}
ln.close();
FileReader fr = new FileReader("C:/hello.txt");
BufferedReader br = new BufferedReader(fr);
for (lineNo = 1; lineNo <= count; lineNo++){
if (lineNo == 2) {
for (lineNo = 2; lineNo <= 4; lineNo++) {
buffer.append(br.readLine());
buffer.append("\n");
}
} else
br.readLine();
}
BufferedWriter bw=new BufferedWriter(new FileWriter(new File("C:/new.txt"),true));
bw.write(buffer.toString());
bw.newLine();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thanks
Related Tutorials/Questions & Answers:
Redirecting the console output into a file in JavaRedirecting the
console output into a
file in Java Hi,
i coded a program that read specific lines from a text
file.
It's working well.
Now i want to redirect the
output of the
console to a text
file.
i used your tutorial
Advertisements
Java Redirect output to fileJava Redirect
output to
file
This section illustrates you how to redirect the
output to the
file.
It may be comfortable to a programmer if they are able... for future
reference. Here we are going to discuss about
redirecting console outputs
JSP output in Console - JSP-ServletJSP
output in Console
Q:An input text should be read and the same should be printed in the
CONSOLE.
Actually i was able to do it in the browser... the
output on
console.
Thanks
CONSOLE itself, representing standard
output, is an instance of java.io.PrintStream class. Standard
output is, on most operating systems,
console output.
format...: It is a class made available by
Java to let you manipulate various operating
ClearScreen in Console JavaClearScreen in
Console Java How can I perform Clear Screen Operation in
Java.
As we used to do in C++ using clrscr();
Please guide
Command Line Standard Output In Java be written at the
console, in a
file, or at any
output source.
Java provides...Command Line Standard
Output In
Java
In this section we will discuss about the Command Line
Java IO Standard
Output.
Standard streams, feature of various O
output java - Java Beginners;
}
a. what is the
output of the ff.
java statement:?
i. System.out.println (secret...
output java public static int secret(int one)
{
int i;
int...? Hello
Are you beginner?
Ok, The first
Output is 125
Show output as a xml file using Velocity
Show
output as a xml
file using Velocity
This
Example shows you how
to show
output as a xml
file...;to produce
the
output.#foreach( $stu in $stuList ): This works same
input output in javainput
output in java
java program using filereader and filewriter...();
System.out.println("
File is copied");
}
}
Thanks
Hi...);
}
out.close();
System.out.println("
File is copied");
}
}
Thanks
File Handling Classes in JavaIn this section, you will get to know about
file handling classes in
java to handle the
file input
output operations
Java Input/Output ExamplesJava Input/
Output Examples
In this tutorial you will learn about how the Inputs and outputs are managed in
java. To understand the
Java Input
&
Output... to File
Java Write To
File Append
Java Write To
File Core Java Interview Question Page 1 direct program messages to the system
console, but error messages, say to a
file..., they both point at the system
console. This how the standard
output could be re...
Core
Java Interview Question Page 1
outputoutput Sir,I am experiencing some problems with the
output of this program.I got some useful help this website,but the
output of the program isn't producing what it should.let me explain the
output below is what happen when
How to read properties file in Java? the
java.util.Properties class for reading a property
file in
Java program... a property
file in
Java: "How to read properties
file in
Java?"
ADS...Example program of reading a properties
file and then printing the data
Input / Output in Easiest Method in JavaInput /
Output in Easiest Method in Java How to input from keyboard, both strings and characters and display the result on the screen with the easiest method in
Java?
Hi Friend,
Try the following code:
import
Process Flows Output - Java BeginnersProcess Flows Output Hi, I have been asked to get a particular structure as the
output. the assignment is as follows Any process flow has..... there might be a split activity sumwhere in the process, that splits the process flow
Input and Output problems - Java BeginnersInput and
Output problems 1) what is Difference between InputStreamReader and FileReader?
2) what is buffer?
Hi friend.... This link will help you.
http://www.roseindia.net/
java/beginners
Output of this one - Java Interview QuestionsOutput of this one
public class Test {
int i=-277;
{
try{
if(Byte i<<1)
{
System.out.println("Executed");
}
else... errors.
The following code displays "Executed" as an
output.
public class
How to write a file in Java?How to write a
file in
Java? To write a
file in
Java use the class FileWriter... and strings.
Example of how to write text to a
file in
java:ADS_TO_REPLACE_6
import... data into
output stream.ADS_TO_REPLACE_1
It is used for writing character files
JSP to output Java String Array - JSP-ServletJSP to
output Java String Array I am just a little confused about the
output that I would get from printing a 2D String array loaded with database fields. For example lets say we have the following array: String [ ][ ] array
different output trying to execute same java codedifferent
output trying to execute same
java code i am using net beans 7 ide and
java 6 to develop my
java projects. i used the following coding... there is different
output trying to execute same
java code
different output trying to execute same java codedifferent
output trying to execute same
java code i am using net beans 7 ide and
java 6 to develop my
java projects. i used the following coding... " portList : false"
Why there is different
output trying to execute same
java different output trying to execute same java codedifferent
output trying to execute same
java code i am using net beans 7 ide and
java 6 to develop my
java projects. i used the following coding... " portList : false"
Why there is different
output trying to execute same
java Input From Console -based
console device
associated with the current
Java virtual machine... Input From
Console
The
Console Class inherits from Java.io.console
File Reader example in JRuby on
your
console as
output. Here is the JRubyFile.txt as follows: ADS...
File Reader example in JRuby
... to Read
File in JRuby. We can read and write
file on the console
Console Appender in Log4j
Console Appender in Log4j
In this log4j
console appender tutorial you will be introduced with ConsoleAppender
which is used in Log4j for appending
output plz explain me the output of program - Java Beginnersplz explain me the
output of program Hi friends,
can u please explain me the
output of below program with proper explanation of each and every line...;Hi Friend,
Output of this program is:
a=2
b=0
c=0
mul=27
volume=18
Java File - Java Beginners.
Output
Print the
file names and size of the
file to the
console.
Please...
Java File Hi Friend,
Thank you for the support I got previously...
Anyone please send me the
Java Code for scanning a directory and print