/**
*
* @Description: TODO
* @param @param 硬盘名
* @param @param 文件名
* @param @param 文件夹名
* @param @param 保存后缀名
* @param @param 保存的内容
* @return void
* @throws
* @author joe
* @date 2015-3-6
*/
public static void writeToFile(String topName, String fileName,
String tagName, String type, String content) {
File dirFile = null;
try {
dirFile = new File(topName + ":\\" + tagName);
if (!(dirFile.exists()) && !(dirFile.isDirectory())) {
boolean creadok = dirFile.mkdirs();
if (creadok) {
System.out.println(" ok:创建文件夹成功! ");
} else {
System.out.println(" err:创建文件夹失败! ");
}
}
} catch (Exception e) {
e.printStackTrace();
}
String fullPath = dirFile + "/" + fileName + "." + type;
write(fullPath, content);
}
/**
* 写文件
*
* @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 static void downLoadFile(String fileUrl, String topName, String fileName,
String tagName, String type) {
// 下载网络文件
int bytesum = 0;
int byteread = 0;
try {
System.out.println("开始下载"+fileUrl);
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();
}
}
} |