JayGao

JayGao 的个人博客,分享技术,记录生活,感谢您的支持与关注。

  menu
19 文章
0 浏览
0 当前访客
ღゝ◡╹)ノ❤️

文件字符流读取随笔

附件1:codedemo.zip

附件2:newCode.zip

实现文件读行并替换,代码:

package com.xiaofang520.codes.demo;

import java.io.*;
import java.util.Objects;

/**
 * 实现文件读行并替换
 *
 * @author Gao
 * @date 2022/6/22 15:19
 */
public class FileReaderOrWriterTest {

    public static void main(String[] args) throws IOException {
        String filePath = Objects.requireNonNull(Thread.currentThread().getContextClassLoader()
                .getResource("testdir")).getPath()+File.separator+"test.properties";
        //数据流读取文件
        BufferedReader bufReader= new BufferedReader(new FileReader(filePath));
        StringBuilder stringBuffer = new StringBuilder();
        while (bufReader.ready()){
            String line = bufReader.readLine().replace("name=zhangsan", "name=lisi").replace("age=20","age=18");
            stringBuffer.append(line);
            stringBuffer.append("\n");
        }
        bufReader.close();
        BufferedWriter bufWriter = new BufferedWriter(new FileWriter(filePath));
        bufWriter.write(stringBuffer.toString());
        bufWriter.flush();
        bufWriter.close();
    }
}

test.properties文件内容,如下:

name=zhangsan
age=20
sex=男

读取文件内容转换成字符串或输出流

package com.xiaofang520.codes.demo;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * 实现文件读行并替换
 *
 * @author Gao
 * @date 2022/6/22 15:19
 */
public class FileReaderOrWriterTestInputStream {

    public static void main(String[] args) throws IOException, InterruptedException {
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("market/test.properties");
        // 读取文件
        BufferedReader bufReader = new BufferedReader(new InputStreamReader(inputStream));
        // 储存文件内容
        StringBuilder stringBuffer = new StringBuilder();
        // 字符缓冲流准备好了,开始读数据
        while (bufReader.ready()){
            // 读取一行数据替换里面(满足条件)的内容
            String line = bufReader.readLine()/*.replace("name=zhangsan", "name=lisi").replace("age=18","age=19")*/;
            // 开始储存
            stringBuffer.append(line);
            // 追加换行
            stringBuffer.append("\n");
        }
        bufReader.close();
        System.out.println(stringBuffer.toString());
        byte[] bytes = stringBuffer.toString().getBytes();
        InputStream returnInputStream = new ByteArrayInputStream(bytes);
        // 一次性读完数据,并转换城字符串
        // int n = returnInputStream.available();
        // byte[] databytes = new byte[n];
        // int read = returnInputStream.read(databytes, 0, n);
        // System.out.println(read+"<<======读取标记");
        // System.out.println(new String(databytes));
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        // 使用read的返回作为读取的标记,不可读了为-1
        // int charInt;
        // while ((charInt = returnInputStream.read())!=-1){
        //使用可读的总数量作为标记,不可读了为0
        while (returnInputStream.available()!=0){
            // outputStream.write(charInt);
            outputStream.write(returnInputStream.read());
        }
        outputStream.flush();
        outputStream.close();
        System.out.println(outputStream.toString());


    }
}

标题:文件字符流读取随笔
作者:JayGao
地址:https://www.xiaofang520.com/articles/2022/06/22/1655889148956.html
版权:版权JayGao所有,如需转载,请注明出处
捐赠: