Java file read & write
2015-11-11 06:34
591 查看
1. read
2. write
public static void readfile(String filepath) { BufferedReader br = null; try { String sCurrentLine; br = new BufferedReader(new FileReader(filepath)); while ((sCurrentLine = br.readLine()) != null) { System.out.println(sCurrentLine); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
2. write
public static void writefile(String filepath) { try { String content = "This is the content to write into file asndfsa"; File file = new File(filepath); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } }
相关文章推荐
- Windows平台Java开发环境的搭建
- '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp error
- Struts2中Action接收参数的方法
- struts2读常量顺序
- spring数据源配置
- Spring注解详解
- Java IO代码详解
- how to create java test folder in intellij idea
- JAVA事务的特性
- MyEclipse10.0安装 SVN插件
- spring mvc +spring jdbc配置
- Java平台3个版本:
- Mybatis、JDBC、Habernate、Mybatis+Spring的Mysql数据库操作实例
- 源码分析:Java堆的创建
- DownLoad文件
- java.lang.NoSuchMethodError: javax.persistence.One
- Java基本数据类型与四个运算
- Java2平台版本
- JAVA成长之路
- Spring用gradle构建restful websrevice