日期:2014-05-20 浏览次数:20914 次
package util; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileWriterTester { public static void main(String[] args) throws IOException { long l = 4657646574646L; FileWriter writer = new FileWriter("D:/temp"); writer.write(longToChar(l)); writer.flush(); writer.close(); FileReader reader = new FileReader("D:/temp"); char[] c = new char[4]; reader.read(c); reader.close(); System.err.println(charToLong(c) == l); new File("D:/temp").delete(); } public static char[] longToChar(long l) { char[] chars = new char[4]; for (int i = 0; i < 4; i++) { chars[i] = (char) (l << (i * 16) >> (16 * 3)); } return chars; } public static long charToLong(char[] c) { long result = 0; for (int i = 0; i < 4; i++) { long temp = c[i]; result |= temp << (16 * (3 - i)); } return result; } }