導航:首頁 > 編程語言 > java讀取txt數組

java讀取txt數組

發布時間:2022-11-29 10:24:14

java如何讀取txt文本數據並以數組形式一行

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class ReadFiledata {
public static String txt2String(File file){
StringBuilder result = new StringBuilder();
try{
BufferedReader br = new BufferedReader(new FileReader(file));//構造一個BufferedReader類來讀取文件
String s = null;
while((s = br.readLine())!=null){//使用readLine方法,一次讀一行
result.append(System.lineSeparator()+s);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
return result.toString();
}

public static void main(String[] args){
File file = new File("F:/card.txt");//我的txt文本存放目錄,根據自己的路徑修改即可
System.out.println(txt2String(file));
}
}

❷ java 讀取txt文件得出數組,希望高手幫我補完卻掉的部分並解釋一下 如果是單詞的呢

import java.util.*;
import java.io.*;

public class order {
public static void main(String[] args) throws IOException {
/***
* 代碼填充部分
*/
//文件讀取
String line2StrVal = ""; // 第二行
String line3StrVal = ""; // 第三行
int lineIndex = 1; //行索引

//創建連接
BufferedReader reader = new BufferedReader(new FileReader("d:/xiaod.txt"));
String readLine = null; //每行臨時存儲
//開始讀取數據
while((readLine = reader.readLine()) != null){
if(lineIndex == 2){
line2StrVal = readLine;
}else if(lineIndex == 3){
line3StrVal = readLine;
}
lineIndex++;
}
reader.close(); // 關閉資源

//查看讀取數據
System.out.println(line2StrVal);
System.out.println(line3StrVal);

//開始將讀取數據載入到數組中
String[] arrayVal = line2StrVal.split(" ");
int a[] = new int[line2StrVal.split(" ").length];
for(int i=0;i<arrayVal.length;i++){
a[i]= Integer.parseInt(arrayVal[i]);
}
arrayVal = line3StrVal.split(" ");
double b[] = new double[line3StrVal.split(" ").length];
for(int i=0;i<arrayVal.length;i++){
b[i]= Double.parseDouble(arrayVal[i]);
}

/**
* 代碼填充結束
*/
double array[] = new double[1];
for (int i = 0; i < array.length; i++) {
array[i] = a[i] * b[i];
System.out.print("Total Number of Items:");
System.out.println(a[0] + a[1] + a[2] + a[3] + a[4] + a[5]);
System.out.print("Final Cost:$ ");
System.out.println(a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3] + a[4] * b[4] + a[5] * b[5]);

}
}
}

❸ Java 怎樣讀取TXT文件並把每行內容賦值到一個數組裡面,最好能有全部代碼。

			JFramejf=newJFrame("io");//窗體
Containerc=jf.getContentPane();//這個窗體的容器
JTextAreajta=newJTextArea();//文本域
Panelp=newPanel();//面板
JScrollPanejsp=newJScrollPane(jta);//實現不了
jta.setLineWrap(true);//自動換行
JButtonjb2=newJButton("讀取文件");

//監聽讀取按鈕
jb2.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente)
{

try{
Filef=newFile("D:\JAVA實訓\eclipse\pratice\a.txt");
InputStreamReaderis=newInputStreamReader(newFileInputStream(f),"gbk");
char[]c=newchar[10];
intlength=is.read(c);//read()方法能少用就少用,指針指向太麻煩
if(length==-1)
{
JDialogjd=newJDialog(jf,"讀取內容");
Containerc2=jd.getContentPane();
JLabeljl=newJLabel("你所讀取的內容為空",SwingConstants.CENTER);
c2.add(jl);
jd.setBounds(570,200,200,130);
jd.setVisible(true);
is.close();
}else{

jta.setText(newString(c,0,length));
is.close();
}
is.close();
}catch(IOExceptione1){
e1.printStackTrace();
}
}

});

//文本域加到容器

c.add(jsp);
//按鈕加到面板再加到容器
p.add(jb2);
c.add(BorderLayout.SOUTH,p);
jf.setBounds(430,150,500,300);
jf.setVisible(true);

主要代碼,其實你使用read(char[] c)這個方法,它會把讀取到的內容寫到「c」這個數組裡面,並且返回一int類型的長度給你,還有那個f的路徑你自己選好

❹ 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文件並放到數組中

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中的數據存放到數組

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {
public static void main(String[] args) throws NumberFormatException,
IOException {
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
String line = "";
List<Integer> list = new ArrayList<Integer>();
while ((line = reader.readLine()) != null) {
list.add(Integer.parseInt(line));
}
reader.close();
Integer[] num = list.toArray(new Integer[list.size()]);
System.out.println(Arrays.toString(num));
}
}

❽ 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文件然後賦值二維數組

java讀取txt文件然後賦值二維數組實現方法如下:

package shi;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Test13 {

/**
* 讀取文件
* @param filePath
* @return
*/
public static List readTxtFile(String filePath) {
List<String> list = new ArrayList<String>();
try {
String encoding = "UTF-8";
File file = new File(filePath);
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
if (!lineTxt.startsWith("#"))
list.add(lineTxt);
}
read.close();
} else {
System.out.println("找不到文件");
}
} catch (Exception e) {
System.out.println("出錯了");
e.printStackTrace();
}
return list;

}

/**
* 創建二維數組
* @param list
* @return
*/
public static String[][] createArray(String filePath){
List<String> list = readTxtFile(filePath);
String array[][] = new String[list.size()][];
for(int i=0;i<list.size();i++){
array[i] = new String[3];
String linetxt=list.get(i);
String[] myArray = linetxt.replaceAll("\s+", "@").split("@");
for(int j=0;j<myArray.length;j++){
if(j<3){
array[i][j]=myArray[j];
}
}
}
return array;
}

/**
* 列印數組
* @param array
*/
public static void printArray(String array[][]){
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
if(j!=array[i].length-1){
System.out.print("array["+i+"]["+j+"]="+array[i][j]+",");
}
else{
System.out.print("array["+i+"]["+j+"]="+array[i][j]);
}

}
System.out.println();
}
}


public static void main(String args[]) {
String array[][] = createArray("F:\test1.txt");
printArray(array);
}


}

❿ 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;j>0;j--){
k[length-j]=Integer.parseInt(b.substring(2*(j-1), 2*j));
}
//列印數據
for(int m=0;m<k.length;m++){
System.out.println("k["+m+"]="+k[m]);
}

} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
}

閱讀全文

與java讀取txt數組相關的資料

熱點內容
伺服器怎麼用不會斷電 瀏覽:301
主從伺服器有什麼用 瀏覽:213
jstlpdf 瀏覽:14
安卓原神在哪個app下載 瀏覽:808
單片機編程技術什麼意思 瀏覽:104
e點課堂源碼 瀏覽:45
免費打擊墊app哪個好 瀏覽:532
程序員必裝的6款軟體 瀏覽:750
基於單片機的遙控器設計 瀏覽:521
安卓如何取消圓圖標 瀏覽:11
收件伺服器怎麼樣 瀏覽:48
建築設計規范pdf 瀏覽:98
如何合並兩個pdf 瀏覽:174
刷機包必須要解壓的單詞 瀏覽:483
android課表實現 瀏覽:864
頭條app在哪裡能看見有什麼活動 瀏覽:511
冰櫃壓縮機電容80歐 瀏覽:609
安卓各個版本圖標什麼樣 瀏覽:152
無錫哪裡有製作手機app 瀏覽:538
php字元串轉json數組 瀏覽:6