java指定路径写、读文件
2016-05-20 22:24
399 查看
package com.util; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /** * @ClassName: IOUtill * @Description: I/O工具类 * @author 无名 * @date 2016-5-20 下午9:00:18 * @version 1.0 */ public final class IOUtill { private IOUtill(){} public static void writeByUrl(String url,String content) { File file = new File(url); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } try { FileWriter fw = new FileWriter(file, true); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.flush(); bw.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } public static String readByUrl(String url) { File file = new File(url); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } String content = ""; try { FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); try { String line = br.readLine(); content += line; while(line != null) { line = br.readLine(); content += line; } }catch (IOException ex) { ex.printStackTrace(); } }catch(FileNotFoundException e){ e.printStackTrace(); } return content; } }
相关文章推荐
- java语言关系运算符
- 一个最简单的SPRINGMVC示例
- JAVA 期末设计(一)
- JAVA简单读取xls表格
- Java线程安全详细总结
- LRU缓存实现(Java)
- Java基础学习总结——Java对象的序列化和反序列化
- java中的native方法
- Closeable和java7自动资源释放特性
- JAVA——System类
- [Java]javac 编译源文件出现“锘缝ublic”、“非法字符: \65279”的乱码情况
- eclipse的应用技巧(一)
- Java环境搭建,以win10为例
- java并发-ReentrantLock的lock和lockInterruptibly的区别
- Java内存与垃圾收集知识总结
- Java 设计模式 -- 职责链模式
- 为什么要设置Java环境变量(详解)
- Struts逐步学习
- Java基础——数组(一维数组,二维数组)
- Eclipse导出JAR包【java】