java读取txt文件

阅读更多
 public static void readTxtFile(String filePath){
            try {
                    String encoding="GBK";
                    File file=new File(filePath);
                    if(file.isFile() && file.exists()){ //判断文件是否存在
                        InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
                        BufferedReader bufferedReader = new BufferedReader(read);
                        String lineTxt = null;
                        while((lineTxt = bufferedReader.readLine()) != null){
                           // System.out.println(lineTxt);//获取的就是逐行输出
                            if(lineTxt.equals("---------------------")){
                                System.out.println("省份");
                            }else{
                                String[] cities=lineTxt.split("-");
                                String city=cities[0];
                                String pin=cities[1];
                                String sql="insert into t_departure_city(city_code,city_name,province_code,province_name,isPopular) values('"+pin+"','"+city+"','"+pin+"','"+city+"','0"+"');";
                                System.out.println(sql);
                            }
                          
                        }
                        read.close();
            }else{
                System.out.println("找不到指定的文件");
            }
            } catch (Exception e) {
                System.out.println("读取文件内容出错");
                e.printStackTrace();
            }
        
        }

 

你可能感兴趣的:(txt,文件读取)