服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - JAVA文件读取常用工具类(8种)

JAVA文件读取常用工具类(8种)

2021-12-07 13:39小阿杰 Java教程

JAVA操作文件在经常会使用到,本文汇总了部分JAVA操作文件的读取常用工具类,主要介绍了8种方法,具有一定的参考价值,感兴趣的可以了解一下

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

一、读取文件成字节

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//读取文件成字节数组
  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()方法,写入数据,之后关流。

?
1
2
3
4
5
6
7
8
9
10
//将字节数组写入文件
 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流处理。具体代码如下。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//按行读取文件成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到文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//输出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();
       }
   }

五、从标准输入中读入

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

六、读取文件成字符串

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//读取文件成字符串
  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();
    }

七、输出字符串到文件

?
1
2
3
4
5
6
7
8
9
10
11
//输出字符串到文件
 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();
       }
   }

八、读取文件成数据矩阵

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//读取文件成数据矩阵
  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;
    }

总结

对文件的操作方式还有很多,本文使用的只是一个基础的参考示例,到此这篇关于JAVA文件读取常用工具类(8种)的文章就介绍到这了,更多相关JAVA文件读取内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/6985172105709125640

延伸 · 阅读

精彩推荐