// Version of the file reading program that // increases the size of the array by 1 whenever it gets full. // (Slow) import java.io.*; class StringReader { char[] string = {}; public void append(char c) { // Create a new array, 1 bigger than before. char[] newString = new char[string.length+1]; // Copy the old array there. for (int i = 0; i < string.length; i++) newString[i] = string[i]; string = newString; // Put the character at the end of the new array. string[string.length-1] = c; } public String toString() { return new String(string); } } public class CopyNaive { public static void main(String[] args) { StringReader reader = new StringReader(); try { int i = 0; // Read in characters one at a time. Character c = readChar(); while (c != null) { reader.append(c); c = readChar(); i++; if (i % 1000 == 0) System.out.println("Read " + i + " characters"); } System.out.print(reader); System.out.print(reader); } catch (IOException e) { System.err.println(e); } } static InputStreamReader in = new InputStreamReader(System.in); // Read one character from System.in, or return null on end-of-file. static Character readChar() throws IOException { int c = in.read(); if (c == -1) return null; else return (char)c; } }