JAVA 读取文件一行整数怎么转换为一行数组
如number.txt里的两行整数用空格隔开:12345678910111213141516171819这样怎么读进去intb[]数组中,请高手帮帮忙拉!!...
如number.txt里的两行整数 用空格隔开:
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
这样怎么读进去int b[]数组中,请高手帮帮忙拉!! 展开
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
这样怎么读进去int b[]数组中,请高手帮帮忙拉!! 展开
1个回答
展开全部
//package com.color.program;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ReadTxtToArray {
public static void main(String[] args) throws IOException{
File f = new File("D:\\abc.txt");
InputStream input = new FileInputStream(f);
BufferedReader b = new BufferedReader(new InputStreamReader(input));
StringBuffer buffer = new StringBuffer();
String value = b.readLine();
while(value != null){
buffer.append(" "+value);
value = b.readLine();
}
String[] temp = buffer.toString().replaceFirst(" ","").split("\\s+");
int[] number = new int[temp.length];
for(int i=0;i<temp.length;i++){
try{
number[i] = Integer.parseInt(temp[i]);
System.out.print(number[i]+" ");
}catch(Exception e){
System.out.println("文件中存在非数字字符!");
}
}
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ReadTxtToArray {
public static void main(String[] args) throws IOException{
File f = new File("D:\\abc.txt");
InputStream input = new FileInputStream(f);
BufferedReader b = new BufferedReader(new InputStreamReader(input));
StringBuffer buffer = new StringBuffer();
String value = b.readLine();
while(value != null){
buffer.append(" "+value);
value = b.readLine();
}
String[] temp = buffer.toString().replaceFirst(" ","").split("\\s+");
int[] number = new int[temp.length];
for(int i=0;i<temp.length;i++){
try{
number[i] = Integer.parseInt(temp[i]);
System.out.print(number[i]+" ");
}catch(Exception e){
System.out.println("文件中存在非数字字符!");
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询