Java 文件处理简单工具函数

   ## Java 文件处理简单工具函数 ##
   **在日常生活中,遇到一些关于文件处理的简单问题,总结了一些工具函数。
   1.能得到某个文件夹下所有文件的路径——set保存
   2.获取文件的后缀名——可以用来筛选**

1:获取文件夹下所有文件路径(不包括文件夹本身), 参数File为文件夹路径,返回值set保存了所有文件路径

    public static Set<String> getFile(File file){

        Set<String> set=new HashSet<String>();
        if (file.isFile()) {
            set.add(file.getAbsolutePath());
        } else {
            for(File f:file.listFiles()) {
                set.addAll(getFile(f));
            }
        }
        return set;
    }

2.获取文件后缀名,参数可以是文件路径也可是文件名,返回值为后缀名(不包括 “.” )

    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/** * 一、BufferedReader类 public class BufferedReader extends Reader * 从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。 可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值足够大。 * 通常,Reader 所作的每个读取请求都会导致对底层字符或字节流进行相应的读取请求。因此,建议用 BufferedReader包装所有其 read() * 操作可能开销很高的 Reader(如 FileReader和 InputStreamReader)。 * BufferedReader流能够读取文本行,通过向BufferedReader传递一个Reader对象 * ,来创建一个BufferedReader对象,之所以这样做是因为FileReader没有提供读取文本行的功能. * * 二、InputStreamReader类 * * InputStreamReader 将字节流转换为字符流。是字节流通向字符流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK。 * * 构造方法: * * InputStreamReader isr = new InputStreamReader(InputStream * in);//构造一个默认编码集的InputStreamReader类 * * InputStreamReader isr = new InputStreamReader(InputStream in,String * charsetName);//构造一个指定编码集的InputStreamReader类。 * * 参数 in对象通过 InputStream in = System.in;获得。//读取键盘上的数据。 * * 或者 InputStream in = new FileInputStream(String fileName);//读取文件中的数据。可以看出 * FileInputStream 为InputStream的子类。 * * 主要方法:int read();//读取单个字符。 int read(char []cbuf);//将读取到的字符存到数组中。返回读取的字符数。 * * 三、FileWriter(少量文字) 和 BufferedWriter(大量文字)实现简单文件写操作 * @author hulk */
package com.hexiang.utils; import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /** * Read content from local file. FIXME How to judge UTF-8 and GBK, the * correct code should be: FileReader fr = new FileReader(new * InputStreamReader(fileName, "ENCODING")); Might let the user select the * encoding would be a better idea. While reading UTF-8 files, the content * is bad when saved out. * * @param fileName - * local file name to read * @return * @throws Exception */ public static String readFileAsString(String fileName) throws Exception { String content = new String(readFileBinary(fileName)); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(String fileName, String encoding) throws Exception { String content = new String(readFileBinary(fileName), encoding); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(InputStream in) throws Exception { String content = new String(readFileBinary(in)); return content; } /** * Read content from local file to binary byte array. * * @param fileName - * local file name to read * @return * @throws Exception */ public static byte[] readFileBinary(String fileName) throws Exception { FileInputStream fin = new FileInputStream(fileName); return readFileBinary(fin); } /** * 从输入流读取数据为二进制字节数组. * @param streamIn * @return * @throws IOException */ public static byte[] readFileBinary(InputStream streamIn) throws IOException { BufferedInputStream in = new BufferedInputStream(streamIn); ByteArrayOutputStream out = new ByteArrayOutputStream(10240); int len; while ((len

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值