// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.*;
import java.util.*;

public class PrettyPrint {
    public static String load(String filename) {
        StringBuffer sb = new StringBuffer("");
        try {
            BufferedReader in = new BufferedReader(new FileReader(filename));
            String line;
            while((line = in.readLine()) != null) {
                sb.append(line);
                sb.append(' ');
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
    public static void format(String infnm, String outfnm) throws IOException {
        String a = load(infnm);
        PrintWriter pw = new PrintWriter(new FileOutputStream(outfnm));
        StringTokenizer p = new StringTokenizer(a, " ");
        String temp = p.nextToken();
        while (p.hasMoreTokens()) {
            String word = p.nextToken();
            if(temp.length() + 1 + word.length() > 10) {
                pw.println(temp);
                temp = word;
            } else {
                temp = temp + " " + word;
            }
        }
        if(temp.length() > 0) {
            pw.println(temp);
        }
        pw.close();
    }
    public static void main(String[] args) throws Exception {
        PrettyPrint.format("C:\\test.txt", "C:\\testout.txt");
    }
}
