import java.io.*; // need File, PrintWriter and IOException import java.util.Scanner; // this program will input all of the Strings from a file and output them to a second file using an array to store them temporarily public class FileReaderWriter { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); // use a Scanner to get keyboard input for the file names System.out.print("Enter the input file name: "); String file1 = in.next(); // input file name System.out.print("Enter the output file name: "); String file2 = in.next(); // output file name String[] list = getInput(file1); // getInput will input from file1 and return it as an array sendOutput(list, file2); // sendOutput will take the array and output its contents to file2 } public static String[] getInput(String inputFile) throws IOException // method to input from inputFile and store in a String array, returning a version { // of the String array whose size matches the number of inputs Scanner infile = new Scanner(new File(inputFile)); // open the Scanner (File) String[] list = new String[1000]; // assume no more than 1000 Strings int n = 0; // n is the number of input Strings to this point, used to resize the array later while(infile.hasNext()&&n<1000) // input while the input file is not empty and we haven't reached 1000 Strings list[n++] = infile.next(); infile.close(); // close the input file if(n==1000) System.out.println("Array full, cannot read any more"); // warn user if array is full String[] list2 = new String[n]; // create a second String array whose size is exactly n for(int i=0;i