文件创建 file txt java
2016-04-19 16:57
585 查看
创建文件夹
fPath="F:"+File.separator+"tomcat6work"+File.separator+"nihao1";
public static void getFileExists(String fPath) {
File file = new File(fPath);
// 如果文件夹不存在则创建
if (!file.exists() && !file.isDirectory()) {
file.mkdir();
}
}
//文件写入txt
public static void createTxt(String filePath, String content, boolean b) {
String str = new String(); // 原有txt内容
String s1 = new String();// 内容更新
try {
File f = new File(filePath);
if (!f.exists()) {
f.createNewFile();// 不存在则创建
}
if (false == b) {
BufferedReader input = new BufferedReader(new FileReader(f));
while ((str = input.readLine()) != null) {
s1 += str + "\n";
}
System.out.println(s1);
input.close();
}
s1 += content;
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
fPath="F:"+File.separator+"tomcat6work"+File.separator+"nihao1";
public static void getFileExists(String fPath) {
File file = new File(fPath);
// 如果文件夹不存在则创建
if (!file.exists() && !file.isDirectory()) {
file.mkdir();
}
}
//文件写入txt
public static void createTxt(String filePath, String content, boolean b) {
String str = new String(); // 原有txt内容
String s1 = new String();// 内容更新
try {
File f = new File(filePath);
if (!f.exists()) {
f.createNewFile();// 不存在则创建
}
if (false == b) {
BufferedReader input = new BufferedReader(new FileReader(f));
while ((str = input.readLine()) != null) {
s1 += str + "\n";
}
System.out.println(s1);
input.close();
}
s1 += content;
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树