📄 schedule.java
字号:
package myBeans;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Schedule {
public static synchronized String readFile(String path){
FileReader fr = null;
BufferedReader br = null;
try{
System.out.println("CommonClass.readFile:"+path);
fr=new FileReader(path);
br=new BufferedReader(fr);
String line = null;
line = br.readLine();
StringBuffer sb=new StringBuffer();
int i=0;
for(i=0;line!=null;line=br.readLine()){
sb.append(line+"\r\n");
}
//System.out.println(sb.toString());
return sb.toString();
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
try{
if(fr != null)
fr.close();
if(br != null)
br.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static synchronized boolean writeFile(String filePath,String content){
FileWriter fw = null;
try{
System.out.println("CommonClass.writeFile:"+filePath);
// if(CommonClass.fileExist(filePath)){
// //if(CommonClass.deleteFile(filePath)) {
// FileWriter fw = new FileWriter(filePath);
// fw.write(content);
// System.out.println(content);
// fw.close();
// //}
// }else{
fw = new FileWriter(filePath);
fw.write(content);
// System.out.println(content);
// }
return true;
}catch(Exception e1){
e1.printStackTrace();
return false;
}finally{
try{
if(fw != null)
fw.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static synchronized boolean fileExist(String path){
boolean result = true;
try{
File file = new File(path);
if(file.exists())
result = true;
else
result = false;
} catch(Exception e){
e.printStackTrace();
result = false;
}
return result;
}
public static synchronized void record(String path, String eno, String date, String content){
String newContent = "";
boolean foundLine = false;
FileReader fr = null;
BufferedReader br = null;
try {
if (fileExist(path)) {
fr = new FileReader(path);
String line;
br = new BufferedReader(fr);
line = br.readLine();
while (line != null) {
if (line.indexOf(eno + ";") != -1&& line.indexOf(date + ";") != -1) {
newContent += eno + ";" + date + ";" + content + "\r\n";
foundLine = true;
line = br.readLine();
continue;
}
newContent += line + "\r\n";
line = br.readLine();
}
if (!foundLine) {
newContent += eno + ";" + date + ";" + content + "\r\n";
}
} else {
newContent = eno + ";" + date + ";" + content + "\r\n";
}
writeFile(path, newContent);
} catch(Exception e){
e.printStackTrace();
} finally{
try{
if(fr != null)
fr.close();
if(br != null)
br.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static synchronized String findLine(String path, String eno, String date){
FileReader fr = null;
BufferedReader br = null;
try{
if(fileExist(path)){
fr = new FileReader(path);
String line;
br = new BufferedReader(fr);
line = br.readLine();
while(line != null){
if(line.indexOf(eno+";")!=-1&&line.indexOf(date+";")!=-1){
return line;
}
line = br.readLine();
}
}
return "";
} catch (Exception e){
e.printStackTrace();
return "";
} finally{
try{
if(fr != null)
fr.close();
if(br != null)
br.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static synchronized List queryRecord(String path, String eno, String startDate, String endDate){
List resultList = new ArrayList();
FileReader fr = null;
BufferedReader br = null;
try{
if(fileExist(path)){
fr = new FileReader(path);
String line;
br = new BufferedReader(fr);
line = br.readLine();
while(line != null){
if(line.indexOf(eno+";")!=-1){
String date = line.split(";")[1];
if(dateCompare(date, startDate, endDate))
resultList.add(line);
}
line = br.readLine();
}
}
return resultList;
} catch (Exception e){
e.printStackTrace();
return null;
} finally{
try{
if(fr != null)
fr.close();
if(br != null)
br.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static boolean dateCompare(String date, String start, String end){
if(start.equalsIgnoreCase("")&&end.equalsIgnoreCase(""))
return true;
StringTokenizer dateST = new StringTokenizer(date,"/",false);
if(!start.equalsIgnoreCase("")){
StringTokenizer startST = new StringTokenizer(start,"/",false);
int d = Integer.parseInt(dateST.nextToken());
int s = Integer.parseInt(startST.nextToken());
if(d<s)
return false;
if(d==s){
d = Integer.parseInt(dateST.nextToken());
s = Integer.parseInt(startST.nextToken());
if(d<s)
return false;
if(d==s){
d = Integer.parseInt(dateST.nextToken());
s = Integer.parseInt(startST.nextToken());
if(d<s)
return false;
}
}
}
dateST = new StringTokenizer(date,"/",false);
if (!end.equalsIgnoreCase("")) {
StringTokenizer endST = new StringTokenizer(end, "/", false);
int d = Integer.parseInt(dateST.nextToken());
int e = Integer.parseInt(endST.nextToken());
if (d>e)
return false;
if(d==e){
d = Integer.parseInt(dateST.nextToken());
e = Integer.parseInt(endST.nextToken());
if(d>e)
return false;
if(d==e){
d = Integer.parseInt(dateST.nextToken());
e = Integer.parseInt(endST.nextToken());
if(d>e)
return false;
}
}
}
return true;
}
public static void main(String [] args){
System.out.println(Schedule.dateCompare("2008/08/08", "", ""));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -