Perform the actual copy and conversion @param from The input file @param to The output file @throws IOException Thrown if an error occurs during the conversion
(File from, File to)
| 128 | * @throws IOException Thrown if an error occurs during the conversion |
| 129 | */ |
| 130 | private void convert(File from, File to) throws IOException { |
| 131 | // Open files: |
| 132 | try (BufferedReader in = |
| 133 | new BufferedReader(new InputStreamReader(new FileInputStream(from), SOURCE_ENCODING))) { |
| 134 | try (PrintWriter out = |
| 135 | new PrintWriter(new OutputStreamWriter(new FileOutputStream(to), StandardCharsets.UTF_8))) { |
| 136 | |
| 137 | // Output header: |
| 138 | out.print("<!DOCTYPE html><html><head><meta charset=\"UTF-8\" />" + |
| 139 | "<title>Source Code</title></head><body><pre>"); |
| 140 | |
| 141 | // Convert, line-by-line: |
| 142 | String line; |
| 143 | while ((line = in.readLine()) != null) { |
| 144 | StringBuilder result = new StringBuilder(); |
| 145 | int len = line.length(); |
| 146 | for (int i = 0; i < len; i++) { |
| 147 | char c = line.charAt(i); |
| 148 | switch (c) { |
| 149 | case '&': |
| 150 | result.append("&"); |
| 151 | break; |
| 152 | case '<': |
| 153 | result.append("<"); |
| 154 | break; |
| 155 | default: |
| 156 | result.append(c); |
| 157 | } |
| 158 | } |
| 159 | out.print(result.toString() + LINE_SEPARATOR); |
| 160 | } |
| 161 | |
| 162 | // Output footer: |
| 163 | out.print("</pre></body></html>"); |
| 164 | |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | } |
| 170 |