文件是酱紫的:num.txt
2.3 1
3.5 2
2.2 0
.
.
.现在想将他们读出来,(一行中分别存到 double 跟int 型的变量里面)。 只是读出来这么简单。请教大哥了。 小弟刚从C++转营过来。//java 的IO还真TM麻烦,如果有fscanf 这样的就好了。

解决方案 »

  1.   

    这个简单,我随便写点了。BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream("a.txt")));
    String line = null;
    while((line=reader.readLine())!=null){
      double a  = Double.parseDouble(line);
    }只提供个思路,代码没测试
      

  2.   

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.util.Arrays;public class Main {
    public static void main(String[] args) throws Exception {
    double[] ds = new double[3];
    int[] ins = new int[3];
    int i = 0;
    BufferedReader br = new BufferedReader(new FileReader(new File(
    "d:/num.txt")));
    String s = "";
    while ((s = br.readLine()) != null) {
    String[] ss = s.split(" ");
    ds[i] = Double.parseDouble(ss[0]);
    ins[i++] = Integer.parseInt(ss[1]);
    }
    System.out.println(Arrays.toString(ds)+","+Arrays.toString(ins));
    }
    }
      

  3.   

    用正则表达式就很容易实现,在java中先import java.util.regex.*整数用:\\-?\\d+
    浮点数用: (\\-?\\d*)\\.?\\d+\\-:代表负号   ?:负号出现0次或1次 \\d:代表数字0-9 *:代表数字出现0到多次  \\.:代表小数点  +:代表数字出现一次或多次 
      

  4.   


    JAVA读取是很方便的啊。用Scanner就行了。代码如下:import java.util.*;
    import java.io.*;public class ReadTxt {
    public static void main(String[] args) throws Exception {
    // TODO 自动生成方法存根 Scanner sc=new Scanner(new File("c:/data.txt"));
    while(sc.hasNext())//还有数据吗?
    {
    System.out.println(sc.nextDouble()+"  "+sc.nextInt());//读每行的第一个实数,第二个整数
    }
    sc.close();
    }}运行结果:
    2.3  1
    3.5  2
    2.2  0
      

  5.   

    import java.io.*;
    public class ReadFile
    {
    public static void main(String args[])throws Exception
    {
    BufferedReader br=new BufferedReader(new FileReader("num.txt "));
    String s=null;
    while(br.ready())
    {
    s=br.readLine();
    System.out.println("所读入的字符串是: "+s);
    }

    }
    }然后在"num.txt"中输入,
    2.3 1
    3.5 2
    2.2 0 
    放在同一目录。
      

  6.   


    import java.io.*;/**
     *
     * @author scj
     */
    public class Main {    /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws Exception {
            // TODO code application logic here
            RandomAccessFile ra = new RandomAccessFile("a.txt", "rw");
            String s = "";
            while ((s = ra.readLine()) != null) {
                System.out.println(s);
            }
        }
    }
      

  7.   

    不知道这个行不行:
    ArrayList arrDouble = new ArrayList();
    ArrayList arrInt = new ArrayList();
    BufferedReader readFile = null;
    try
    {
    readFile = new BufferedReader(new FileReader(new File(
    "fileName")));
    String line = "";
    while ((line = readFile.readLine()) != null)
    {
    String[] arrList = line.split(" ");
    for (int i = 0; i < arrList.length; i++)
    {
    if (arrList[i].trim().length() == 0)
    {
    continue;
    }
    else if (Pattern.matches("^[0-9]{1,10}$", arrList[i]))
    {
    arrInt.add(Integer.valueOf(arrList[i]));
    }
    else if (Pattern.matches("^[0-9]{1,10}.[0-9]{1,10}$",
    arrList[i]))
    {
    arrDouble.add(Double.valueOf(arrList[i]));
    }
    else
    {
    System.out.println("Error");
    }
    }
    }
    }
    catch (Exception ex)
    {
    ex.printStackTrace();
    }
    finally
    {
    if (readFile != null)
    {
    readFile.close();
    }
    }