1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package javaomnimarktest;
import java.io.*;
public class Sample21 {
private static String readFileAsString(String filePath, String charset) throws IOException {
StringBuilder value = new StringBuilder ("");
int i;
Reader r = new InputStreamReader (new FileInputStream (filePath), charset);
while ((i = r.read ()) >= 0) {
value.appendCodePoint (i);
}
r.close ();
return value.toString ();
}
private static void expandChars (String s, String fileName) throws IOException {
PrintStream ps = new PrintStream (fileName);
int i;
for (i = 0; i < s.length(); i++) {
int c = s.codePointAt (i);
if (c <= 127) {
ps.write (c);
} else {
ps.format ("&#x%04x;", c);
}
}
ps.close ();
}
public static void main (String [] args) throws IOException {
System.err.println ("Running Sample2.java");
String inputString = readFileAsString ("in.txt", "UTF-8");
expandChars (inputString, "in-chars-java.txt");
}
} |
Partager