
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

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
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.