package Util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.csvreader.CsvReader;
/**
*
* Copyright 2014 Bitong
*
* All right reserved
*
* Create on 2014-1-10 p.m 3:11:11
*
* Author jie.ying
*
* Class explain:file operation class
*/
public class FileUtil {
/**
* Store finished URL id
*/
public static List<String> finishKeyList = new ArrayList<String>();
/**
* Not completed page index
*/
public static int noFinishPage = 1;
/**
* Not completed URL id
*/
public static String noFinishKey = null;
/**
* count completed file number
*/
public static int index = 0;
/**
*
* Illustrate:create one file
*
* @param fileName
* @return boolean
*
*/
public static boolean createFile(String fileName) {
File file = new File(fileName);
if (file.exists()) {
System.out.println("Create the file" + fileName + "failed,target file already exists!");
return false;
}
if (fileName.endsWith(File.separator)) {
System.out.println("Create the file" + fileName + "failed,the target file can not be a directory!");
return false;
}
System.out.println(file.getParentFile());
if (!file.getParentFile().exists()) {
System.out.println("The directory of target file is not exist,ready to create...");
if (!file.getParentFile().mkdirs()) {
System.out.println("Create the directory of the target file failed!");
return false;
}
}
try {
if (file.createNewFile()) {
System.out.println("Create the file" + fileName + "success!");
return true;
} else {
System.out.println("Create the file" + fileName + "failed!");
return false;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Create the file" + fileName + "failed!");
return false;
}
}
/**
*
* Illustrate:create file directory
*
* @param FileDirName
* @return boolean
*
*/
public static boolean createDir(String FileDirName) {
File dir = new File(FileDirName);
if (dir.exists()) {
System.out.println("Create the directory" + FileDirName + "failed,the target directory is exist!");
return false;
}
if (!FileDirName.endsWith(File.separator))
FileDirName = FileDirName + File.separator;
//Create single directory
if (dir.mkdirs()) {
System.out.println("Create the directory" + FileDirName + "success!");
return true;
} else {
System.out.println("Create the directory" + FileDirName + "failed!");
return false;
}
}
/**
*
* Illustrate:write content into file
*
* @param content
* @param fileName
* @return boolean
*
*/
public static boolean writeFile(String content, String fileName) {
FileWriter fw;
if (StringUtils.isEmpty(content)) {
System.out.println("---------------------------------------------Content is null");
return false;
}
try {
fw = new FileWriter(fileName);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.flush();
bw.close();
System.out.println(fileName + "write success");
return true;
} catch (IOException e) {
e.printStackTrace();
System.out.println("Write failed");
return false;
}
}
/**
*
* Illustrate:write log
*
* @param content
* @param fileName
* @return boolean
*
*/
public static boolean writeToLog(String content, String fileName) {
FileWriter fileWriter;
try {
if(StringUtils.isEmpty(content)){
return false;
}
//True is append write
fileWriter = new FileWriter(fileName, true);
//Next line write
String nextLine = System.getProperty("line.separator");
fileWriter.write(content + nextLine);
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
/**
*
* Illustrate:read log
*
* @param fileName
* @return boolean
*
*/
public static boolean readLog(String fileName) {
try {
String encoding = "UTF-8";
File file = new File(fileName);
if(!file.exists()){
file.createNewFile();
}
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
if(lineTxt.contains("finished")){
finishKeyList.add(lineTxt.split(":")[0]);
//If URL finished,Initialize Page number
noFinishPage = 1;
}
if(lineTxt.contains("end")){
String[] strArray = lineTxt.split(":");
//If you manually interrupted, record the current page number
noFinishKey = strArray[0];
noFinishPage = Integer.parseInt(strArray[1]);
index++;
}
if(lineTxt.contains("exception")){
String[] strArray = lineTxt.split(":");
//Exception interruption, record the current page number
noFinishKey = strArray[0];
noFinishPage = Integer.parseInt(strArray[1]);
}
}
read.close();
} else {
System.out.println("Can not find the file");
return false;
}
} catch (Exception e) {
System.out.println("Reading the file contents error");
e.printStackTrace();
return false;
}
return true;
}
/**
*
* Illustrate: read CVS file
*
* @param csvFilePath
* @return Map<String,String>
*
*/
public static Map<String, String> readCVS(String csvFilePath) {
Map<String, String> csvMap = new LinkedHashMap<String, String>();
try {
CsvReader reader = new CsvReader(csvFilePath, ';',
Charset.forName("UTF-8"));
while (reader.readRecord()) {
String[] csvArray = reader.getValues();
if (!StringUtils.isEmpty(csvArray[0])
&& !StringUtils.isEmpty(csvArray[1])) {
csvMap.put(csvArray[0], csvArray[1]);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
csvMap.remove("id");
return csvMap;
}
/**
*
* Illustrate:parse XML file
*
* @param configurationFile
* @return Map<String,String>
*
*/
public static Map<String, String> parseXml(String configurationFile) {
String[] tagNames = { "binName", "exePath", "csvFilePath",
"downloadfolderPath", "pageSizeExpression",
"nextPageExpression", "clickExpression",
"threadSleepMilliSecond" };
Map<String, String> xmlMap = new HashMap<String, String>();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document doc = db.parse(new File(configurationFile));
for (String tagName : tagNames) {
NodeList nodeList = doc.getElementsByTagName(tagName);
for (int i = 0; i < nodeList.getLength(); i++) {
Element el = (Element) nodeList.item(i);
String value = el.getFirstChild().getNodeValue();
xmlMap.put(tagName, value);
}
}
Set<String> set = xmlMap.keySet();
for (String key : set) {
System.out.println(key + ":" + xmlMap.get(key));
}
} catch (Exception e) {
e.printStackTrace();
}
return xmlMap;
}
public static void main(String[] args) {
/*
* String dirName = "D:\\test1"; FileUtil.createDir(dirName);
*
* String fileName = dirName+"\\1.txt"; FileUtil.createFile(fileName);
*
* FileUtil.writeFile("text", fileName);
*/
/*
* String url = "http://s.1688.com/company/-B7C4D6AFC6A4B8EF.html";
*
* String str = url.substring(7); String str2 = str.replaceAll("/", "");
*
*
* System.out.println(str2);
*/
// FileUtil.readCVS("E:\\100years -copy.csv");
// parseXml("config/alibabaConfig.xml");
for (int i = 0; i < 3; i++) {
writeToLog(String.valueOf(i), "log/alibaba.log");
}
readLog("log/alibaba.log");
}
}
FileUtil
最新推荐文章于 2024-05-09 11:43:14 发布