Redirecting the console output into a file in Java

Ads

 
 

Share on Google+Share on Google+

HHH
Redirecting the console output into a file in Java
1 Answer(s)      6 years and 10 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

Ads
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


Ads









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
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
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
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
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
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
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
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
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      
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
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
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
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
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
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
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   
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
How to read properties file in Java?
the java.util.Properties class for reading a property file in Java program.... Here is the video tutorial of reading a property file in Java: "How to read properties file in Java?" Here is the data.properties the properties
java coding for creating table in the console
java coding for creating table in the console  write a java program to create table
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 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
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
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
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
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
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
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
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: import java.io.... will make a text file by the name "out.txt" and will write "Hello Java

Ads

 
Advertisement null

Ads