内存中有一个SHORT数组,如何输出到文件,采用二进制格式把内存中数据原封不动输出文件~麻烦大家帮帮忙!~
解决方案 »
- 0x08 8C 01 如何转成中文?
- 字符集编码转换
- 用Socket发送信息怎么老是报 Connection reset ??
- 大家帮我看下这个Applet题,看看哪有错误!谢谢!
- 请问怎样实现密码区域?
- 这样的一个服务器推的方法可行吗?
- 求大牛解释,程序运行不了,在面板中实现3个按钮,点击变换相应颜色
- 请问有谁参加过scjd的培训?5天要7700大元?有什么收获啊?然后谁有比较好的scjd英文书推荐?谢谢
- 请问我的JBUILDER中,DATA PILOT为什么DRIVER很多都不能用呀??
- 能举个最简单的多线程同步的程序例子吗?
- java类中,如果有一个{},代表什么呀
- struts框架中为什么地址栏输入的是http://localhost:7001/struts-app/login.do而打开了一个jsp页面??
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
byte[] setByteData = null; try
{
dos.writeShort(changeData);
setByteData = baos.toByteArray();
}
catch (Exception e)
{
printStackTrace(e);
}
return setByteData;
}
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)
};
}
}