txt文档转化为String

时间:2020-09-20 13:19:43
【文件属性】:

文件名称:txt文档转化为String

文件大小:68KB

文件格式:PDF

更新时间:2020-09-20 13:19:43

字符转化

将.txt文档转化为String类型 具体代码如下: package IndexSearch; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; /** * 将txt的路径,将txt文档转换为String内容 * @author jean * */ public class TextFileExchange { //将txt转换为String public static String readTxt(String path) throws IOException{ StringBuffer content = new StringBuffer(""); //文档内容 try { FileReader reader = new FileReader(path); BufferedReader br = new BufferedReader(reader); String s1 = null; while((s1 = br.readLine()) != null) { content.append(s1+"\r"); } br.close(); reader.close(); }catch(IOException e) { e.printStackTrace(); } return content.toString().trim(); } public static void main(String[] args) { System.out.println(IndexPath.Index_File_Path); String path = IndexPath.Index_File_Path; try { String temp = readTxt(path); System.out.println(temp); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }


网友评论