java中如何从一个TXT文件中读取数字然后存入一维数组?数字有一位和两位的,数字之间有空格隔开
3个回答
展开全部
public static void main(String arg[]) {
try {
String encoding = "UTF-8"; // 字符编码GBK,GB2312(可解决中文乱码问题 )
File file = new File("c:/aa.txt");
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTXT = null;
String allNumString = "";
int[] array = null;
while ((lineTXT = bufferedReader.readLine()) != null) {
// 读出的每一行
System.out.println(lineTXT.toString().trim());
allNumString += lineTXT+",";
}
if(allNumString != null && !"".equals(allNumString)){
String[] numbers = allNumString.split(",");
array = new int[numbers.length];
for (int i = 0; i < numbers.length; i++) {
array[i] = Integer.parseInt(numbers[i]);
}
}
for (int i = 0; i < array.length; i++) {
System.out.println("数组["+i+"]:"+array[i]);
}
read.close();
} else {
System.out.println("找不到指定的文件!");
}
} catch (Exception e) {
System.out.println("读取文件内容操作出错");
e.printStackTrace();
}
}
try {
String encoding = "UTF-8"; // 字符编码GBK,GB2312(可解决中文乱码问题 )
File file = new File("c:/aa.txt");
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTXT = null;
String allNumString = "";
int[] array = null;
while ((lineTXT = bufferedReader.readLine()) != null) {
// 读出的每一行
System.out.println(lineTXT.toString().trim());
allNumString += lineTXT+",";
}
if(allNumString != null && !"".equals(allNumString)){
String[] numbers = allNumString.split(",");
array = new int[numbers.length];
for (int i = 0; i < numbers.length; i++) {
array[i] = Integer.parseInt(numbers[i]);
}
}
for (int i = 0; i < array.length; i++) {
System.out.println("数组["+i+"]:"+array[i]);
}
read.close();
} else {
System.out.println("找不到指定的文件!");
}
} catch (Exception e) {
System.out.println("读取文件内容操作出错");
e.printStackTrace();
}
}
追问
读取文件内容操作出错
java.lang.NumberFormatException: For input string: "33 29 8 48 4 37 10 31 0 21 1 15 49 20 28 19 34 35 2 27 30 7 25 6 42 23 22 47 5 26 50 45 11 36 16 3 46 17 13 24 12 40 18 39 41 43 14 44 32 38 9 33 "
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
。。。
能存进去,但是出错哦,怎么解决呢
追答
囧啊 用空格隔开 我看成是 ,
引用cn#upupQukap的回答:
package huang.test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadTxt
{
private static BufferedReader br;
public static void main(String[] args) throws IOException
{
br = new BufferedReader(new FileReader("D:test.txt"));
String s="";
String s1;
while((s1=br.readLine())!=null)
{
s=s+s1;
}
String [] data=s.split(" ");
int [] datas=new int [data.length];
//将String类型数组转成int类型
for(int j=0;j<data.length;j++)
{
datas[j]=Integer.parseInt(data[j]);
}
for(int i=0;i<datas.length;i++)
{
System.out.print(datas[i]+" ");
}
}
}
数据之间用一个空格格开,两个空格的出不出错我就没试过。求分!
package huang.test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadTxt
{
private static BufferedReader br;
public static void main(String[] args) throws IOException
{
br = new BufferedReader(new FileReader("D:test.txt"));
String s="";
String s1;
while((s1=br.readLine())!=null)
{
s=s+s1;
}
String [] data=s.split(" ");
int [] datas=new int [data.length];
//将String类型数组转成int类型
for(int j=0;j<data.length;j++)
{
datas[j]=Integer.parseInt(data[j]);
}
for(int i=0;i<datas.length;i++)
{
System.out.print(datas[i]+" ");
}
}
}
数据之间用一个空格格开,两个空格的出不出错我就没试过。求分!
展开全部
两个空格的会出错,刚才试了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package huang.test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadTxt
{
private static BufferedReader br;
public static void main(String[] args) throws IOException
{
br = new BufferedReader(new FileReader("D:test.txt"));
String s="";
String s1;
while((s1=br.readLine())!=null)
{
s=s+s1;
}
String [] data=s.split(" ");
int [] datas=new int [data.length];
//将String类型数组转成int类型
for(int j=0;j<data.length;j++)
{
datas[j]=Integer.parseInt(data[j]);
}
for(int i=0;i<datas.length;i++)
{
System.out.print(datas[i]+" ");
}
}
}
数据之间用一个空格格开,两个空格的出不出错我就没试过。求分!
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadTxt
{
private static BufferedReader br;
public static void main(String[] args) throws IOException
{
br = new BufferedReader(new FileReader("D:test.txt"));
String s="";
String s1;
while((s1=br.readLine())!=null)
{
s=s+s1;
}
String [] data=s.split(" ");
int [] datas=new int [data.length];
//将String类型数组转成int类型
for(int j=0;j<data.length;j++)
{
datas[j]=Integer.parseInt(data[j]);
}
for(int i=0;i<datas.length;i++)
{
System.out.print(datas[i]+" ");
}
}
}
数据之间用一个空格格开,两个空格的出不出错我就没试过。求分!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询