标签:
/** * 将note.txt文件中每一行字符串读取出来并输出到控制台 * */ public class FileToConlse { public static void main(String[] args) { /*建立测试文件夹及文件对象*/ File dir = new File("."+File.separator+"src"+File.separator+"practise"+File.separator+"io"); if(!dir.exists()){ dir.mkdir(); } File file = new File(dir,"note.txt"); /*建立BufferedReader字符输入流读取文件*/ BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(new FileInputStream(file))); } catch (FileNotFoundException e) { System.out.println("文件未找到"); e.printStackTrace(); } /*循环readLine 直到读到文件末尾*/ PrintWriter out = new PrintWriter(System.out); //输出至其他文件请改参数 String line; try { while ( (line = in.readLine())!= null){ out.println(line); } } catch (IOException e1) { // 未知IO异常 e1.printStackTrace(); } finally{ /*关闭资源*/ try { if(in != null) in.close(); if(out != null) out.close(); } catch (IOException e) { e.printStackTrace(); } } } }
标签:
原文地址:http://www.cnblogs.com/zyjcxc/p/5467166.html