package txt;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
/**
* 功能描写叙述:创建TXT文件并进行读、写、改动操作
* @author lizhiyong
* @version $Id: ReadWriteFile.java, v 0.1
2014年8月5日 下午1:27:38 Exp $
*/
public class ReadWriteFile {
//指定文件路径和名称
private static String path = "C:/測试.txt";
private static File filename = new File(path);
private static String readStr = " "; /**
* 创建文本文件.
* @throws IOException
*
*/
public static void creatTxtFile() throws IOException {
if (!filename.exists()) {
filename.createNewFile();
System.err.println(filename + "已创建! ");
} else {
filename.delete();
creatTxtFile();
}
} /**
* 读取文本文件.
* @throws UnsupportedEncodingException
*
*/
@SuppressWarnings("resource")
public static String readTxtFile() throws UnsupportedEncodingException {
String readData = null;
//BufferedReader br = null;
BufferedReader br = null;
try {
//br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
br = new BufferedReader(new FileReader(filename));
try {
while ((readData = br.readLine()) != null) {
System.out.println("readData:" + readData);
readStr = readStr + readData + "\r\n";
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} System.out.println("文件内容2是:" + "\r\n" + readStr);
return readStr;
} /**
* 给文件写内容.
* @param content 写入的文件内容
* @throws IOException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void writeTxtFile(List contentList, HashMap<String, String> map)
throws IOException {
//先读取原有文件内容。然后进行写入操作
FileWriter writer = null;
String filein = map.get("1") + readStr + map.get("2") + readStr + map.get("3") + readStr
+ map.get("4");
try {
writer = new FileWriter(filename, true);
writer.write(filein);
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
} for (Iterator iterator = contentList.iterator(); iterator.hasNext();) {
HashMap<String, String> map2 = (HashMap<String, String>) iterator.next();
String name = map2.get("name");
String age = map2.get("age");
String postion = map2.get("postion");
String complit = map2.get("complit");
String filein1 = "\r\n" + name + readStr + age + readStr + postion + readStr + complit
+ "\r\n";
try {
writer = new FileWriter(filename, true);
writer.write(filein1);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
} } readTxtFile();
} /**
* 将文件里指定内容的第一行替换为其他内容.
*
* @param oldStr
* 查找内容
* @param replaceStr
* 替换内容
*/
@SuppressWarnings("unused")
public static void replaceTxtByStr(String oldStr, String replaceStr) {
String temp = "";
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer(); // 保存该行前面的内容
for (int j = 1; (temp = br.readLine()) != null && !temp.equals(oldStr); j++) {
buf = buf.append(temp);
buf = buf.append(System.getProperty("line.separator"));
} // 将内容插入
buf = buf.append(replaceStr); // 保存该行后面的内容
while ((temp = br.readLine()) != null) {
buf = buf.append(System.getProperty("line.separator"));
buf = buf.append(temp);
} br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* main方法測试
* @param s
* @throws IOException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] s) throws IOException {
ReadWriteFile.creatTxtFile();
//ReadWriteFile.readTxtFile();
List list = new ArrayList();
HashMap<String, String> map = new HashMap<String, String>();
map.put("1", "姓名");
map.put("2", "年龄");
map.put("3", "职位");
map.put("4", "工作单位"); HashMap<String, String> map2 = new HashMap<String, String>();
map2.put("name", "李四");
map2.put("age", "25");
map2.put("postion", "Java开发project师");
map2.put("complit", "上海汽车財务集团有限公司");
list.add(map2); HashMap<String, String> map3 = new HashMap<String, String>();
map3.put("name", "李四1");
map3.put("age", "251");
map3.put("postion", "Java开发project师1");
map3.put("complit", "上海汽车財务集团有限公司1");
list.add(map3); ReadWriteFile.writeTxtFile(list, map);
// ReadWriteFile.replaceTxtByStr("ken", "zhang");
}
}

最新文章

  1. Android中实现APP文本内容的分享发送与接收方法简述
  2. reason: &#39;[&lt;__NSDictionary0 0x7fda88f00c90&gt; setValue:forUndefinedKey:]: this class is not key value c
  3. Shell入门教程:Shell函数的返回值
  4. WPF RoadMap
  5. MySQL 数据库发展历程及缺点
  6. 转:C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文
  7. **IOS:xib文件解析(xib和storyboard的比较,一个轻量级一个重量级)
  8. 硬盘结构介绍--mbr及分区
  9. net.sf.json.JSONException: Object is null
  10. 局部变量存储区域静态变量存储区域static变量存储区域
  11. ThinkPHP 3.1.2 查询方式的一般使用1
  12. win10无法新建文件夹怎么办 win10右键新建菜单设置方法
  13. 5. SQL Server数据库性能监控 - 当前请求
  14. junit 单元测试 - 参数化测试
  15. Disassembly2:Built-in Type
  16. 开源中文分词工具探析(四):THULAC
  17. Django(一)
  18. Linux发行版 CentOS6.5 修改默认主机名
  19. Mockito框架入门教程(二)
  20. Python的内置方法

热门文章

  1. Codeforces Round #439 (Div. 2) A. The Artful Expedient
  2. cf 1029 C
  3. Luogu3195 [HNOI2008]玩具装箱TOY (方程变形 + 斜率优化 )
  4. JQuery中xxx is not a function或者can not find $
  5. lnmp环境运行laravel open_basedir restriction in effect 问题
  6. i++和++i的区别,及其线程安全问题
  7. 【LeetCode】String Without AAA or BBB(不含 AAA 或 BBB 的字符串)
  8. xtu数据结构 C. Ultra-QuickSort
  9. scp命令(基于ssh上传文件等)
  10. Codeforces #765D