• IO流一行一行读取TXT文件


    我们在开发或者测试的时候,往往会用到读取本地txt文件内容来处理数据的情况。下面是读取本地txt文件内容,是一行一行读取。如下列txt例子

    小明 20

    小红 20

    小亮 20

    下面是代码:

    public void test1(){
                try {
                        String encoding="utf-8";//GBK
                        String filePath="/demo/RegionList_zh_CN.txt";//要读取的文件路径
                        
                        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;//每一行的文本内容
                            String cityId="";
                            String cityName_zhcn="";//中文名称
                            String cityNameLong_zhcn="";//中文名称,长名
                            
                            int i=0;
                            while((lineTxt = bufferedReader.readLine()) != null){
                                
                                try {
                                    String[] str=lineTxt.split("\|");
                                    
                                    if(str.length>=1){
                                        cityId=str[0];
                                    }else{
                                        cityId="";
                                    }
                                    
                                    if(str.length>=2){
                                        
                                    }else{
                                        
                                    }
                                    
                                    if(str.length>=3){
                                        cityName_zhcn=str[2];
                                    }else{
                                        cityName_zhcn="";
                                    }
                                    
                                    if(str.length>=4){
                                        cityNameLong_zhcn=str[3];
                                    }else{
                                        cityNameLong_zhcn="";
                                    }
                                    
                                    
                                    System.out.println("正在插入第"+i+"条数据......."+lineTxt);
                                    String sql="insert into z_sysCity_zhcn values (?,?,?)";
                                    jdbcTemplateService.update(sql, new Object[]{cityId,cityName_zhcn,cityNameLong_zhcn});
                                    
                                    i++;
                                } catch (Exception e) {
                                    // TODO: handle exception
                                }
                            }
                            read.close();
                }else{
                    System.out.println("找不到指定的文件");
                }
                } catch (Exception e) {
                    System.out.println("读取文件内容出错");
                    e.printStackTrace();
                }
             
            }
  • 相关阅读:
    【转】Redis主从复制简介
    Redis配置文件详解
    Redis在Windows环境下搭建
    Redis桌面管理工具 RedisDesktopManager
    Redis服务停止报错解决方案[NOAUTH Authentication required]
    Redis启动警告错误解决
    修改tcp内核参数:somaxconn
    CentOS6.8安装Redis3.2.5
    Github之协同开发
    自定义实现栈的功能
  • 原文地址:https://www.cnblogs.com/dongzhongwei/p/5964772.html
Copyright © 2020-2023  润新知