生成和解析txt文件
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");
}
}
生成和解析txt文件的更多相关文章
- dom4j生成和解析xml文件
dom4j生成和解析xml文件 要生成和解析如下格式的xml文件: <?xml version="1.0" encoding="UTF-8"?> & ...
- python的OS模块生成100个txt文件
#!/user/bin/env/python35 # -*-coding:utf-8-*- # author:Keekuun """ 问题:生成一个文件夹,文件夹下面生成 ...
- php 批量生成html、txt文件
首先建立一个conn.php的文件用来链接数据库 <?php $link = mysql_connect("mysql_host" , "mysql_use ...
- C++生成和解析XML文件
1.xml 指可扩展标记语言(EXtensible Markup Language) 2.xml 是一种标记语言,类似html 3.xml 的设计宗旨是传输数据,而非显示数据 4.xml 标签没有被预 ...
- [java] java解析txt文件
/** * 读取txt文件内容封装为map返回 * @param filePath * @return */ public static String readTxt(String filePath) ...
- 【Java】使用Apache POI生成和解析Excel文件
概述 Excel是我们平时工作中比较常用的用于存储二维表数据的,JAVA也可以直接对Excel进行操作,分别有jxl和poi,2种方式. HSSF is the POI Project's pure ...
- C#生成、解析xml文件以及处理报错原因
转载自:http://blog.csdn.net/lilinoscar/article/details/21027319 简单的介绍一下生成XML文件以及解析,因为有些数据不一定放到数据库,减少链接数 ...
- 解析oui.txt文件,通过MAC前缀获取Organization
1.前言 OUI是指Organizationally unique identifier (组织唯一标识符),签发给各类组织的唯一标识符.MAC地址共有6个字节48位组成,前3个字节体现了OUI,其 ...
- js生成txt文件
HTML CODE: <div class="modal-footer"> <a onfocus="this.blur();" id=&quo ...
随机推荐
- Verilog学习笔记基本语法篇(八)········ 结构说明语句
Verilog中的任何过程都可以属于以下四种结构的说明语句; 1) initial; 2) always; 3) task; 4) function; 1) initial说明语句: 一个程序 ...
- 图论:POJ2186-Popular Cows (求强连通分量)
Popular Cows Description Every cow's dream is to become the most popular cow in the herd. In a herd ...
- VIJOS1476 旅行规划(树形Dp + DFS暴力乱搞)
题意: 给出一个树,树上每一条边的边权为 1,求树上所有最长链的点集并. 细节: 可能存在多条最长链!最长链!最长链!重要的事情说三遍 分析: 方法round 1:暴力乱搞Q A Q,边权为正-> ...
- git commit 含有中文的代码,提示Warnning:Your console font probably doesn't support Unicode.......
git 提交代码是会遇到以下问题, git commit 代码时提示: Warning: Your console font probably doesn't support Unicode. If ...
- angular controller与directive相互引用
/** * Created by Administrator on 2017/8/28. */ var app =angular.module('app',[]); app.directive('fo ...
- SAXException Parser configuration problem: namespace reporting is not enabled
问题描述: 用sax的方式组装xml,在tomcat上正常运行,在weblogic12c上报错: SAXException Parser configuration problem: namespac ...
- Web开发细节搜集
App_Data 百度百科: App_Data文件夹应该包含应用程序的本地数据存储.它通常以文件(诸如Microsoft Access或Microsoft SQL Server Express数据库 ...
- 【Luogu】P1879玉米田(状压DP)
题目链接 数据范围这么小,难度又这么大,一般就是状态压缩DP了. 对输入进行处理,二进制表示每一行的草地状况.如111表示这一行草地肥沃,压缩成7. 所以f[i][j]表示第i行状态为j时的方案数 状 ...
- [BZOJ1419] Red is good(期望DP)
传送门 逆推 只不过顺序还是顺着的,思想是逆着的 f[i][j]表示还剩下i张红牌,j张黑牌的期望值 那么边界是 f[i][0]=i,因为只剩i张红牌 f[0][j]=0,只剩黑牌,显然直接停止最优 ...
- [TJOI2009]开关 (线段树)
题目描述 现有N(2 ≤ N ≤ 100000)盏灯排成一排,从左到右依次编号为:1,2,......,N.然后依次执行M(1 ≤ M ≤ 100000)项操作,操作分为两种:第一种操作指定一个区间[ ...