Home Answers Viewqa Java-Beginners Redirecting the console output into a file in Java

 
 


HHH
Redirecting the console output into a file in Java
1 Answer(s)      2 years and 4 months ago
Posted in : Java Beginners

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 Pages:
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
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
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
Console Output
Java NotesConsole Output You can write programs that write text lines... can write one complete output line to the console by calling... a similar print method which writes output to the console, but doesn't start
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
Console Input-Output (Java 5)
Java NotesConsole Input-Output (Java 5) Java 5's java.util.Scanner class has simplified console I0. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // File
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
Console Input: Scanner
Java NotesConsole Input: Scanner The java.util.Scanner class (added in Java 5) allows simple console and file input. Of course, your program should... 16 17 18 19 20 21 22 23 // File : introductory
Dialog and Console Input-Output
Prev: Dialog Box Output | Next: Example: Captitalize Java NotesDialog and Console Input-Output  This is similar to the previous program...: Scanner The java.util.Scanner class (added in Java 5) allows simple console
input output
; Introduction The Java I/O means Java Input/Output and is a part... the output stream in machine format. File This class... It uses for writing data to a file and also implements an output
Console I/O
Java Notes Console I/O Java was designed for graphical user interfaces (GUI) and industrial strength file and Internet I/O. No attempt was made... System.out.println for output during the debugging phase. Console I/O streams
JSP output in Console - JSP-Servlet
the output on console. Thanks...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
Input And Output
; Introduction The Java I/O means Java Input/Output and is a part of java.io...;} } Filter Files in Java: The Filter File Java example...;}   } } Java read file line by line
Console vs Dialog I/O
class is only available in Java 5 and beyond, and doing standard console...); Dialog None required. Output Console... Java NotesConsole vs Dialog I/O Dialog box I/O is useful. Normal
Command Line Standard Output In Java
be written at the console, in a file, or at any output source. Java provides... output data. These output data can be displayed at the console, file or any...Command Line Standard Output In Java In this section we will discuss about
input output
input output  java program using fileinputstream and fileoutputstream   Hi Friend, Try the following code: import java.io.*; class...(); System.out.println("File is copied"); } } Thanks
input output
input output  java program using fileinputstream and fileoutputstream   Hi Friend, Try the following code: import java.io.*; class...(); System.out.println("File is copied"); } } Thanks
HIBERNATE IN CONSOLE & SERVLET
the basic theoretical ideas of java source file of the class representing the data... HIBERNATE IN CONSOLE & SERVLET ( part-3... for using Hibernate in a console application & a servlet. 
Input From Console
-based console device associated with the current Java virtual machine... Input From Console       The Console Class inherits from Java.io.console
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
Need help writing a console program
Need help writing a console program  I need help cant seems to figure... will replace all sequences of 2 spaces with 1 space. The program will read a file...): This is a test, this is only a test! This test is a test of your Java programming
Invalid console appender in JBoss console
Invalid console appender in JBoss console  "ERROR: invalid console appender config detected, console stream is looping" The above is the error i am getting while starting the server after deploying the ear file into the jboss. I
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
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
Input and Output package
Input and Output package  Hi guys, My professor used input and output... she used in.readint() out.writeln() commands to read input and print output. she created two new objects directly to use this statements. /* input and output
File Reader example in JRuby
on your console as output. Here is the JRubyFile.txt as follows: .... For reading whole text file we have used while-loop in our program. Here is the output... File Reader example in JRuby   
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
java console programming - Java Beginners
java console programming  how to clear d screen in java console programming
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
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
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
unable to see the output of applet. - Applet
unable to see the output of applet.  Sir, I was going through the following tutorial http://www.roseindia.net/java/example/java/applet...://www.roseindia.net/java/example/java/applet/FirstApplet.html but the problem
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
Output of flex
Output of flex  hi....... please provide the name of the output file. What is the output of flex applications? please rply ASAP........ Thanks
Dialog Box Output
Prev: Do Nothing | Next: Dialog Box Input-Output Java NotesDialog Box Output... are generally followed by a semicolon. Console Output Beginning textbooks often use console output. In this style text lines
Spring Console
Spring Console         The Spring Console is a FREE standalone Java Swing application... Library files. The Spring Console also plugs into multiple, popular Java IDEs
Struts Console
Struts Console         The Struts Console is a FREE standalone Java Swing application for managing Struts-based applications. With the Struts Console you can
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
Faces Console
Faces Console         The Faces Console is a FREE standalone Java Swing application... Library files. The Faces Console also plugs into multiple, popular Java IDEs
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
Console Appender in Log4j
you will get following output on your console. 16:22:51,406... Console Appender in Log4j       In this log4j console appender tutorial  you
java coding for creating table in the console
java coding for creating table in the console  write a java program to create table
Hibernate generated SQL statements on console
the Hibernate generated SQL statements on console, what should we do?   If you want to see the Hibernate generated SQL statements on console just add property in hibernate configuration file- <property name=?show_sql? >true<
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
Java: Rainfall user interface - Console
Java NotesRainfall user interface - Console 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 // RainfallScanner.java
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
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   
Working With File,Java Input,Java Input Output,Java Inputstream,Java io Tutorial,Java io package,Java io example
:\nisha>java CreateFile1 New file "myfile.txt" has been created... CreateFile1.java C:\nisha>java CreateFile1 The specified file... Working With File      
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.