-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9a7147
commit 024e3d4
Showing
3 changed files
with
198 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package utils; | ||
|
||
/** | ||
* Created by ZQ on 2016/4/22. | ||
*/ | ||
public abstract class Constants { | ||
public static final String USER_AGENT = "2016IR201330551365"; | ||
public static final String SEED_URL = "http://www.scut.edu.cn"; | ||
public static final String DATE_FORMAT = "yyyy-mm-dd hh:mm:ss.SSS"; | ||
public static final String TYPE_CONNECTING = "Connecting"; | ||
public static final String TYPE_FETCHING = "Fetching"; | ||
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
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; | ||
|
||
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(); | ||
} | ||
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); | ||
} | ||
} |