要读的文件如下,名字叫3.txt。3
Rajgarh, Rajasthan
75.38
28.63
Mbini, Equatorial Guinea
9.62
1.58
Divo, Ivory Coast
-5.37
5.85第一行表明这个文件中有多少个样本,我要做的是用第一行的数据创建几个对象,然后从第二行开始读入数据,并把数据附给这几个对象。现在就不知道该怎么样从第二行开始读?各位大侠帮帮忙吧!

解决方案 »

  1.   

    把这个文件写成xml文件就好办了,因java解析xml文件很自如.
      

  2.   


    Home > List of Packages > java.io  [35 examples] > Reading and Writing  [6 examples]e35. Reading Text from a File    try {
            BufferedReader in = new BufferedReader(new FileReader("C:\\a.txt"));
            String str;
            int line = 0;
            while ((str = in.readLine()) != null) {
                line++;
                if(line==1) continue;
                // process(str);
            }
            in.close();
        } catch (IOException e) {
        }
      

  3.   

    masse(当午)你的方法我想过了,可是我现在创建的对象是这样的:
    public class City { 
    String name;
    double latCord;
    double longCord;
    }
    如果我要给3个对象赋值,没法在while循环里做,我需要用一个for循环,像这样
    for(int i=1; i<cityNum+1; i++){
    cities[i].name = in.readLine();
    cities[i].latCord = Double.parseDouble(in.readLine());
    cities[i].longCord = Double.parseDouble(in.readLine());
    }你给看看该怎么解决阿?
      

  4.   

    public class City { 
    String name;
    double latCord;
    double longCord;
    }
    如果我要给3个对象赋值,没法在while循环里做,我需要用一个for循环,像这样in.readLine();  //多读一行,就完了呗
    for(int i=1; i<cityNum+1; i++){
    cities[i].name = in.readLine();
    cities[i].latCord = Double.parseDouble(in.readLine());
    cities[i].longCord = Double.parseDouble(in.readLine());
    }
      

  5.   

    public class BufferedReaderTest
    {
        public static void main(String[] args)
        {
            
            /**
             * 3.txt
             * 3
             * Rajgarh, Rajasthan
             * 75.38
             * 28.63
             * Mbini, Equatorial Guinea
             * 9.62
             * 1.58
             * Divo, Ivory Coast
             * -5.37
             * 5.85
             */
            
            
            
            try
            {
                BufferedReader reader = new BufferedReader(new FileReader("C:\\3.txt"));
                
                String readline = reader.readLine();
                
                int length = Integer.parseInt(readline);
                City[] citys = new City[length];
                while(length > 0)
                {
                    String name = reader.readLine();
                    double latCord = Double.parseDouble(reader.readLine());
                    double longCord = Double.parseDouble(reader.readLine());
                    citys[citys.length - length] = new City(name, latCord, longCord);
                    length--;
                }
                reader.close();
                
                for(City c : citys)
                {
                    System.out.println(c);
                }
            }
            catch (FileNotFoundException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                System.err.println("文件内容缺失!!!");
            }
        }
    }class City { 
        String name;
        double latCord;
        double longCord;
        
        public City(String name, double latCord, double longCord)
        {
            this.name = name;
            this.latCord = latCord;
            this.longCord = longCord;
        }
        
        public String toString()
        {
            return "name=" + name
                + ",latCord=" + latCord
                + ",longCord=" + longCord;
        }
    }你真的很弱
      

  6.   

    多谢你们的解答阿,我是新手,编这个程序费死劲了,现在我的程序是这样,还是出现Exception in thread "main" java.lang.NullPointerException
    at GPS.main(GPS.java:111)的错误,我知道是全局变量和局部变量的问题,可怎么改啊,都改了一个小时了。多谢多谢!
    import java.util.*;
    import java.io.*;
    import java.lang.Math;public class GPS {

    public static String getFile(){        
      String fName = "";
      try{
      InputStreamReader isr = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);
      System.out.println("Enter the file you want to read");
      fName = br.readLine(); 
      }
      catch(IOException ioe){
      System.err.println("IOException: " + ioe.getMessage());
      }    
      return fName;
      }
      
    public static int getCityNum(String fName){
        /********** determine how many cities ***********/
    int tempCityNumInt = 0;
    try {
         BufferedReader in=new BufferedReader(
         new FileReader(fName));  
         String tempCityNum=in.readLine();
         tempCityNumInt = Integer.parseInt(tempCityNum);
          in.close();
        
          }catch(Exception e){
          System.out.println(e);
          e.printStackTrace();
          }
          return tempCityNumInt;   
      }
      
    public static void initCity(String fName){
    try {
         BufferedReader in=new BufferedReader(
         new FileReader(fName));  
        
    String readline = in.readLine(); int length = Integer.parseInt(readline);
    City[] cities = new City[length];

             System.out.println("Come here");
             
                while(length > 0)
                {
                 String[] temp = in.readLine().split(",");
                
                    String name = temp[0];
                    String state = temp[1];
                    double latCord = Double.parseDouble(in.readLine());
                    double longCord = Double.parseDouble(in.readLine());
                    cities[cities.length - length] = new City(name, name, latCord, longCord);
                    length--;
                }
                in.close();

       }catch(Exception e){
          System.out.println(e);
          e.printStackTrace();
          } }
      
      
    public static void main(String[] args) throws IOException{

    String fileName = "";
    String cityNum = "";
    int cityNumInt = 0;
     
    fileName = getFile();

            System.out.println("The file being read is " + fileName);
            System.out.println("......................");
            
            cityNumInt = getCityNum(fileName);
           
            City cities[] = new City[cityNumInt];
          
            System.out.println(cityNumInt + "Cities Objects created");        initCity(fileName);
           
            for(int i = 1; i<4; i++){
            System.out.println("cities[i].name = " + cities[i].name);
            //System.out.println("cities[i].state = " + cities[i].state);
            System.out.println("cities[i].latCord = " + cities[i].latCord);
            System.out.println("cities[i].longCord = " + cities[i].longCord);
            }      
            
    }}