❶ java 怎样将TXT文件里的数值读入一个数组里
//直接给一个只读一行的吧,呵呵,数字之间以空格来分隔
publicstaticvoidmain(String[]args)throwsException{
Filefile=newFile("src/test.txt");
double[]a=getLineFromTxt(file,"");
for(inti=0;i<a.length;i++){
System.out.println("a["+i+"]="+a[i]);
}
}
test中的数据以空格隔开123232.02
publicstaticdouble[]getLineFromTxt(Filefile,Stringsplit)throwsException{
BufferedReaderbr=newBufferedReader(newFileReader(file));
StringfirstLine=br.readLine();//就读第一行哦
String[]arrs=firstLine.split("");
//将字符数组转为double数组
double[]arr=newdouble[arrs.length];
for(inti=0;i<arr.length;i++){
arr[i]=Double.parseDouble(arrs[i]);
}
if(br!=null){
br.close();
br=null;
}
returnarr;
}
❷ java编程读取txt文件中的内容放到数组中,计算后再输出到txt文件中,怎么实现
1、创建一个路径为要读取的txt文件的file对象rFile。
2、创建一个路径为要写入的txt文件的file对象wFile。
3、创建一个FileReader对象,传入rFile到构造器。
4、准备一个char数组,FileReader类有一个继承自java.io.Reader的read(char[] cbuf)方法,将字符读入数组。
5、创建一个FileWriter对象,传入wFile到构造器。
6、FileWriter类有一个继承自java.io.Writer的write(char[] cbuf)方法,可以写入字符数组。
7、最后别忘了关闭流。
❸ java:从txt文件中读数据到数组中。
package guoqing03;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class TxtToArray {
public static void main(String[] args) {
//根据城市id,来获得相应的数组,你可以通过改变id来获得不同的数组
int id = 2;
int[] city = getCityLocation(id);
System.out.println(Arrays.toString(city));
}
/**
* @param id 传入一个城市id,指代需要查询的是那个城市
* @return int[] 返回一个数组 如 [2, 489, 537]
*/
public static int[] getCityLocation(int id) {
//大家都懂的我方法全是用的静态方法,获得包含所有城市数据的一个数组
int[] citys = getAllCitys();
int[] city = new int[3];
int j = 0;
//根据用户的id来获取不同城市的数组并返回
for(int i=id*3-3; i<3*id; i++) {
city[j] = citys[i];
j++;
}
return city;
}
/**
* @return 返回包换全部城市的一个数组
*/
public static int[] getAllCitys() {
//“city.txt”我是放在当前项目下面的,要是你不要包名呢,直接放到你的当前目录下面就OK了
File file = new File("citys.txt");
//定义一个数据流管道哈
BufferedReader read = null;
//StringBuffer嘛 是一个很常用到的方法,给String一样,只是
//这个东西拼接字符串速度比较快,效率高
StringBuffer strbuf = new StringBuffer("");
String str = "";
try {
read = new BufferedReader(
new InputStreamReader(
new FileInputStream(file)));
if((str = read.readLine()) != null) {
//字符串拼接,你懂的
strbuf.append(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//将StringBuffer类型转换成String类型
String strbuf2 = new String(strbuf);
//利用正则表达式将String撇开成一个Sring数组
String[] strCitys = strbuf2.split("\\s+");
int[] citys = new int[strCitys.length];
//将String数组转换成int数组
for(int i=0; i<strCitys.length; i++) {
String strCity = strCitys[i];
citys[i] = Integer.parseInt(strCity);
}
return citys;
}
//程序缺陷:1.没有实现人机交互,不过也没必要
// 2.没有对用户的id值进行判断,是否符合要求,超出范围程序会出
// java.lang.的异常
}
❹ Java中如何提取TXT文件数据并讲数据导入到数组里...急求
publicstaticvoidmain(String[]args)throwsFileNotFoundException{
Scannerscanner=newScanner(newFileInputStream("d:/data.txt"));//通过FileInputStream构建Scanner
ArrayList<Integer[]>integerDataList=newArrayList<>();//初始化数据存放list,arrayList中的每一项是一条数据
while(scanner.hasNext()){
Stringline=scanner.nextLine();//读入一行数据
String[]datas=line.split(",");//根据逗号分隔字符串
if(datas.length!=3){
//如果分割后的数据不足三个,说明数据错误,抛弃本条数据
continue;
}
//构建integer类型数组,保存本行数据
Integer[]integerData=newInteger[3];
//通过Integer.valueOf方法将字符串转换为整型数字
integerData[0]=Integer.valueOf(datas[0]);
integerData[1]=Integer.valueOf(datas[1]);
integerData[2]=Integer.valueOf(datas[2]);
//将本行数据添加到所有数据的集合中
integerDataList.add(integerData);
}
//输出所有数据
for(Integer[]integerData:integerDataList){
System.out.println(Arrays.toString(integerData));
}
}
❺ java中如何把一个txt文件中的信息保存在数组内存中
首先你需要读取txt,得到每一行的数据内容,用字符串接出来。
然后分析你的字符串,多个表示之间是空格隔开,所以使用split分隔成为数组。然后你可以得到一个二维数组。遍历这个而二维数组对应下表对应一个信息
代码的话随便写点,未测试:
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("1.txt")));
ArrayList cardIds=new ArrayList<String>();
ArrayList usernames=new ArrayList<String>();
ArrayList passwords=new ArrayList<String>();
ArrayList moneys=new ArrayList<String>();
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文件并放到数组中
FileInputStream fr=new FileInputStream(new File("d:/1.txt"));
BufferedReader br=new BufferedReader(fr);
String str;
String substr;
final int begin=0;
int end=1;
while((str=br.readLine())!=null)//读取文件的一行,循环直到文件读取完成
{
//对文件中的一行,进行字符串的截取。
while(end!=-1)
{
end=str.IndxOf(","); //查找“,”在字符串中的位置,不存在返回-1;
substr=str.substring(begin,end+1);//截取字符串的一部分,从begin开始到end+1结束。
str=str.substring(end+1,str.lenth()); //将剩下的字符串赋值,并再次截取
}
}
❼ java 读取txt存入数组
public static void main(String[] args) {
try {
System.out.println(System.in);
FileReader fileReader = new FileReader("D:\\data.txt");
BufferedReader buf = new BufferedReader(fileReader);
int i = 0;
String readLine = "";
String[] myArray = new String[500]; //100:这个值你自己定义,但不宜过大,要根据你文件的大小了,或者文件的行数
while((readLine = buf.readLine()) != null){
myArray[i] = readLine;
if("33333".equalsIgnoreCase(readLine)){
myArray[i]="aaaaa";
}
i++;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
❽ JAVA怎么把TXT文件的内容逐行放进数组中
最简单的方法,直接文件中把所有内容都读取到一个String字符变量中,然后使用正则分割,也就是str.split("\n");他的返回值就是一个数组(字符数组)。
❾ 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内容存放在数组中
参考代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.io.FileWriter;
public class Demo {
//main方法抛出异常,当然了也可以try catch处理异常
public static void main(String[] args) throws Exception {
byte[] ary = { 2, 6, 8, 1, 5, 6, 8 };
//存入数据的文件目录是c:\\ary.txt
FileWriter fw = new FileWriter("c:\\ary.txt");
for (int i = 0; i < ary.length; i++) {
fw.write(ary[i]+",");//读取一个数字,就写入文件一次
}
fw.close();//输出流用完就关闭
}
}