Home > Java > javaTutorial > body text

How to implement reading and reading of files in java

王林
Release: 2019-12-28 11:54:32
forward
3344 people have browsed it

How to implement reading and reading of files in java

一、文档读取

1、将文件读取为String

public static String TxtToString(File file) {
    String result = "";
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        //构造一个BufferedReader类来读取文件
        String s = null;
        while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行
            result = result + "\n" + s;
        }
        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
Copy after login

2、将文件读取为List集合(按行)(免费视频教程分享:java免费视频教程

public static List<String> TxtToStringList(File file) {
    List<String> result = new ArrayList<>();
    try {
        if (!file.exists()){
            return null;
        }
        BufferedReader br = new BufferedReader(new FileReader(file));
        //构造一个BufferedReader类来读取文件
        String s = null;
        while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行
            result.add(s);
        }
        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
Copy after login

二、Java存储文件

1、将list按行写入到txt文件中

public static void writeFileContext(List<String> strings) throws Exception {
    File file = new File("D:\\IntellijIDEAProject\\KeChenSheJi\\data\\WordLibrary_index");
    //如果没有文件就创建
    if (!file.isFile()) {
        file.createNewFile();
    }
    BufferedWriter writer = new BufferedWriter(new FileWriter
    ("D:\\IntellijIDEAProject\\KeChenSheJi\\data\\WordLibrary_index"));
    for (String l:strings){
        writer.write(l + "\r\n");
    }
    writer.close();
}
Copy after login

2、按照名字将string类型的集合存入文件

public static void writeFileContext_Find(List<String> strings,String name) throws Exception {
    File file = new File("D:\\IntellijIDEAProject\\KeChenSheJi\\data\\wordIndex");
    //如果没有文件就创建
    if (!file.isFile()) {
        file.createNewFile();
    }
    BufferedWriter writer = new BufferedWriter(new FileWriter
    ("D:\\IntellijIDEAProject\\KeChenSheJi\\data\\wordIndex\\"+name));
    for (String l:strings){
        writer.write(l + "\r\n");
    }
    writer.close();
Copy after login

3、将Sting类型的list集合按文件地址存储

public static void writeFileContext_Found(List<String> strings,String filename) throws Exception {
    File file = new File(filename);
    //如果没有文件就创建
    if (!file.isFile()) {
        file.createNewFile();
    }
    BufferedWriter writer = new BufferedWriter(new FileWriter
    ("D:\\IntellijIDEAProject\\KeChenSheJi\\data\\file_index\\"+file.getName()));
    for (String l:strings){
        writer.write(l + "\r\n");
    }
    writer.close();
}
Copy after login

相关文章教程推荐:java快速入门

The above is the detailed content of How to implement reading and reading of files in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template