//尝试用printwriter 的println方法写 还是不行import java.io.*;public class Novel {public static void main(String[] args) {try {FileReader fr = new FileReader("F:\\ITcast_IODemo\\Novel.txt");BufferedReader br = new BufferedReader(fr);String aLine = br.readLine();// 附加1:tmp是一个缓存对象,用来缓存即将写入最新一章的内容StringBuffer tmp = new StringBuffer("");// 附加2:fw是用来写入每一章txt的文件输出流对象FileWriter fw = null;while (aLine != null) {if (aLine.contains("第") && aLine.contains("章")) {// 是某一章的开头fw = new FileWriter("F:\\ITcast_IODemo\\"+aLine + ".txt");// tmp初始化为空,tmp = new StringBuffer("");}// 将读入的aline 也就是当前这一行,放入tmp中。等待时机再存入txttmp.append(aLine + "\n");aLine = br.readLine();if ((aLine == null) || (aLine.contains("第") && aLine.contains("章"))) {// 如果下一行是 null(文件结束)或者下一章开始// 要将tmp内容写到fw中去,fw关闭,fw.write(tmp.toString());System.out.println("------写入文件内容-------");System.out.println(tmp.toString());fw.close();//}}} catch (FileNotFoundException e) {System.out.println("文件找不到" + e.toString());} catch (IOException e) {System.out.println("文件IO异常" + e.toString());}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。