JAVA如何从TXT文本中 读取数据到一个二维数组 TXT文本中如据形如:123456789读入二维数组效果为:temp[0][]={1,2,3};temp[1][]={4,5,6};temp[2][]={7,8,9};新手请大家指点 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一次读一个字节。然后将这个字节赋值给数组中的一个元素。 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赋值给数组中元素。 } 1楼的不全对,应该用String input = fis.readLine(); 读取一行,然后将这行分解存入二维数组中. 1楼:read() 从此输入流中读取下一个数据字节。一个数字是四字节,好像用read()不好实现 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(); } }} 用BufferedReader读取一行,然后分割,转成int类型后存入数组 超简单的问题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++; } 可以先读一行InLine(),读出的是string类型的,然后利用stringToken 把字符分析出来 放入数组 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(); } } 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(); } } 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(); } } 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); }} 测试类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.txt1234567892.txt121;343;3;232;3;23;23;4;533; 楼主参阅http://blog.csdn.net/moshangchenzi/archive/2008/11/11/3270483.aspx输入输出,对象流都写上了 split("\\d") 正则表达式应该是"\\ ",以空格为分割符 一道匹配算法题,求解 新手请教一个窗体事件小程序。。。 Acegi退出问题 求助!!!用线程控制划随鼠标移动随即显示的线,报错了! 这个异常是什么意思啊 问一个问题 求救啊马上给分 高手请进!!!!!!!!!!!!!!! 同一页面的两个APPLET,希望通过一个让另一个自动刷新,都不是图象 怎样用actionPerformed方法,创建jlabel,特急!问了好多人,都不会. jbuilder5的database piolt的中文问题 FileInputStream,FileOutputStream在控制台中文显示乱码,如何解决? new--Random中nextInt问题
FileInputStream fis = new FileInputStream("c:\\1.txt");
int input;
while(true){
input = fis.read();
if(input == -1){
break;
}
//将input赋值给数组中元素。
}
从此输入流中读取下一个数据字节。
一个数字是四字节,好像用read()不好实现
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();
}
}
}
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++;
}
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();
}
}
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();
}
}
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();
}
}
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);
}}
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
7892.txt
121;343;3;
232;3;23;
23;4;533;