以下包括 讀檔、寫檔的範例。
public class PAV3 {
public static final String Left = "L";
public static final String Right = "R";
private String BLD_DNLOAD = "/Users/macbook/java/test/FTP_DIR/BLD_DNLOAD";
private String PAV3 = "";
private String LST3_FILE = "";
private String BV1_FILE = "";
private String BV2_FILE = "";
private String BV3_FILE = "";
public void run(String path) {
this.BLD_DNLOAD = path;
}
//先讀取舊的 PAV3.DAT
public Map ReadPAV3() {
ArrayList<String> newList = new ArrayList<String>();
Map newMap = new HashMap(2);
PAV3 = BLD_DNLOAD + "//PAV3.DAT";
FileReader f;
BufferedReader br;
try {
f = new FileReader(PAV3);
br = new BufferedReader(f);
String strLine;
while ((strLine = br.readLine()) != null) {
if(strLine.startsWith("H")){
newMap.put("header", strLine);
}else if(strLine.startsWith("D")){
if(!this.isExistHIJK(strLine)){
//System.out.println("["+strLine+"]");
newList.add(strLine);
}
}else{
}
}
newMap.put("detail", newList);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newMap;
}
public void run() {
//先讀取舊的 PAV3.DAT
Map oldMap = ReadPAV3();
File f = new File(BLD_DNLOAD);
File[] files = f.listFiles();
TreeMap tm = new TreeMap();
ArrayList<String> al_LST3 = new ArrayList<String>();
ArrayList<String> al_BV1 = new ArrayList<String>();
ArrayList<String> al_BV2 = new ArrayList<String>();
ArrayList<String> al_BV3 = new ArrayList<String>();
ArrayList<String> al_hijk = new ArrayList<String>();
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory()
&& !files[i].getName().startsWith(".DS_Store")) {
String filename = files[i].getName();
String sysDate = this.retriSysDate();
String fileDate = this.retriveFileDate(filename);
// 檔案日期 大於或等於 系統日
if (this.compare(fileDate, sysDate)) {
if (this.whichHIJK(filename).equals("i")) {
al_LST3.add(filename);
} else if (this.whichHIJK(filename).equals("j")) {
al_BV1.add(filename);
} else if (this.whichHIJK(filename).equals("k")) {
al_BV2.add(filename);
} else if (this.whichHIJK(filename).equals("h")) {
al_BV3.add(filename);
}
} else {
if (this.whichHIJK(filename).equals("i")) {
if (this.compare(fileDate, LST3_FILE)) {
LST3_FILE = filename;
}
} else if (this.whichHIJK(filename).equals("j")) {
if (this.compare(fileDate, LST3_FILE)) {
BV1_FILE = filename;
}
} else if (this.whichHIJK(filename).equals("k")) {
if (this.compare(fileDate, LST3_FILE)) {
BV2_FILE = filename;
}
} else if (this.whichHIJK(filename).equals("h")) {
if (this.compare(fileDate, LST3_FILE)) {
BV3_FILE = filename;
}
}
}
}
}
if (!LST3_FILE.equals("")) al_LST3.add(LST3_FILE);
if (!BV1_FILE.equals("")) al_BV1.add(BV1_FILE);
if (!BV2_FILE.equals("")) al_BV2.add(BV2_FILE);
if (!BV3_FILE.equals("")) al_BV3.add(BV3_FILE);
al_hijk.addAll(al_LST3);
al_hijk.addAll(al_BV1);
al_hijk.addAll(al_BV2);
al_hijk.addAll(al_BV3);
tm.put("HIJK", al_hijk);
// tm.put("BV1", al_BV1);
// tm.put("BV2", al_BV2);
// tm.put("BV3", al_BV3);
String b = this.buildPav3(tm,oldMap);
System.out.println(b);
}
//產生 PAV3.DAT
public String buildPav3(TreeMap tm,Map oldmap) {
StringBuilder b = new StringBuilder();
// String company = "0001";
// header
//b = this.header(b, company);
b.append(oldmap.get("header")).append("\n");
// detail
int s = 0;
ArrayList l0 = (ArrayList)oldmap.get("detail");
for (java.util.Iterator<String> it = l0.iterator(); it.hasNext();) {
String olddetail = it.next();
b.append(olddetail).append("\n");
s++;
}
List l1 = (ArrayList) tm.get("HIJK");
for (java.util.Iterator<String> it = l1.iterator(); it.hasNext();) {
String filename = it.next();
b = this.detail(b, filename);
s++;
}
// trailer
b = this.trailer(b, s);
//update PAV3
this.writeToPAV3(b.toString());
return b.toString();
}
public void writeToPAV3(String p){
File saveFile=new File(PAV3);
try
{
FileWriter fwriter=new FileWriter(saveFile);
fwriter.write(p);
fwriter.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
// header
public StringBuilder header(StringBuilder b, String com) {
// 1.
b.append("H");
// 2.
b.append(" ");
// 3.4.
b.append(this.retriSysDateTime());
// 5.
b.append(com);
// 6.
for (int i = 0; i < 12; i++) {
b.append(" ");
}
// 7.
b.append("\n");
return b;
}
// detail
public StringBuilder detail(StringBuilder b, String filename) {
// 1.
b.append("D");
// 2.
b.append(" ");
// 3.
b.append(this.fillZero(filename, 30, "L"));
// 4.
b.append("\n");
return b;
}
// trailer
public StringBuilder trailer(StringBuilder b, int t) {
// 1.
b.append("T");
// 2.
b.append(" ");
// 3.
b.append(this.fillZero(t, 6, "R"));
// 4.
for (int i = 0; i < 24; i++) {
b.append(" ");
}
// 5.
b.append("\n");
return b;
}
// 左邊補0
public String LeftZero(int f, int len) {
String totalcount = "";
String count = String.valueOf(f);
int l = count.length();
for (int j = 0; j < len - l; j++) {
totalcount += "0";
}
totalcount += count;
return totalcount;
}
// 右邊補0
public String RightZero(int f, int len) {
String totalcount = "";
String count = String.valueOf(f);
int l = count.length();
totalcount += count;
for (int j = 0; j < len - l; j++) {
totalcount += "0";
}
return totalcount;
}
// 系統日與FileDate做比較
public boolean compare(String sysDate, String fileDate) {
// System.out.println("Compare:"+sysDate+'/'+fileDate);
if (fileDate.equals(""))
fileDate = "19000000";
if (sysDate.compareTo(fileDate) >= 0) {
return true;
} else {
return false;
}
}
//判斷是否存在文件代碼是 h,i,j,k
public boolean isExistHIJK(String strLine){
if(StringUtils.contains(strLine, "TBLD") || StringUtils.contains(strLine, "BV_")){
return true;
}
return false;
}
// 補0
// Example
// p.fillZero(21, 5, "R") ==>21000
// p.fillZero(21, 5, "L") ==>00021
private String fillZero(int f, int len, String LR) {
String totalcount = "";
String count = String.valueOf(f);
int l = count.length();
if (LR.equalsIgnoreCase(this.Right)) {
totalcount += count;
}
for (int j = 0; j < len - l; j++) {
totalcount += "0";
}
if (!LR.equalsIgnoreCase(this.Right)) {
totalcount += count;
}
return totalcount;
}
public String fillZero(Object f, int len, String LR) {
String totalcount = "";
String count = String.valueOf(f);
int l = count.length();
if (LR.equalsIgnoreCase(this.Right)) {
totalcount += count;
}
for (int j = 0; j < len - l; j++) {
totalcount += "0";
}
if (!LR.equalsIgnoreCase(this.Right)) {
totalcount += count;
}
return totalcount;
}
public boolean isHIJK(String filename) {
if (filename.toUpperCase().startsWith("TBLD")
|| filename.toUpperCase().startsWith("BV_UPDATE3")
|| filename.toUpperCase().startsWith("BV_STARTUP3")
|| filename.toUpperCase().startsWith("BV_MAIN3")) {
return true;
}
return false;
}
public String whichHIJK(String filename) {
if (filename.toUpperCase().startsWith("TBLD")) {// h
return "h";
} else if (filename.toUpperCase().startsWith("BV_UPDATE3")) {// i
return "i";
} else if (filename.toUpperCase().startsWith("BV_STARTUP3")) {// j
return "j";
} else if (filename.toUpperCase().startsWith("BV_MAIN3")) {// k
return "k";
}
return "";
}
public String retriveFileDate(String filename) {
// System.out.println(filename+'/'+filename.length());
if (filename.toUpperCase().startsWith("TBLD")
&& filename.length() == 17) {// h
return filename.substring(5, 13);
} else if (filename.toUpperCase().startsWith("BV_UPDATE3")
&& filename.length() == 19) {// i
return filename.substring(11, 19);
} else if (filename.toUpperCase().startsWith("BV_STARTUP3")
&& filename.length() == 20) {// j
return filename.substring(12, 20);
} else if (filename.toUpperCase().startsWith("BV_MAIN3")
&& filename.length() == 21) {// k
return filename.substring(9, 17);
}
return "00000000";
}
//取得系統時間
public String retriSysDate() {
Calendar cl = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(cl.getTime());
}
public String retriSysDateTime() {
Calendar cl = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(cl.getTime());
}
}
沒有留言:
張貼留言