Home > Java > javaTutorial > How to read and write txt files in java?

How to read and write txt files in java?

青灯夜游
Release: 2019-11-16 15:12:29
Original
6926 people have browsed it

How to read and write txt files in java?

How to read txt file in java?

1. Use FileInputStream to read the content of the txt file

2. Use FileOutputStream to write the content of the txt file

package cn.xiaobing.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class ReadTxt {
    /**传入txt路径读取txt文件
     * @param txtPath
     * @return 返回读取到的内容
     */
    public static String readTxt(String txtPath) {
        File file = new File(txtPath);
        if(file.isFile() && file.exists()){
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                 
                StringBuffer sb = new StringBuffer();
                String text = null;
                while((text = bufferedReader.readLine()) != null){
                    sb.append(text);
                }
                return sb.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    

    /**使用FileOutputStream来写入txt文件
     * @param txtPath txt文件路径
     * @param content 需要写入的文本
     */
    public static void writeTxt(String txtPath,String content){    
       FileOutputStream fileOutputStream = null;
       File file = new File(txtPath);
       try {
           if(file.exists()){
               //判断文件是否存在,如果不存在就新建一个txt
               file.createNewFile();
           }
           fileOutputStream = new FileOutputStream(file);
           fileOutputStream.write(content.getBytes());
           fileOutputStream.flush();
           fileOutputStream.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
    }
}
Copy after login

3. Verification code

//验证方法:先写入文件后读取打印如下:
    public static void main(String[] args) {
        writeTxt("D:/yzm/result1.txt", "测试写入txt文件内容");
        String str = readTxt("D:/yzm/result1.txt");
        System.out.println(str);
    }
Copy after login

How to read and write txt files in java?

Explanation:

1. FileInputStream

FileInputStream is the abstract class InputStream in the Java language used to create concrete implementation classes object. FileInputStream can obtain input bytes from a file in the file system. The availability of the obtained file depends on the host environment.

The constructor of FileInputStream needs to specify the source of the file and creates a FileInputStream by opening a connection to the actual file, which is specified by the File object file in the file system.

2. FileOutputStream

FileOutputStream means file output stream, which is the output stream used to write data to File or FileDescriptor.

FileOutputStream A stream for writing raw bytes such as image data. To write to a character stream, consider using a FileWriter.

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

Related labels:
source:php.cn
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