Java之使用IO流输入输出
程序员文章站
2024-03-17 11:03:22
...
题目
从文本文档输入沁园春,成一行排列。输出效果成古文形式排列,且输出到新的txt文档
代码
public class Test {
public static void main(String[] args) throws IOException {
FileFormat();
}
public static void FileFormat() throws IOException{
String str = null;
Test t = new Test();
//字符流
Reader r = new FileReader("C:\\沁园春.txt");
Writer w = new FileWriter("C:\\1.txt");
//缓冲流
BufferedReader br = new BufferedReader(r);
BufferedWriter bw = new BufferedWriter(w);
try {
while((str = br.readLine()) != null){
System.out.println(str);
System.out.println("读取完成");
String[] s = t.init(str);
for(int i = 0; i < s.length; i++){
bw.write(s[i]);
bw.write("。");
bw.newLine();
}
System.out.println("写入完成");
}
bw.close();
br.close();
w.close();
r.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String[] init(String str){
String[] s = new String[str.length()];
s = str.split("。");
return s;
}
}