一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

java怎么读第二行?java如何跳过第一行从第二行开始读?

时间:2017-02-16 编辑:简简单单 来源:一聚教程网

 代码如下复制代码
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;
    }
}

热门栏目