我有一个.txt文件,里面存有几百个1到100的数字(不知道具体多少个),中间都是用空格隔开的.我现在想把这些数字读出来存到一个数组或向量里,请问应该怎么做?我IO这部分学的不好,请多指教.谢谢.
解决方案 »
- 求高人指点,编译通不过,java和c混合编译的问题。
- 如何将网页内容发送到手机
- ant unzip 问题
- 请问swing里设置背景色的问题
- java.sql.SQLException: 对只转发结果集的无效操作: first ????一个异常!!!
- 新手求教 java 中ptivate作用是什么啊
- 为什么我每次必须要加-classpath 才能编译执行JAVA程序,这样设置classpath 有错吗(如下)
- 一个怪问题。
- 有哪个函数能把字符串转化成 int?
- 使用BufferedOutputStream的write(byte[] b, int off, int len) 是不是会复制不完整?
- 请教一个简单的连接数据库的问题,高手帮忙~
- 树结点数据实体不同,如何建造树.
import java.io.FileNotFoundException;
import java.io.IOException;public class FileToArray { public static void main(String[] args) {
// TODO Auto-generated method stub
int Temp;
String str="";
String mm[] = null;
try{
FileInputStream fis = new FileInputStream("G:\\workspace\\test\\filetoarray\\a.txt");
while((Temp = fis.read())!=-1){
str += (char)Temp;
}
mm = str.split(" ");
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
catch(IOException e){
System.out.println(e);
}
finally{
for(int i = 0; i< mm.length; i++)
{
System.out.println(mm[i]);
}
}
}}
File f = new File("test.txt") ;
BufferedReader in = new BufferedReader( new FileReader(f) ) ;
String s = in.readLine() ;
String[] ss = s.split(" ") ;
int[] count = new int[ss.length] ;
for (int i = 0; i<ss.length; i++)
{
count[i] = new Integer(ss[i]) ;
}
最后的count[]就是得到的数组
/**文件路径
* @param path
* @return
* @throws IOException
*/
public int[] readnumber(String path) throws IOException{
File file=new File(path);
if(!file.exists()||file.isDirectory())
throw new FileNotFoundException();
BufferedReader br=new BufferedReader(new FileReader(file));
String temp=null;
StringBuffer sb=new StringBuffer();
temp=br.readLine();
while(temp!=null){
sb.append(temp+" ");
temp=br.readLine();
}
String[] array_str=sb.toString().split(" ");
int[] array_int=new int[array_str.length];
for(int i=0;i<array_str.length;i++)
array_int[i]=Integer.parseInt(array_str[i]);
return array_int;
}