Java从文件中读取数据存入一个数组中 5
Guest[]g=newGuest[4];//定义客户类数组g[0]=newGuest("10170828","10170828",5000);//客户类数组里给的帐号,...
Guest[] g=new Guest[4]; //定义客户类数组
g[0]=new Guest("10170828","10170828",5000);//客户类数组里给的帐号,密 码,钱数 g[1]=new Guest("111111","111111",5000);
g[2]=new Guest("222222","222222",5000);
g[3]=new Guest("333333","333333",5000);
这样是自己直接定义数组内容,我想把这些内容放在文件中然后读取,怎么办啊,各位大神帮帮忙哇 展开
g[0]=new Guest("10170828","10170828",5000);//客户类数组里给的帐号,密 码,钱数 g[1]=new Guest("111111","111111",5000);
g[2]=new Guest("222222","222222",5000);
g[3]=new Guest("333333","333333",5000);
这样是自己直接定义数组内容,我想把这些内容放在文件中然后读取,怎么办啊,各位大神帮帮忙哇 展开
展开全部
// 直接给一个只读一行的吧,呵呵,数字之间以空格来分隔public static void main(String[] args) throws Exception {File file = new File("src/test.txt");double[] a = getLineFromTxt(file," ");for(int i = 0; i< a.length; i++){System.out.println("a["+i+"] = "+a[i]);}} test中的数据以空格隔开 12 32 32.02 public static double[] getLineFromTxt(File file, String split) throws Exception{BufferedReader br = new BufferedReader(new FileReader(file));String firstLine = br.readLine(); //就读第一行哦String[] arrs = firstLine.split(" ");//将字符数组转为double数组double[] arr = new double[arrs.length];for(int i = 0; i< arr.length; i++){arr[i] = Double.parseDouble(arrs[i]);}if(br!= null){br.close();br = null;}return arr;}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询