1、一次性读取整个文件内容
/** * 一次性读取全部文件数据 * @param strFile */ public static void readFile(String strFile){ try{ InputStream is = new FileInputStream(strFile); int iAvail = is.available(); byte[] bytes = new byte[iAvail]; is.read(bytes); logger.info("文件内容:\n" + new String(bytes)); is.close(); }catch(Exception e){ e.printStackTrace(); } }
2、按行读取文件
/** * 按行读取文件 * @param strFile */ public static void readFileByLine(String strFile){ try { File file = new File(strFile); BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); String strLine = null; int lineCount = 1; while(null != (strLine = bufferedReader.readLine())){ logger.info("第[" + lineCount + "]行数据:[" + strLine + "]"); lineCount++; } }catch(Exception e){ e.printStackTrace(); } }