Skip to content

Commit

Permalink
add log file output
Browse files Browse the repository at this point in the history
  • Loading branch information
FreedomZZQ committed Apr 23, 2016
1 parent 024e3d4 commit d69c3ed
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 140 deletions.
24 changes: 19 additions & 5 deletions src/crawler/Crawler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package crawler;

import utils.FileUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -125,11 +127,23 @@ public void addURL(String url) {

public void log(String type, String url, String tag){
String date = sFormat.format(new Date());
System.out.println(USER_AGENT + " "
+ date + " "
+ type + " "
+ url + " "
+ tag);

StringBuilder sb = new StringBuilder();
sb.append(USER_AGENT);
sb.append(" ");
sb.append(date);
sb.append(" ");
sb.append(type);
sb.append(" ");
sb.append(url);
sb.append(" ");
sb.append(tag);
sb.append("\n\r");
System.out.println(sb.toString());

FileUtils.writeFileAppend(LOG_FILE_NAME, sb.toString());


}


Expand Down
2 changes: 1 addition & 1 deletion src/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public abstract class Constants {
public static final String TYPE_PARSING = "Parsing";
public static final String TAG_SUCCESS = "Successful";
public static final String TAG_ERROR = "Error";
public static final String LOG_FILE_NAME = "IR201330551365LOG";
public static final String LOG_FILE_NAME = "IR201330551365LOG.txt";
}
147 changes: 13 additions & 134 deletions src/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -1,144 +1,23 @@
package utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;

public class FileUtils {
public static void deleteDir(File dir) {
File[] filelist = dir.listFiles();
for (File file : filelist) {
if (file.isFile()) {
file.delete();
} else {
deleteDir(file);
}
}
dir.delete();
}

public static void copy(File origin, File newfile) throws FileNotFoundException, IOException {
if (!newfile.getParentFile().exists()) {
newfile.getParentFile().mkdirs();
}
FileInputStream fis = new FileInputStream(origin);
FileOutputStream fos = new FileOutputStream(newfile);
byte[] buf = new byte[2048];
int read;
while ((read = fis.read(buf)) != -1) {
fos.write(buf, 0, read);
}
fis.close();
fos.close();
}

public static void writeFile(String fileName, String contentStr, String charset) throws FileNotFoundException, IOException {
byte[] content = contentStr.getBytes(charset);
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(content);
fos.close();
}

public static void writeFile(File file, String contentStr, String charset) throws FileNotFoundException, IOException {
byte[] content = contentStr.getBytes(charset);
FileOutputStream fos = new FileOutputStream(file);
fos.write(content);
fos.close();
}

public static void writeFileWithParent(String fileName, String contentStr, String charset) throws FileNotFoundException, IOException {
File file = new File(fileName);
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
byte[] content = contentStr.getBytes(charset);
FileOutputStream fos = new FileOutputStream(file);
fos.write(content);
fos.close();
}

public static void writeFileWithParent(File file, String contentStr, String charset) throws FileNotFoundException, IOException {
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
byte[] content = contentStr.getBytes(charset);
FileOutputStream fos = new FileOutputStream(file);
fos.write(content);
fos.close();
}

public static void writeFile(String fileName, byte[] content) throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(content);
fos.close();
}

public static void writeFile(File file, byte[] content) throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream(file);
fos.write(content);
fos.close();
}

public static void writeFileWithParent(String fileName, byte[] content) throws FileNotFoundException, IOException {
File file = new File(fileName);
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
public static void writeFileAppend(String fileName, String content) {
try {
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
//将写文件指针移到文件尾
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(content);
fos.close();
}

public static void writeFileWithParent(File file, byte[] content) throws FileNotFoundException, IOException {

File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(content);
fos.close();
}

public static byte[] readFile(File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[2048];
int read;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((read = fis.read(buf)) != -1) {
bos.write(buf, 0, read);
}

fis.close();
return bos.toByteArray();
}

public static byte[] readFile(String fileName) throws IOException {
File file = new File(fileName);
return readFile(file);
}

public static String readFile(File file, String charset) throws Exception {
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[2048];
int read;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((read = fis.read(buf)) != -1) {
bos.write(buf, 0, read);
}

fis.close();
return new String(bos.toByteArray(), charset);
}

public static String readFile(String fileName, String charset) throws Exception {
File file = new File(fileName);
return readFile(file, charset);
}
}

0 comments on commit d69c3ed

Please sign in to comment.