public static void main(String[] args) { String filePath = "D:\\sql.txt"; // "res/"; String sql =readTxtFile(filePath); System.out.println(sql ); }
public static String readTxtFile(String filePath){ String sql=""; try { String encoding="GBK"; File file=new File(filePath); if(file.isFile() && file.exists()){ //判断文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file),encoding);//考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while((lineTxt = bufferedReader.readLine()) != null){ System.out.println(lineTxt); sql=sql+lineTxt; } read.close(); }else{ System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } return sql; }
本文转自tianjian_0913 51CTO博客,原文链接:http://blog.51cto.com/tianjian/1896095,如需转载请自行联系原作者