如何将int数组中得值保存为一个文件,然后再一个一个从文件中读去出来保存到一个新数组中 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你可以将int 型转成字符串.toString().然后用逗号分割类似写成一个csv文件,不过你保存成txt格式的,然后读的时候全处出来,直接放到一个string中,然后split成数组,再转成intInteger.parseInt(str[i]); 而且你可以利用DataOutputStream 和 DataOutputStream里面的writeInt()和readInt()直接读取 import java.io.*;public class TestDataStreams{ public static void main(String[] args) { int[]a={1,2,3,4,5,6,7,8}; DataInputStream dis = null; DataOutputStream dos = null; File tempFile = new File("mytemp.dat"); if(tempFile.exists()){ //Exit } //write try{ dos = new DataOutputStream(new FileOutputStream(tempFile)); for(int i=0;i < a.length;i++){ dos.writeInt(a[i]); } }catch(IOException ex) { //Message }finally{ if(dos != null){ dos.close(); } } //read int[]b; try{ dis = new DataInputStream(new FileInputStream(tempFile)); for(int j=0;j < a.length;j++){ b[j] = dis.readInt() }catch(IOException ex) { //Message }finally{ if(dis != null){ dis.close(); } } } }} 真正的java人生 语言重要,还是算法? 手机虚拟内存 垃圾回收 公安笔录管理软件中的数据导入导出问题 游戏中,飞机发射子弹应该怎么实现? 散分啦!! 关于在SWING打开IE浏览器 Toolkit.getDefaultToolkit().getScreenSize()获取的是全屏幕的大小。我想获取鼠标周围一小范围 java.util.dictionary的用法 新手求教基本方法 java中的+= 关于JScrollPane的滚动条事件
Integer.parseInt(str[i]);
public class TestDataStreams{
public static void main(String[] args)
{
int[]a={1,2,3,4,5,6,7,8};
DataInputStream dis = null;
DataOutputStream dos = null;
File tempFile = new File("mytemp.dat");
if(tempFile.exists()){
//Exit
}
//write
try{
dos = new DataOutputStream(new FileOutputStream(tempFile));
for(int i=0;i < a.length;i++){
dos.writeInt(a[i]);
}
}catch(IOException ex)
{
//Message
}finally{
if(dos != null){
dos.close();
}
}
//read
int[]b;
try{
dis = new DataInputStream(new FileInputStream(tempFile));
for(int j=0;j < a.length;j++){
b[j] = dis.readInt()
}catch(IOException ex)
{
//Message
}finally{
if(dis != null){
dis.close();
}
}
}
}}