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

    代码如下:

    package com.work.javaio;
    
    import java.io.*;
    
    public class ReadTxt {
        public static void readTxtFile(String filePath){
            try{
                String encoding = "UTF-8";
                File file = new File(filePath);
                if(file.isFile() && file.exists()){
                    FileInputStream fileInputStream = new FileInputStream(file);
                    InputStreamReader read = new InputStreamReader(fileInputStream,encoding);
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    while ((lineTxt = bufferedReader.readLine()) != null){
                        System.out.println(lineTxt);
                    }
                    read.close();
                }else{
                    System.out.println("找不到指定的文件");
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            String filePath = "D:\temp\hello.txt";
            readTxtFile(filePath);
        }
    }
  • 相关阅读:
    rjust()方法
    rindex()方法
    rfind()方法
    replace()方法
    min(S)函数
    max(S)函数
    maketrans()方法
    lstrip()方法
    lower()方法
    eclipse Tomcat 启动报错
  • 原文地址:https://www.cnblogs.com/LoganChen/p/14059822.html
Copyright © 2011-2022 走看看