// LineReturnFixer2.java
//
// Copies one text file to another.
// The original file is read in a platform-independent fashion.
// In the copy, end-of-line markers will be appropriate to the
// platform on which the program is run. The copy will have
// the same name as the original file plus the prefix "fixed-".
//
// Illustrates use of java.io class PrintWriter for text file
// output and java.io class BufferedReader for both text file
// input and prompted command-line input.
//
// IOExceptions are handled.
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.PrintWriter;
public class LineReturnFixer2 {
public static void main(String[] args)
{
BufferedReader keyboardIn = new BufferedReader(
new InputStreamReader(System.in),
1);
System.out.print("Enter name of file to be fixed: ");
try {
String inputFileName = keyboardIn.readLine();
String outputFileName = "fixed-" + inputFileName;
try {
// Open file for reading:
BufferedReader br = new BufferedReader(
new InputStreamReader(
new FileInputStream(
inputFileName)));
try {
// Open file for writing:
PrintWriter pw = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(
outputFileName))));
// Copy one file to the other, until end-of-file:
while (true)
{
String line = br.readLine();
if ( line == null )
break;
pw.println(line);
} // while
// Send output from buffer to the file itself:
pw.flush();
// Close files and buffers:
br.close();
pw.close();
// Inform user that the job has been completed:
System.out.println("File " + outputFileName + " has been created.");
} catch ( FileNotFoundException fnfe ) { // from FileOutputStream
System.err.println("Could not create file: "
+ outputFileName);
} catch ( IOException ioe ) { // from br.readLine()
System.err.println("Error reading file: "
+ inputFileName);
} // catch
} catch ( FileNotFoundException fnfe ) { // from FileInputStream
System.err.println("File " + inputFileName + " not found.");
} // catch
} catch ( IOException ioe ) { // from keyboardIn.readLine();
System.err.println("Error reading your input. "
+ "Something is wrong with your machine.");
} // catch
} // method main(String[])
} // class LineReturnFixer2