JAVA文本读取二维数组
如果想用JAVA从一个TXT文本中读取字符串变量,比如说id,name,sex01,wu,male03,li,female02,zhu,female...存储在二维数组中...
如果想用JAVA从一个TXT文本中读取字符串变量,比如说
id,name,sex
01,wu,male
03,li,female
02,zhu,female
...
存储在二维数组中,比如说a[1][0]="01",a[1][1]="wu"之类的
需要怎么做,最好有代码 展开
id,name,sex
01,wu,male
03,li,female
02,zhu,female
...
存储在二维数组中,比如说a[1][0]="01",a[1][1]="wu"之类的
需要怎么做,最好有代码 展开
2个回答
展开全部
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Test {
private String[][] sDblArr = null;
private int i = 0;
private BufferedReader in = null;
private File file = null;
public Test(File file) {
this.file = file;
try {
inti();
} catch (IOException e) {
e.printStackTrace();
}
}
private void inti() throws IOException {
try {
in = new BufferedReader(new FileReader(file));
int length = 0;
while (in.readLine() != null)
length++;
sDblArr = new String[length][3];
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null)
in.close();
}
}
private void put2Arr(String s) {
String[] sArr = s.trim().split(",");
if (sArr.length == 3) {
sDblArr[i][0] = sArr[0];
sDblArr[i][1] = sArr[1];
sDblArr[i][2] = sArr[2];
i++;
}
}
public String[][] action() {
try {
in = new BufferedReader(new FileReader(file));
String s = null;
while ((s = in.readLine()) != null)
put2Arr(s);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sDblArr;
}
public static void main(String[] args) {
Test t = new Test(new File("C:/a.txt"));
//下面,返回并打印二维数组String Double Array ==>sDblArr
String[][] sDblArr = t.action();
for (String[] sArr : sDblArr) {
for (String s : sArr) {
System.out.print(s + "\t");
}
System.out.println();
}
}
}
其中C:/a.txt中的内容为:
id,name,sex
01,wu,male
03,li,female
02,zhu,female
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Test {
private String[][] sDblArr = null;
private int i = 0;
private BufferedReader in = null;
private File file = null;
public Test(File file) {
this.file = file;
try {
inti();
} catch (IOException e) {
e.printStackTrace();
}
}
private void inti() throws IOException {
try {
in = new BufferedReader(new FileReader(file));
int length = 0;
while (in.readLine() != null)
length++;
sDblArr = new String[length][3];
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null)
in.close();
}
}
private void put2Arr(String s) {
String[] sArr = s.trim().split(",");
if (sArr.length == 3) {
sDblArr[i][0] = sArr[0];
sDblArr[i][1] = sArr[1];
sDblArr[i][2] = sArr[2];
i++;
}
}
public String[][] action() {
try {
in = new BufferedReader(new FileReader(file));
String s = null;
while ((s = in.readLine()) != null)
put2Arr(s);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sDblArr;
}
public static void main(String[] args) {
Test t = new Test(new File("C:/a.txt"));
//下面,返回并打印二维数组String Double Array ==>sDblArr
String[][] sDblArr = t.action();
for (String[] sArr : sDblArr) {
for (String s : sArr) {
System.out.print(s + "\t");
}
System.out.println();
}
}
}
其中C:/a.txt中的内容为:
id,name,sex
01,wu,male
03,li,female
02,zhu,female
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询