When it comes to Java's local storage, it must be operated using IO streams.
First, we need a function createNewFile to create a file:
Copy the code code as follows:
public static boolean createNewFile(String filePath) {
boolean isSuccess = true;
// If there is, convert "//" to "/", if not, there will be no change.
String filePathTurn = filePath.replaceAll("////", "/");
// Filter out file names first
int index = filePathTurn.lastIndexOf("/");
String dir = filePathTurn.substring(0, index);
//Create the folder again
File fileDir = new File(dir);
isSuccess = fileDir.mkdirs();
//Create file
File file = new File(filePathTurn);
try {
isSuccess = file.createNewFile();
} catch (IOException e) {
isSuccess = false;
e.printStackTrace();
}
return isSuccess;
}
Then, we need a function to write to the file:
Copy the code code as follows:
public static boolean writeIntoFile(String content, String filePath,
boolean isAppend) {
boolean isSuccess = true;
// Filter out file names first
int index = filePath.lastIndexOf("/");
String dir = filePath.substring(0, index);
//Create a path to the file
File fileDir = new File(dir);
fileDir.mkdirs();
//Create the file under the path again
File file = null;
try {
file = new File(filePath);
file.createNewFile();
} catch (IOException e) {
isSuccess = false;
e.printStackTrace();
}
// write to file
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(file, isAppend);
fileWriter.write(content);
fileWriter.flush();
} catch (IOException e) {
isSuccess = false;
e.printStackTrace();
} finally {
try {
if (fileWriter != null)
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return isSuccess;
}
We encapsulate these two functions into a FileReaderWriter.java file for subsequent use.
Then we return to Zhihu crawler.
We need to add a function to Zhihu's Zhihu encapsulation class to format the typesetting when writing to the local.
Copy the code code as follows:
public String writeString() {
String result = "";
result += "Question:" + question + "/r/n";
result += "Description:" + questionDescription + "/r/n";
result += "Link:" + zhihuUrl + "/r/n";
for (int i = 0; i < answers.size(); i++) {
result += "answer" + i + ":" + answers.get(i) + "/r/n";
}
result += "/r/n/r/n";
return result;
}
OK, that's almost it. Next, change System.out.println in the main method to
Copy the code code as follows:
//Write locally
for (Zhihu zhihu : myZhihu) {
FileReaderWriter.writeIntoFile(zhihu.writeString(),
"D:/Zhihu_Editor's recommendation.txt", true);
}
After running, you can see that the content originally seen in the console has been written to the local txt file:
At first glance, there is nothing wrong with it. If you take a closer look, you will find a problem: there are too many html tags, mainly <b> and <br>.
We can process these tags during output.
First replace <br> with /r/n in the io stream, and then delete all html tags, so that it will look much clearer.
Copy the code code as follows:
public String writeString() {
// Splice and write local strings
String result = "";
result += "Question:" + question + "/r/n";
result += "Description:" + questionDescription + "/r/n";
result += "Link:" + zhihuUrl + "/r/n";
for (int i = 0; i < answers.size(); i++) {
result += "answer" + i + ":" + answers.get(i) + "/r/n/r/n";
}
result += "/r/n/r/n/r/n/r/n";
// Filter the html tags in it
result = result.replaceAll("<br>", "/r/n");
result = result.replaceAll("<.*?>", "");
return result;
}
The replaceAll function here can use regular expressions, so all <> tags are deleted at the end.