Question: Java question about reading an input file and writing it to an output file. I see lots of questions on here about reading numbers from
Java question about reading an input file and writing it to an output file.
I see lots of questions on here about reading numbers from an input file.
I just want to read line by line.
I can get it to read fine and then I have it show in the console perfectly.
I am trying to use these 2 lines of code to capture that info before it is sent to console, and instead send it to a file, "opt.txt"
~ PrintStream out = new PrintStream(new FileOutputStream("opt.txt"));
~ System.setOut(out);
I get no errors, but the screen just flashes and I get no output in console and I get no "otp.txt" file.
If I remove those 2 lines, it outputs perfectly to console.
Here is my code. "inp.txt" is a file local to the project with numbers, letter, and spaces.
###
import java.io.*;
public class ReadLines
{
public static void ReadLines(File f) throws IOException
{
PrintStream out = new PrintStream(new FileOutputStream("opt.txt"));
System.setOut(out);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null)
{
System.out.println(line);
}
br.close();
fr.close();
}
public static void main(String[] args)
{
File f = new File ("inp.txt");
try
{
ReadLines(f);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
