❶ 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();//輸出流用完就關閉
}
}