标签:
文件的读取: public static void duqu(String path) throws IOException { File file=new File(path); FileInputStream fis=new FileInputStream(file); InputStreamReader isr=new InputStreamReader(fis,"gb2312"); BufferedReader br=new BufferedReader(isr); StringBuffer buf=new StringBuffer(); String str=null; while((str=br.readLine())!=null){ buf.append(str); buf.append("\r\n"); } System.out.println(buf.toString()); br.close(); } 文件的写入: public static void write(String path, String content) { String s = new String(); String s1 = new String(); try { File f = new File(path); if (f.exists()) { System.out.println("文件存在"); } else { System.out.println("文件不存在,正在创建..."); if (f.createNewFile()) { System.out.println("文件创建成功!"); } else { System.out.println("文件创建失败!"); } } BufferedReader input = new BufferedReader(new FileReader(f)); while ((s = input.readLine()) != null) { s1 += s + "/n"; } input.close(); s1 += content; BufferedWriter output = new BufferedWriter(new FileWriter(f)); System.out.println("文件内容:" + s1); output.write(s1); output.close(); } catch (Exception e) { e.printStackTrace(); } }
标签:
原文地址:http://www.cnblogs.com/wwxblog/p/4277519.html