javatxt数组的简单介绍
华为云服务器特价优惠火热进行中! 2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。
合作流程: |
今天给各位分享javatxt数组的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
微信号:cloud7591如需了解更多,欢迎添加客服微信咨询。
复制微信号
本文目录一览:
java中如何把一个txt文件中的信息保存在数组内存中?
首先你需要读取txt,得到每一行的数据内容,用字符串接出来。
然后分析你的字符串,多个表示之间是空格隔开,所以使用split分隔成为数组。然后你可以得到一个二维数组。遍历晌历这个而二维数肢饥锋肢销组对应下表对应一个信息
代码的话随便写点,未测试:
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("1.txt")));
ArrayList cardIds=new ArrayListString();
ArrayList usernames=new ArrayListString();
ArrayList passwords=new ArrayListString();
ArrayList moneys=new ArrayListString();
String str=null;
while((str=br.readLine)!= null){
String[] st=str.split(" ");
cardIds.add(st[0]); usernames.add(st[1]);passwords.add(st[2]);moneys.add([st3]);
}
String [] username=usernames.asList();
....
Java读取TXT文件数据到数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
public class FileToAry {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("c:\\test.txt"));//使用BufferedReader 最大好处是扮耐正可以按行读取,每次读取一行
int n = 0;//定义n
int m = 0;//定义m
int[] U = null;//定义数组
int[] S = null;//定义数组
int index = 0;//索引
String temp;//定义字符串,用于亩困保存每行读取到的数据
while ((temp = br.readLine()) != null) {
int[] ary = aryChange(temp);//通过函数吧字符串数组解析成整数数组
if (index == 0) {
n = ary[0];
m = ary[1];
}
if (index == 1) {
U = ary;
}
if (index == 2) {
S = ary;
}
index++;
}
br.close();// 关闭输入流
// 测试输出
System.out.println("n=" + n + "厅悔\tm=" + m + "\n数组U=" + Arrays.toString(U) + "\n数组S=" + Arrays.toString(S));
}
static int[] aryChange(String temp) {// 字符串数组解析成int数组
String[] ss = temp.trim().split("\\s+");// .trim()可以去掉首尾多余的空格
// .split("\\s+")
// 表示用正则表达式去匹配切割,\\s+表示匹配一个或者以上的空白符
int[] ary = new int[ss.length];
for (int i = 0; i ary.length; i++) {
ary[i] = Integer.parseInt(ss[i]);// 解析数组的每一个元素
}
return ary;// 返回一个int数组
}

Java 读取.txt文件数据写入数组。
package com.haoge.license;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class test {
/**
* @param args
*/配蚂
public static void main(String[] args) {
int i=1;//行数
String a="";//第一行数据
String b="";//第二行数据物备
int[] k=null;
File filetxt= new File("D:\\test.txt");
FileInputStream fis;
try {
//建立文件输入罩卖毁流
fis = new FileInputStream(filetxt);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String linetxt="";
while ((linetxt=br.readLine())!=null) {
if(i==1){
a=linetxt.trim();
}else if(i==2){
b=linetxt.trim();
}else{
break;
}
i++;
}
//生成数组
int length=Integer.parseInt(a)/2;
k=new int[length];
for(int j=b.length()/2;j0;j--){
k[length-j]=Integer.parseInt(b.substring(2*(j-1), 2*j));
}
//打印数据
for(int m=0;mk.length;m++){
System.out.println("k["+m+"]="+k[m]);
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
关于javatxt数组和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
