日期:2014-05-20 浏览次数:20774 次
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class WriteShorts { public static void main(String[] args) throws Exception { short[] shorts = {1, 2, 3, 4, 5}; String filename = "c:\\data.dat"; writeShorts(shorts, filename); } private static void writeShorts(short[] shorts, String filename) throws IOException { File file = createFile(filename); FileOutputStream fos = new FileOutputStream(file); for (short aShort : shorts) { fos.write(toBytes(aShort)); } fos.close(); } private static File createFile(String filename) throws IOException { File file = new File(filename); if (!file.exists()) { file.createNewFile(); } return file; } private static byte[] toBytes(short aShort) { return new byte[]{ (byte) ((aShort >> 8) & 0xFF), (byte) (aShort & 0xFF) }; } }
------解决方案--------------------