比如记事本里有三行三列数字:1  2  3
4  5  6
7  8  9
怎么用java程序把里面的数据分别读出来再赋值给其他变量?比如我想把4,5,6依次拿出来赋值给int a,b,c?希望大家能帮忙,谢过先

解决方案 »

  1.   


    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;public class Test { public static void main(String[] args) {
    File f = new File("E:/A/Score.txt"); try {
    BufferedReader dis = new BufferedReader(new FileReader(f));
    String line = null;
    while ((line = dis.readLine()) != null) {
    String[] arr = line.trim().split("\\s+");
    int a = Integer.parseInt(arr[0]);
    int b = Integer.parseInt(arr[1]);
    int c = Integer.parseInt(arr[2]);
    }
    dis.close(); } catch (IOException e) {
    e.printStackTrace();
    }
    }}
      

  2.   

    import java.io.*;
    class Test{
        public static void main(String[] args)throws IOException{
    BufferedReader reader = new BufferedReader(new FileReader("test.txt"));
    String line = reader.readLine();
    line = reader.readLine();
    String[] num = line.split("\\s");
    int a = Integer.parseInt(num[0]);
    int b = Integer.parseInt(num[1]);
    int c = Integer.parseInt(num[2]);     
    reader.close();
    System.out.println(a+" "+b+" "+c);
        }}
      

  3.   

       public static void main(String [] args){
       File file=new File("file1.txt");
       try{
      FileReader fd=new FileReader(file);
          BufferedReader br=new BufferedReader(fd);
          String txt="";
          String str="";
          while((str=br.readLine())!=null){
           txt+=str;
          }
          br.close();
          fd.close();
          char[]ch=txt.toCharArray();
          int a=Integer.parseInt(String.valueOf(ch[3]));
          int b=Integer.parseInt(String.valueOf(ch[4]));
          int c=Integer.parseInt(String.valueOf(ch[5]));
          System.out.println(a+" "+b+" "+c);
       }catch(Exception e){
       e.printStackTrace();
       }
       }