我们以一个实际例子来说明java对txt文件的读、写操作。
计算出差补助费用,补助规则如下:
在30天以内每天补助50元,
在30天以上60天以内,前30天每天补助50,多余30天的每天多补助10元,
在60天以上90天以内,前30天每天补助50,31到60每天多补助10元,多余60天的每天再多补助10元,
其余的以此类推.....
举例:出差28天,补助费用为:28*50;
出差45天,补助费用为:30*50 + 15*60
出差67天,补助费用为:30*50 + 30*60 +7*70
要求:出于灵活性考虑,要求每天补助费用(如50)、达到条件后每天多补助费用(如10)从D:\test\config.properties文件中读取,config.properties内容如下:
base=50
step=10
从D:\test\src.txt文件中读取出差人员的信息,人员信息包括:出差人姓名 出差日期 出差结束日期。其中每个字段以空格区分,例如:
张三 2010-9-17 2010-10-15
李四 2010-9-5 2010-10-30
王五 2010-9-20 2010-11-2
根据以上信息计算出每人的出差天数,应付补助费用,将结果写入D:\test\result.txt中,result.txt的内容包括出差人姓名 出差日期 出差结束日期 出差天数 应付补助,例如:
张三 2010-9-17 2010-10-15 28 1400
李四 2010-9-5 2010-10-30 55 3000
王五 2010-9-20 2010-11-2 43 2280
下面附上操作源码
memberInfo类
package com.dengsilinming.read.file;
/**
* TODO Comment of MemberInfo
*
* @author dengsilinming
* @version $Id: MemberInfo.java 2013-1-31 上午10:30:40 $
*
*/
public class MemberInfo {
// 姓名
private String memberName;
// 出差开始日期
private String startDate;
// 出差结束日期
private String endDate;
//出差天数
private long countDay;
// 应付出差费
private long totalMoney;
public String getMemberName() {
return memberName;
}
public void setMemberName(String memberName) {
this.memberName = memberName;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public long getCountDay() {
return countDay;
}
public void setCountDay(long countDay) {
this.countDay = countDay;
}
public long getTotalMoney() {
return totalMoney;
}
public void setTotalMoney(long totalMoney) {
this.totalMoney = totalMoney;
}
}
operateTxt类
package com.dengsilinming.read.file;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
/**
* TODO Comment of OperateTxt
*
* @author dengsilinming
* @version $Id: OperateTxt.java 2013-1-31 上午10:40:40 $
*
*/
public class OperateTxt {
private static final String BASE_URL = "D:/test/config.properties";
private static final String INPUT_URL = "D:/test/src.txt";
private static final String OUTPUT_URL = "D:/test/result.txt";
/**
* 读取config.properties文件
* @param path 文件路径
* @return
*/
private static String readBaseInfo(String path) {
try {
InputStream is = new BufferedInputStream(new FileInputStream(new File(path)));
Properties properties = new Properties();
properties.load(is);
String base = properties.getProperty("base");
String step = properties.getProperty("step");
return base + "," + step;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
/**
* 解析src.txt文件,并计算每人的出差费用
* @param base
* @param stap
* @param path
* @return memberInfoList
*/
private static List<MemberInfo> parseSrcTxt(long base, long stap, String path) {
try {
List<MemberInfo> memberInfoList = new ArrayList<MemberInfo>();
String line;
File srcFile = new File(path);
FileReader fileReader = new FileReader(srcFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
String[] memberArr = line.split(" ");
MemberInfo member = new MemberInfo();
member.setMemberName(memberArr[0]);
member.setStartDate(memberArr[1]);
member.setEndDate(memberArr[2]);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = df.parse(memberArr[1]);
Date endDate = df.parse(memberArr[2]);
long day = (endDate.getTime() - startDate.getTime())/(60*60*24*1000);
member.setTotalMoney(calculateFee(base, stap, day));
member.setCountDay(day);
memberInfoList.add(member);
}
fileReader.close();
bufferedReader.close();
return memberInfoList;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 计算费用
* @param base 每天的费用
* @param stap 提升等级数
* @param day 出差天数
* @return
*/
private static long calculateFee(long base, long stap, long day) {
int time = (int) (day/30);//循环次数
long count = 0;//应付总费用
for (int i = 0; i <= time; i++) {
if (i == 0) {
if (day <= 30)
count = base * day;
else
count = base * 30;
} else {
if (day - 30 * i <= 30) {
count = count + (base + stap * i) * (day-30*i);
} else
count = count + (base + stap * i) * 30;
}
}
return count;
}
/**
* 向result.txt文件中写内容
* @param memberList
* @param outPutURL
*/
private static void writeToFile(List<MemberInfo> memberList, String outPutURL) {
try {
// 向txt文件中写内容
FileWriter fileWriter = new FileWriter(new File(outPutURL));
for (MemberInfo member : memberList) {
String writeText = member.getMemberName() + " " + member.getStartDate() +
" " + member.getEndDate() + " " + member.getCountDay() + " " +
member.getTotalMoney() + "\r\n";
fileWriter.write(writeText);
}
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String[] baseAndStepArr = readBaseInfo(BASE_URL).split(",");// 读取配置文件内容
List<MemberInfo> memberList = parseSrcTxt(Long.parseLong(baseAndStepArr[0]),
Long.parseLong(baseAndStepArr[1]), INPUT_URL);// 解析txt文件内容
if (memberList != null && memberList.size() > 0) {
writeToFile(memberList, OUTPUT_URL);// 新建txt文件并向其写内容
}
}
}
config.properties
src.txt
result.txt
注:java读取配置文件properties的方法可以参见 java读取properties的方法