java指定路径写、读文件
2024-10-12 03:52:23
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;
}
}
最新文章
- ABP源码分析三十三:ABP.Web
- C#设计模式——职责链模式(Chain Of Responsibility Pattern)
- 匹配一级分类和二级分类 名字和url 里面有玄机
- Jenkins: 使用groovy + job-dsl 创建并触发job
- JavaScript实现一个复数类
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- 一般处理程序(ashx)的使用
- 嵌入式linux——汇编、C语言基础(一)
- 编写shell脚本kill掉占用cpu超过90%以上的程序
- 关于Django部分
- 解决highCharts导出功能汉化问题
- Homestead 修改 Homestead.yaml 文件后 vagrant up 报错的问题
- Mysql密码忘记了怎么办?
- PAT甲题题解-1055. The World's Richest (25)-终于遇见一个排序的不水题
- excel如何快速更改表格边框的颜色
- centOS7 apache ssl证书安装配置
- mysql 8小时问题
- React简单实现双向数据绑定
- Tomcat设置是否可以上传文件到服务器
- Python类对象