zoukankan      html  css  js  c++  java
  • java读取txt文件

    /**
         * 功能:Java读取txt文件的内容
         * 步骤:1:先获得文件句柄
         * 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
         * 3:读取到输入流后,需要读取生成字节流
         * 4:一行一行的输出。readline()。
         * 备注:需要考虑的是异常情况
         * @param filePath

         *String filePath = "\\172.16.4.148\TransOut\mytxt.out"; 

         */
        public 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.print(lineTxt);
                    }
                    read.close();
           }else{
               System.out.println("***找不到指定的文件***");
           }
            } catch (Exception e) {
                System.out.println("读取文件内容出错");
                e.printStackTrace();
            }
         
        }
  • 相关阅读:
    CSS标题线(删除线贯穿线效果)实现之一二
    sublime修改快捷键样式
    使用css鼠标移动到图片放大效果
    js判断苹果安卓操作系统,js更换css
    清除缓存
    mui下拉上拉(明一)
    mui下拉刷新上拉加载
    Cypher学习笔记
    github上计算String相似度好的项目
    代码重构
  • 原文地址:https://www.cnblogs.com/lizuoqi/p/4656306.html
Copyright © 2011-2022 走看看