Question: I am in the process of writing a program which takes data from an input file and then outputs an exact copy of that data
I am in the process of writing a program which takes data from an input file and then outputs an exact copy of that data to a file specified by the user. It contains a very large amount of integer data. My instructions are to take 3000 or less of these elements due to memory constraints (specified by int x below) and put them into an array and copy them over. I need to repeat this loop until the entire file has been copied over. I am not sure how to alter my while loop below to make this happen. Basically once the first array has been read and copied it needs to start the process over at element 0 until the file is over.
import java.util.Scanner; import java.io.File; import java.io.PrintWriter;
public class PPM { public static void main ( String args[] ) { try { System.out.print ( " PPM Image Processor " );
Scanner keyboard = new Scanner ( System.in ); System.out.print ( " Enter input filename: " ); String inFile;
inFile = keyboard.next();
File file = new File( inFile ); Scanner input = new Scanner ( file );
String first = input.next();
int column = input.nextInt();
int row = input.nextInt();
int max = input.nextInt();
Scanner keyboardTwo = new Scanner ( System.in ); String outFile; System.out.print ( " Enter output filename: " ); outFile = keyboardTwo.next();
PrintWriter writer = new PrintWriter ( outFile ); writer.println ( first ); writer.print ( column ); writer.printf ( " %d", row ); writer.printf ( " %d ", max );
// Loop that reads the rest of the int data from the file int x = ( column * 3 ); System.out.printf int y = 0; int RGB[] = new int [x]; while ( input.hasNext() && y < x ) { int v = input.nextInt(); writer.println ( v ); y++; }
writer.close();
}
catch ( Exception ex ) { System.out.println ( "An error has occurred!" ); ex.printStackTrace(); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
