/**
* 写文件
*
* @param path
* @param content
*/
public static boolean write(String path, String content) {
String s = new String();
String s1 = new String();
BufferedWriter output = null;
try {
File f = new File(path);
if (f.exists()) {
} else {
System.out.println("文件不存在,正在创建...");
if (f.createNewFile()) {
System.out.println("文件创建成功!");
} else {
System.out.println("文件创建失败!");
}
}
BufferedReader input = new BufferedReader(new FileReader(f));
while ((s = input.readLine()) != null) {
s1 += s + "\n";
}
System.out.println("原文件内容:" + s1);
input.close();
s1 += content;
output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
* @Description: TODO
* @param @param fileUrl文件链接
* @param @param topName硬盘名
* @param @param fileName文件名
* @param @param tagName文件夹名
* @param @param type 后缀名
* @return void
* @throws
* @author joe
* @date 2015-3-6
*/
public void downLoadFile(String fileUrl, String topName, String fileName,
String tagName, String type) {
// 下载网络文件
int bytesum = 0;
int byteread = 0;
try {
URL url = new URL(fileUrl);
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
File fileD = new File(topName + ":/" + tagName);
// 如果文件夹不存在则创建
if (!fileD.exists() && !fileD.isDirectory()) {
System.out.println("正在新建目录");
fileD.mkdirs();
;
} else {
System.out.println("目录存在");
}
File file = new File(topName + ":/" + tagName + "/" + fileName
+ "." + type);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream fs = new FileOutputStream(topName + ":/" + tagName
+ "/" + fileName + "." + type);
byte[] buffer = new byte[1204];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
System.out.println("downloaded ok");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws ClientProtocolException,
IOException {
CrawlMethodManager manager = new CrawlMethodManager();
}
} |