【总结收藏】JAVA文件读取常用工具类

简介: 【总结收藏】JAVA文件读取常用工具类

 JAVA操作文件在经常会使用到,本文汇总了部分JAVA操作文件的读取常用工具类,希望可以帮到大家。直接上代码。


一、读取文件成字节


  将文件内容转为字节,需要使用到FileInputStream文件字节输入流,将文件输入到文件字节输入流中,使用FileInputStream的available()方法获取与之关联的文件的字节数,然后使用read()方法读取数据,最后记得关闭文件字节流即可。


//读取文件成字节数组
  public static byte[] file2byte(String path){
        try {
            FileInputStream in =new FileInputStream(new File(path));
            byte[] data=new byte[in.available()];
            in.read(data);
            in.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


二、将字节写入文件


  与一中的读取文件成字节类似,字节写入文件使用FileOutputStream流,即可将字节写入到文件中。调用FileOutputStream的write()方法,写入数据,之后关流。


//将字节数组写入文件
  public static void byte2file(String path,byte[] data) {
        try {
            FileOutputStream outputStream  =new FileOutputStream(new File(path));
            outputStream.write(data);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


三、按行读取文件成list


  经常遇到需要将一个文档中的文本按行输出,这是我们可以使用BufferedReader 和 InputStreamReader流处理。具体代码如下。


//按行读取文件成list
  public static ArrayList<String> file2list(String path,String encoder) {
        ArrayList<String> alline=new ArrayList<String>();
        try {
            BufferedReader in =new BufferedReader(new InputStreamReader(new FileInputStream(path),encoder));
            String str=new String();
            while ((str=in.readLine())!=null) {
                alline.add(str);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return alline;
    }


四、输出list到文件


//输出list到文件
  public static void list2file(String path,ArrayList<String> data,String encoder) {
        try {
            BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),encoder));
            for (String str:data) {
                out.write(str);
                out.newLine();
            }
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


五、从标准输入中读入


//从标准输入中读入
  public static String system2str() throws IOException{
        BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
        return stdin.readLine();
    }


六、读取文件成字符串


//读取文件成字符串
  public static String file2str(String path,String encoder){
        StringBuilder sb=new StringBuilder();
        try {
            BufferedReader in =new BufferedReader(new InputStreamReader(new FileInputStream(path),encoder));
            String str=new String();
            while ((str=in.readLine())!=null) {
                sb.append(str);
            }
            in.close(); 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }


七、输出字符串到文件


//输出字符串到文件
  public static void str2file(String path,String data,String encoder){
        try {
            BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),encoder));
            out.write(data);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


八、读取文件成数据矩阵


//读取文件成数据矩阵
  public static ArrayList<Double> file2matrix(String path){
        ArrayList<Double> alldata=new ArrayList<Double>();
        try {
            DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream(path)));
            //利用DataInputStream来读数据
            while(true)
            {
                alldata.add(in.readDouble());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return alldata;
    }


总结


  对文件的操作方式还有很多,本文使用的只是一个基础的参考示例,欢迎学习交流。



目录
相关文章
|
29天前
|
Java
有关Java发送邮件信息(支持附件、html文件模板发送)
有关Java发送邮件信息(支持附件、html文件模板发送)
30 1
|
1月前
|
Java
java中替换文件内容
java中替换文件内容
14 1
|
3天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
27 3
|
7天前
|
Java 编译器
Java Character 类
4月更文挑战第13天
|
8天前
|
存储 Java
Java基础教程(7)-Java中的面向对象和类
【4月更文挑战第7天】Java是面向对象编程(OOP)语言,强调将事务抽象成对象。面向对象与面向过程的区别在于,前者通过对象间的交互解决问题,后者按步骤顺序执行。类是对象的模板,对象是类的实例。创建类使用`class`关键字,对象通过`new`运算符动态分配内存。方法包括构造函数和一般方法,构造函数用于对象初始化,一般方法处理逻辑。方法可以有0个或多个参数,可变参数用`类型...`定义。`this`关键字用于访问当前对象的属性。
|
12天前
|
Java Shell
Java 21颠覆传统:未命名类与实例Main方法的编码变革
Java 21颠覆传统:未命名类与实例Main方法的编码变革
13 0
|
12天前
|
Java
Java 15 神秘登场:隐藏类解析未知领域
Java 15 神秘登场:隐藏类解析未知领域
16 0
|
14天前
|
安全 Java
append在Java中是哪个类下的方法
append在Java中是哪个类下的方法
22 9
|
14天前
|
JavaScript Java 测试技术
基于Java的网络类课程思政学习系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的网络类课程思政学习系统的设计与实现(源码+lw+部署文档+讲解等)
30 0
基于Java的网络类课程思政学习系统的设计与实现(源码+lw+部署文档+讲解等)
|
15天前
|
存储 安全 Java
java多线程之原子操作类
java多线程之原子操作类