JAVA如何从TXT文本中 读取数据到一个二维数组

luwenjietc 2008-11-16 10:26:15
TXT文本中如据形如:
123
456
789

读入二维数组效果为:
temp[0][]={1,2,3};
temp[1][]={4,5,6};
temp[2][]={7,8,9};

新手
请大家指点
...全文
1507 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
z598751848 2012-05-13
  • 打赏
  • 举报
回复
split("\\d") 正则表达式应该是"\\ ",以空格为分割符
[Quote=引用 7 楼 的回复:]

超简单的问题
Java code

BufferedReader bf=new BufferedReader(new FileReader("Your file"));
String lineContent=null;
int i = 0;
int [][] temp = new int [3][];
while((lineContent=bf.readLine())!=nu……
[/Quote]
moshangchenzi 2008-11-18
  • 打赏
  • 举报
回复
楼主参阅http://blog.csdn.net/moshangchenzi/archive/2008/11/11/3270483.aspx

输入输出,对象流都写上了
renmms 2008-11-17
  • 打赏
  • 举报
回复
可以先读一行InLine(),

读出的是string类型的,

然后利用stringToken 把字符分析出来 放入数组
風男 2008-11-17
  • 打赏
  • 举报
回复
超简单的问题

BufferedReader bf=new BufferedReader(new FileReader("Your file"));
String lineContent=null;
int i = 0;
int [][] temp = new int [3][];
while((lineContent=bf.readLine())!=null){
String [] str = lineContent.split("\\d");// 将 lineContent 按数字拆分
for(int j = 0; j < str.length(); j++){
int [i][j] = Integer.parseInt(str[j]);
}
i++;
}
king_spf 2008-11-17
  • 打赏
  • 举报
回复
用BufferedReader读取一行,然后分割,转成int类型后存入数组
ZiSheng 2008-11-17
  • 打赏
  • 举报
回复

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.*;


public class xx{
public static void main(String[]args){
String s;
int[][]save=new int[3][3];
try{
BufferedReader in =new BufferedReader(new FileReader("C:\\txt.txt"));
int i=0;
while((s=in.readLine())!=null){
save[i][0]=Integer.parseInt(s.substring(0,1));
save[i][1]=Integer.parseInt(s.substring(1,2));
save[i][2]=Integer.parseInt(s.substring(2,3));
i++;
}
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++){
System.out.print(save[i][j]);
}
System.out.println();
}
}
}
erosyin 2008-11-17
  • 打赏
  • 举报
回复
测试类


package org.eros.sharon;

import java.io.File;

import org.junit.Test;

public class TestArray {

@Test
public void test() {
int[][] temp = ArrayConvert.getArray("d:/1.txt");
for (int i = 0; i < temp.length; i++) {
for (int j = 0; j < temp[i].length; j++) {
System.out.print(temp[i][j] + " ");
}
System.out.println();
}

}

@Test
public void test1() {
String str = ";";
int[][] temp = ArrayConvert1.getArray(new File("d:/2.txt"), str);
for (int i = 0; i < temp.length; i++) {
for (int j = 0; j < temp[i].length; j++) {
System.out.print(temp[i][j] + " ");
}
System.out.println();
}

}

}

1.txt
123
456
789

2.txt
121;343;3;
232;3;23;
23;4;533;
erosyin 2008-11-17
  • 打赏
  • 举报
回复
package org.eros.sharon;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import java.util.List;

public class ArrayConvert1 {

public static int[][] getArray(File file) {
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String str = null;
List<int[]> list = new ArrayList<int[]>();
while ((str = br.readLine()) != null) {
if (str.length() > 0) {// 去掉空行
int[] temp = toArray(str.toCharArray());
list.add(temp);
}
}
return list.toArray(new int[][] {});
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

public static int[][] getArray(String path) {
return getArray(new File(path));
}

private static int[] toArray(char[] args) {
int[] temp = new int[args.length];
for (int i = 0; i < args.length; i++) {
temp[i] = args[i] - '0';
}
return temp;
}

private static int[] toArray(String[] args) {
int[] temp = new int[args.length];
for (int i = 0; i < args.length; i++) {
temp[i] = Integer.valueOf(args[i]);
}
return temp;
}

/**
*
* @param path
* 文件路径
* @param regex
* 分割符 ArrayConvert 按照指定的分割符进行数组转换
* @return
*/
public static int[][] getArray(File file, String regex) {
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String str = null;
List<int[]> list = new ArrayList<int[]>();
while ((str = br.readLine()) != null) {
if (str.length() > 0) {// 去掉空行
int[] temp = toArray(str.split(regex));
list.add(temp);
}
}
return list.toArray(new int[][] {});
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

public static int[][] getArray(String path,String regex){
return getArray(path,regex);
}

}
shengtianyei 2008-11-17
  • 打赏
  • 举报
回复
1楼:read()
从此输入流中读取下一个数据字节。
一个数字是四字节,好像用read()不好实现
hy0231 2008-11-17
  • 打赏
  • 举报
回复
1楼的不全对,应该用String input = fis.readLine(); 读取一行,然后将这行分解存入二维数组中.
zhengxin070513 2008-11-17
  • 打赏
  • 举报
回复
public static void getStringData(String path){
try {
String str="";
BufferedReader br=new BufferedReader(new FileReader(path));
int len;
int k=0;
char[][] data=new char[100][100];
char[] temp=new char[100];
while((len=br.read(temp))!=-1){
for(int i=0;i<100;i++){
data[k][i]=temp[i];
}
k++;
}
for(int i=0;i<k;i++){
for(int j=0;j<100;j++){
System.out.print(data[i][j]);
if(j==99){
System.out.println();
}
}
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
zhengxin070513 2008-11-17
  • 打赏
  • 举报
回复
public static void getStringData(String path){
try {
String str="";
BufferedReader br=new BufferedReader(new FileReader(path));
int len;
int k=0;
char[][] data=new char[100][100];
char[] temp=new char[100];
while((len=br.read(temp))!=-1){
for(int i=0;i<100;i++){
data[k][i]=temp[i];
}
k++;
}
for(int i=0;i<k;i++){
for(int j=0;j<100;j++){
System.out.print(data[i][j]);
if(j==99){
System.out.println();
}
}
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
zhengxin070513 2008-11-17
  • 打赏
  • 举报
回复
public static void getStringData(String path){
try {
String str="";
BufferedReader br=new BufferedReader(new FileReader(path));
int len;
int k=0;
char[][] data=new char[100][100];
char[] temp=new char[100];
while((len=br.read(temp))!=-1){
for(int i=0;i<100;i++){
data[k][i]=temp[i];
}
k++;
}
for(int i=0;i<k;i++){
for(int j=0;j<100;j++){
System.out.print(data[i][j]);
if(j==99){
System.out.println();
}
}
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
luwenjietc 2008-11-16
  • 打赏
  • 举报
回复
代码越详细越好
感激不尽
风沙无痕 2008-11-16
  • 打赏
  • 举报
回复
一次读一个字节。然后将这个字节赋值给数组中的一个元素。

int temp[][] = new int[100][100];

FileInputStream fis = new FileInputStream("c:\\1.txt");
int input;
while(true){

input = fis.read();
if(input == -1){
break;
}
//将input赋值给数组中元素。

}

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧