当前页面: 开发资料首页 → J2SE 专题 → read和write的问题
read和write的问题
摘要: read和write的问题
我要写两个小程序,分别是write.java, read.java。 write.java的作用是随机产生一个二元double数组,然后写到weights.txt中去,read.java的作用是从weights.txt中读出数据,并且装在到一个二元double数组中去。
import java.util.*;
import java.io.*;
import java.lang.*;
class write
{
public static void main(String[] args)
{
// Weights' Values
double [][] w_hi = new double[5][15361]; // input nodes to hidden nodes
double [][] w_oh = new double[3][5]; // hidden nodes to output nodes
int i, j, k;
Random rand = new Random();
// initialize the weight to be random double between -0.05 and 0.05
for(j=1; j <= 4; j++)
{
for(i = 1; i <= 15360; i++)
w_hi[j][i] = (-0.05) + rand.nextDouble() * (0.05);
}
for(j=1; j <= 4; j++)
{
for(i = 1; i <= 15360; i++)
System.out.println("w_hi["+ j+ "][" +i +"] = " + w_hi[j][i] );
}
try
{
File txt = new File("weights.txt");
FileOutputStream datStream = new FileOutputStream(txt);
byte[] data = w_hi[][];//问题在这里。
datStream.write(data);
}
catch (Exception e)
{
e.printStackTrace();
}
//for(j=1; j <= 4; j++)
//{
//for(i = 1; i <= 15360; i++)
//
//outData.writeDouble(w_hi[j][i]);
//
//
//
//}
}
}
write.java的问题是如果用writeDouble的话,好像写出来的数据是乱码,不知道为什么,所以我就想把double数据先转成String,然后写出。但程序老提示class expected在转数据格式的时候,不知道该怎么改。
import java.util.*;
import java.io.*;
import java.lang.*;
class read
{
public static void main(String[] args)
{
// Weights' Values
double [][] w_hi = new double[5][15361]; // input nodes to hidden nodes
double [][] w_oh = new double[3][5]; // hidden nodes to output nodes
int i, j, k;
try
{
File in = new File("weights.txt");
FileInputStream file = new FileInputStream(in);
int length = (int)in.length();
byte[] length_byte = new byte[length];
file.read(length_byte);
String s = new String(length_byte);
s = s.replaceAll("/n", "");
String[] result = s.split("//s");
for(j=1; j <= 4; j++)
{
for(i = 1; i <= 15360; i++)
w_hi[j][i] = Double.parseDouble(result[i]);
}
}
catch (IOException e)
{
System.out.println("Could not read file.");
}
}
}
由于write老出问题,read没法测试,也不知道对不对。
各位大侠帮帮忙,多谢多谢。
byte[] data = w_hi[]这样俩边才是一维数组
那就把 byte[] data = w_hi[][];
改成 byte[] data = w_hi[];?
还是出问题阿:'.class' expected?
如果要将数字写进文件
那就用DataOutputStream吧
楼主现在程序写进文件的是乱码
学习学习
我改了一下,虽然可以运行,并写出一个文件,可打开之后还是乱码,这个怎么解决阿?还是它本身就是那样?
这是新的可以运行的程序:
import java.util.*;
import java.io.*;
import java.lang.*;
class write
{
public static void main(String[] args)
{
// Weights' Values
double [][] w_hi = new double[5][15361]; // input nodes to hidden nodes
double [][] w_oh = new double[3][5]; // hidden nodes to output nodes
int i, j, k;
Random rand = new Random();
// initialize the weight to be random double between -0.05 and 0.05
for(j=1; j <= 4; j++)
{
for(i = 1; i <= 15360; i++)
w_hi[j][i] = (-0.05) + rand.nextDouble() * (0.05);
}
for(j=1; j <= 4; j++)
{
for(i = 1; i <= 15360; i++)
System.out.println("w_hi["+ j+ "][" +i +"] = " + w_hi[j][i] );
}
try
{
File txt = new File("weights.txt");
FileOutputStream fos = new FileOutputStream(txt);
DataOutputStream dos = new DataOutputStream(fos);
for(j=1; j <= 4; j++)
{
for(i = 1; i <= 15360; i++)
dos.writeDouble(w_hi[j][i]);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
嘿嘿,问题解决了,我在写和读的时候分别用了writeDouble和readDouble,谢谢gefengxztg(戈峰)的提醒,马上给你加分。