📄 filewriter.java
字号:
package issa.webspider;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
public class FileWriter {
URL url;
String downLoadDir;
public FileWriter(URL url) {
this.url = url;
this.downLoadDir = CommonVar.downLoadDir;
}
public File writeToFile(Reader reader) {
try {
String stringFile = makeLocalPath();
String stringPath = splitPath(stringFile);
File file = new File(stringFile);
File dir = new File(stringPath);
if (!dir.exists()) {
// System.out.println(dir + " is not exists!");
dir.mkdirs();
}
if (!file.exists()) {
// System.out.println(file + " is not exists!");
file.createNewFile();
}
// InputStream in = new BufferedInputStream(url.openStream());
// InputStreamReader reader = new InputStreamReader(in);
java.io.FileWriter fw = new java.io.FileWriter(file);
int c;
while ((c = reader.read()) != -1) {
fw.write(c);
}
fw.flush();
fw.close();
return file;
} catch (IOException e) {
return null;
}
}
private String makeLocalPath() {
URI parentUri;
try {
parentUri = url.toURI();
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
StringBuilder sb = new StringBuilder();
sb.append(downLoadDir);
sb.append("\\");
sb.append(parentUri.getHost());
sb.append(parentUri.getPath());
return replaceSlash(sb.toString());
}
private String replaceSlash(String url) {
char[] temp = url.toCharArray();
for (int i = 0; i < temp.length; i++) {
if (temp[i] == '/' || temp[i] == '\\') {
temp[i] = File.separatorChar;
}
}
String tempString = new String(temp);
if (tempString.endsWith(File.separator)) {
return tempString + "noname.html";
} else {
return tempString;
}
}
private String splitPath(String url) {
int slash = url.lastIndexOf(File.separator);
return url.substring(0, slash + 1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -