日期:2014-05-20 浏览次数:20811 次
import java.io.*; public class FileToString { public static String readFile(String fileName) { String output = ""; File file = new File(fileName); if(file.exists()){ if(file.isFile()){ try{ BufferedReader input = new BufferedReader (new FileReader(file)); StringBuffer buffer = new StringBuffer(); String text = input.readLine(); while(text != null){ buffer.append(text +"\n"); text = input.readLine(); } output = buffer.toString(); } catch(IOException ioException){ System.err.println("File Error!"); } } else if(file.isDirectory()){ String[] dir = file.list(); output += "Directory contents:\n"; for(int i=0; i<dir.length; i++){ output += dir[i] +"\n"; } } } else{ System.err.println("Does not exist!"); } return output; } public static void main (String args[]){ String str = readFile("C:/1.txt"); //System.out.print(str); String[] strSplit = str.split("。"); for (String element : strSplit){ System.out.println(element.trim()); System.out.println(); } } }
------解决方案--------------------
我这样处理不知是否能满足楼的要求:
import java.io.*; public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub StringBuffer text = new StringBuffer(); try { BufferedReader reader = new BufferedReader(new FileReader("D:\\test.txt")); String str; while ((str = reader.readLine()) != null) { str = str.replaceAll("。", "。\n\n"); text.append(str); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(text); } }
------解决方案--------------------
package com.qq.server; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Test4 { public static void main(String[] args) { try { FileReader fr = new FileReader("e:\\test.txt"); BufferedReader br = new BufferedReader(fr); String str = null; String outputStr = ""; String[] outputArray = null; while ((str = br.readLine()) != null) { outputStr += str.trim(); } outputArray = outputStr.split("(?<=[。!?])");// 用 。 ! ?其中一个分隔 for (int i = 0; i < outputArray.length; i++) { System.out.println(outputArray[i] + "\n"); } br.close(); fr.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }