给定InputStream替换字符并产生OutputStream [英] Given InputStream replace character and produce OutputStream

查看:86
本文介绍了给定InputStream替换字符并产生OutputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多海量文件,需要通过替换某些字符将其转换为CSV.

I have a lot of massive files I need convert to CSV by replacing certain characters.

我正在寻找给定InputStream返回OutputStream并将所有字符c1替换为c2的可靠方法.

I am looking for reliable approach given InputStream return OutputStream and replace all characters c1 to c2.

这里的技巧是并行读写,我无法将整个文件容纳在内存中.

Trick here is to read and write in parallel, I can't fit whole file in memory.

如果我想同时读写,是否需要在单独的线程中运行它?

Do I need to run it in separate thread if I want read and write at the same time?

非常感谢您的建议.

推荐答案

要将数据从输入流复制到输出流,请在每次读取一个字节(或字符)或一行的同时写入数据

To copy data from an input stream to an output stream you write data while you're reading it either a byte (or character) or a line at a time.

这里是一个读取文件的示例,该文件将所有"x"字符转换为"y".

Here is an example that reads in a file converting all 'x' characters to 'y'.

BufferedInputStream in = new BufferedInputStream(new FileInputStream("input.dat"));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("output.dat"));
int ch;
while((ch = in.read()) != -1) {
        if (ch == 'x') ch = 'y';
        out.write(ch);
}
out.close();
in.close();

或者如果可以一次使用Reader并处理一行,则可以使用此方法:

Or if can use a Reader and process a line at a time then can use this aproach:

BufferedReader reader = new BufferedReader(new FileReader("input.dat"));
PrintWriter writer = new PrintWriter(
      new BufferedOutputStream(new FileOutputStream("output.dat")));
String str;
while ((str = reader.readLine()) != null) {
    str = str.replace('x', 'y');     // replace character at a time
    str = str.replace("abc", "ABC"); // replace string sequence
    writer.println(str);
}
writer.close();
reader.close();

预先读取

BufferedInputStream BufferedReader 并在缓冲区中保留8K字符以提高性能.可以处理非常大的文件,而一次只在内存中保留8K个字符.

BufferedInputStream and BufferedReader read ahead and keep 8K of characters in a buffer for performance. Very large files can be processed while only keeping 8K of characters in memory at a time.

这篇关于给定InputStream替换字符并产生OutputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆