import java.io.*; /** * Files --- read from a file and writes to another. * @author Blanca J. Polo */ public class Files{ /** * Files Reads the contents of a file and saves them into another file * We use a while loop to iterate through the file because we do not * know if the file is empty or has one or many entries. * We also count the lines copied. * @param arg A string array containing the command line arguments. * @return No return value. * @exception Exception If the file is not found. */ public static void main (String arg[ ]) throws Exception{ //variable declaration for the input file //note that variables are declared but not initialized try{ //the Input file, we are reading from here boolean bFileFound = false; //to check if input file exists File f = new File("inputFile.txt"); FileInputStream fis = new FileInputStream(f); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); //the Output file, we are writing to it File fout = new File ("outputFile.txt"); FileOutputStream fos = new FileOutputStream (fout); PrintStream ps = new PrintStream(fos); int count = 0; String s = ""; s= br.readLine( ); //reading first line from Input File while (s!=null){ count++; //counting lines copied ps.println(s); //writing to the output file s= br.readLine( ); //reading from input file } System.out.println("The input file used is inputFile.txt"); System.out.println("The output file is outputFile.txt"); System.out.println("File copy done, "+ count + " lines were copied"); }//try ends catch(FileNotFoundException fnfe){ System.out.println("Input file does not exist, please create it."); System.out.println("File name should be inputFile.txt"); } //catch ends } //main } //class