java怎么读取文件中内容,并每隔5个字符换行,并写入新的文本 200
2个回答
展开全部
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class App17 {
public static void main(String[] args) throws IOException {
File srcFile = new File("d:\\test.txt");
File outFile = new File("d:\\dest.txt");
InputStreamReader reader = new InputStreamReader(new FileInputStream(srcFile));
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFile));
char[] buffer = new char[5];
while(reader.read(buffer) != -1) {
System.out.println(buffer);
writer.write(String.valueOf(buffer).trim() + "\r\n");
}
reader.close();
writer.flush();
writer.close();
}
}
更多追问追答
追问
如果读取字符串有空格怎么去掉
怎么把空格去掉
展开全部
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class T1 {
public static void main(String argv[]) {
File f0 = new File ("d:/f0.txt");
if (!f0.exists())
return;
File f1 = new File ("d:/f1.txt");
try {
FileReader fr = new FileReader(f0);
FileWriter fw = new FileWriter(f1);
int ch;
int size = 0;
while ((ch = fr.read())!=-1) {
fw.write(ch);
if (++size%5==0)
fw.write('\n');
}
fr.close();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询