Redirecting the console output into a file in Java

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 Java
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
How to capture output from console into textarea in java?
How to capture output from console into textarea in java?  Hi, I would like to know how can we display the result of a program in java into a textarea instead of displaying it on console. Thanks
Advertisements
Redirecting the output of an expression to a variable - JSP-Servlet
Redirecting the output of an expression to a variable  I want to redirect the output of to a variable defined in variable.jsp. What is the syntax...' file: Create another jsp file 'get.jsp' to get the output: Thanks
Java Redirect output to file
Java 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-Servlet
JSP 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
i want to type the intput in the output console box how?
i want to type the intput in the output console box how?  i want to type the intput in the output console box how
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?  write a program in java to read a text file and write the output to an excel file using filereader and filewriter
Java Read Lines from Text File and Output in Reverse order to a Different Text File
Java Read Lines from Text File and Output in Reverse order to a Different Text... to another text file. When that is done the output values of that file need... to display the names and path on the console. But I can not get the file to read line
ModuleNotFoundError: No module named 'file_output'
ModuleNotFoundError: No module named 'file_output'  Hi, My Python... 'file_output' How to remove the ModuleNotFoundError: No module named 'file_output' error? Thanks   Hi, In your python environment
counting the values in input file and and writing the output to a file
counting the values in input file and and writing the output to a file ... it should write the output to a file. Input file has 20 rows and 4 columns. If we...(); fr.close(); System.out.println("Sending the output to file
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
ModuleNotFoundError: No module named 'wishbone_output_file'
ModuleNotFoundError: No module named 'wishbone_output_file'  Hi...: No module named 'wishbone_output_file' How to remove the ModuleNotFoundError: No module named 'wishbone_output_file' error? Thanks   Hi
ModuleNotFoundError: No module named 'wishbone_output_file'
ModuleNotFoundError: No module named 'wishbone_output_file'  Hi...: No module named 'wishbone_output_file' How to remove the ModuleNotFoundError: No module named 'wishbone_output_file' error? Thanks   Hi
ClearScreen in Console Java
ClearScreen in Console Java  How can I perform Clear Screen Operation in Java. As we used to do in C++ using clrscr(); Please guide
how to display the output of the newly created excel file
how to display the output of the newly created excel file  the following program will create the test.xlsx file, how can i display the content of the test.xlsx file in the web browser? thanks, %@ page import
java console programming - Java Beginners
java console programming  how to clear d screen in java console programming
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
Working With File,Java Input,Java Input Output,Java Inputstream,Java io Tutorial,Java io package,Java io example
;java CreateFile1 The specified file is already exist C... Working With File      ... all the operations that are common with a disk file. In lesson, we will learn how
input output in java
input 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 Java
In this section, you will get to know about file handling classes in java to handle the file input output operations
Java Input/Output Examples
Java 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   
output
output  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
executing a batch file which depends on jar files from a system tray but console should not display.
to see the frame outside of system tray and i want to see the console with output...executing a batch file which depends on jar files from a system tray but console should not display.  Hi all, I got following requirement, I have
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 Java
Input / 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
Redirecting and forwarding to views
In this section, you will learn about redirecting and forwarding views through prefixes
Process Flows Output - Java Beginners
Process 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 Beginners
Input 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 Questions
Output 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
java coding for creating table in the console
java coding for creating table in the console  write a java program to create table
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-Servlet
JSP 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 code
different 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 code
different 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 code
different 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
Maven dependency for com.dorkbox - Console version 3.8 is released. Learn to use Console version 3.8 in Maven based Java projects
( com.dorkbox - Console version 3.8 ) in their Java project if it is based on Maven... to use  com.dorkbox - Console version 3.8 in Java projects. Follow the step... and includes  com.dorkbox - Console version 3.8 java library in your
Maven dependency for com.dorkbox - Console version 3.3 is released. Learn to use Console version 3.3 in Maven based Java projects
( com.dorkbox - Console version 3.3 ) in their Java project if it is based on Maven... to use  com.dorkbox - Console version 3.3 in Java projects. Follow the step... that downloads and includes  com.dorkbox - Console version 3.3 java
Maven dependency for com.dorkbox - Console version 3.7 is released. Learn to use Console version 3.7 in Maven based Java projects
( com.dorkbox - Console version 3.7 ) in their Java project if it is based on Maven... to use  com.dorkbox - Console version 3.7 in Java projects. Follow the step... that downloads and includes  com.dorkbox - Console version 3.7 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
console application - text-based menu - Java Beginners
console application - text-based menu  Im doin a text-based menu console application. I have created five classes: namely: 1. addproduct 2. updateproduct 3. deleteproduct 4. view product 5. productmain i used Scanner
plz explain me the output of program - Java Beginners
plz 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
jsf form output - Java Server Faces Questions
button it goes to hello.jsp page and gives the output with all the details whatever
jsf form output - Java Server Faces Questions
the output with all the details whatever i provide. It is urgent for me please give
Maven dependency for org.glassfish.main.admingui - console-common version 7.0.0 is released. Learn to use console-common version 7.0.0 in Maven based Java projects
( org.glassfish.main.admingui - console-common version 7.0.0 ) in their Java project... - console-common version 7.0.0 in Java projects. Follow the step by step... of console-common released The developers of  

Ads