java读取.txt文件工具类FileUtiles

public class FileUtils {

        private static final String ENCODING = "UTF-8";//编码方式

        /**
         * 获取文件的行
         *
         * @param fileName
         *            文件名称
         * @return List<String>
         */
        public static String getContentByLine(String fileName) {
            StringBuffer lines = new StringBuffer();
            InputStreamReader read = null;
            BufferedReader bufferedReader = null;
            try {
                String configPath = FileUtils.class.getClassLoader().getResource(fileName).getPath();
                configPath = configPath.replaceAll("%20", " ");// 处理文件路径中空格问题
                File file = new File(configPath);
                if (file.isFile() && file.exists()) { // 判断文件是否存在
                    read = new InputStreamReader(new FileInputStream(file), ENCODING);
                    bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    while ((lineTxt = bufferedReader.readLine()) != null) {
                        if (lineTxt == null || lineTxt.length() == 0) {
                            continue;
                        }
                        lines.append(lineTxt);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (read != null) {
                        read.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (bufferedReader != null) {
                        try {
                            bufferedReader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            return lines.toString();
        }
    }

 

转载于:https://www.cnblogs.com/pypua/p/9991668.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java读取.txt文件的内容的示例代码: ```java import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class ReadFile { public static void main(String[] args) { File file = new File("file.txt"); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } } } } ``` 这个例子中,我们使用了Java的BufferedReader类来读取文件。BufferedReader类可以一行一行地读取文件内容,并且具有缓冲功能,可以提高读取效率。 首先,我们创建了一个File对象,它表示我们要读取文件。然后,我们创建了一个BufferedReader对象,并将其包装在一个FileReader对象中,这样我们就可以使用BufferedReader的readLine()方法一行一行地读取文件内容了。当读取文件末尾时,readLine()方法会返回null,此时我们就可以退出循环了。 在读取文件时,我们还需要处理可能出现的IOException异常。为了保证资源的释放,我们在finally块中关闭了BufferedReader对象。 需要注意的是,我们在这个例子中使用的是相对路径来指定文件,也就是说,我们假设文件与类文件位于同一目录下。如果文件位于其他目录下,我们需要使用绝对路径或相对于当前工作目录的相对路径来指定文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值