本文整理汇总了Java中happy.coding.io.FileIO.readAsString方法的典型用法代码示例。如果您正苦于以下问题:Java FileIO.readAsString方法的具体用法?Java FileIO.readAsString怎么用?Java FileIO.readAsString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类happy.coding.io.FileIO
的用法示例。
在下文中一共展示了FileIO.readAsString方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ReadFolder
import happy.coding.io.FileIO; //导入方法依赖的package包/类
public static void ReadFolder(String dirPath) throws Exception
{
dirPath = FileIO.makeDirPath(dirPath);
File dir = new File(dirPath);
if (!dir.isDirectory()) throw new Exception(dirPath + " is not a directory");
File[] files = dir.listFiles();
for (File file : files)
{
String results = file.getName() + "\r\n";
results += FileIO.readAsString(file.getPath(), new String[] { "MAE", "MAUE" });
FileIO.writeString(dirPath + "Results.txt", results, true);
}
}
示例2: collectResults
import happy.coding.io.FileIO; //导入方法依赖的package包/类
public static void collectResults() throws Exception
{
String mode = params.BATCH_RUN ? DatasetMode.batch.label : params.DATASET_MODE.label;
String program = methodId + " [" + mode + "]";
/* Collect result files to specific directory */
Path source = FileSystems.getDefault().getPath("results.txt");
Path target = FileSystems.getDefault().getPath(
FileIO.makeDirectory(AbstractCF.params.RESULTS_DIRECTORY + Dataset.LABEL) + program + "@"
+ Dates.now() + ".txt");
Files.copy(source, target);
if (params.BATCH_RUN && params.numRunMethod > 1 && numRunMethod < params.numRunMethod) FileIO.empty(source
.toString());
/* Send email to notify results */
if (params.EMAIL_NOTIFICATION)
{
String text = FileIO.readAsString(source.toString());
Gmailer notifier = new Gmailer();
//notifier.getProps().setProperty("mail.to", "[email protected]");
//notifier.getProps().setProperty("mail.to", "[email protected]");
notifier.getProps().setProperty("mail.to", "[email protected]");
//notifier.getProps().setProperty("mail.bcc", "[email protected]");
notifier.getProps()
.setProperty("mail.subject", Dataset.LABEL + ": " + program + " From " + Systems.getIP());
notifier.send(text, target.toString());
}
}
示例3: run_dvd_ratings
import happy.coding.io.FileIO; //导入方法依赖的package包/类
public void run_dvd_ratings(String url) throws Exception {
String[] data = url.split(": ");
String category = data[0];
String category_url = data[1];
category_url = category_url.substring(0, category_url.lastIndexOf('_'));
String categoryID = category_url.substring(category_url
.lastIndexOf('_') + 1);
String catPath = FileIO.makeDirPath(desktop, domain, category);
File Dir = new File(catPath);
File[] prodDirs = Dir.listFiles();
int tk = prodDirs.length;
for (int k = 0; k < tk; k++) {
File prodDir = prodDirs[k];
// for each product
String productID = prodDir.getName();
if (productID.equals("webPages"))
continue;
if (!prodDir.isDirectory())
continue;
String prodPath = FileIO.makeDirPath(catPath, productID);
String reviewPath = FileIO
.makeDirPath(prodPath, "Detailed_Reviews");
String dvdPath = prodPath + "dvd-ratings.txt";
FileIO.deleteFile(dvdPath);
File reviewDir = new File(reviewPath);
File[] reviewDirs = reviewDir.listFiles();
List<String> reviews = new ArrayList<>();
for (int i = 0; i < reviewDirs.length; i++) {
// for each review
File reviewFile = reviewDirs[i];
String reviewID = reviewFile.getName();
Logs.debug("{}: {} ({}/{}): {} ({}/{})", category, productID,
(k + 1), tk, reviewID, (i + 1), reviewDirs.length);
String dirPath = FileIO.makeDirPath(reviewPath, reviewID);
String ratingPath = dirPath + reviewID + ".html";
if (!FileIO.exist(ratingPath))
continue; // review page is not existing
String html = FileIO.readAsString(ratingPath);
Document doc = Jsoup.parse(html);
Element div = doc.select("div#OH_BingUserInfo").first();
if (div == null)
continue; // no user review exists
// user-info
Element a = div.select("p.m-reer-usertab.clearfix a.black")
.first();
String raw = a.attr("onmousedown");
raw = raw.substring(raw.indexOf("(") + 1, raw.lastIndexOf(")"));
String userUrl = raw.replace(",", "").replace("'", "");
String userID = userUrl.substring(userUrl.lastIndexOf('_') + 1);
// user-rating value
Element r = div.select(
"p.m-reer-usertab.clearfix img.ratingStars").first();
String rating = r.attr("alt");
// user-rating date
div = doc.select("div#OH_BingUserOpinion").first();
Element date = div
.select("div.m-reer-opheader.reviewTitle span.m-reer-ddwrap span[property]")
.first();
String datetime = date.attr("content");
// content
String review = userID + "," + productID + "," + categoryID
+ "," + reviewID + "," + rating + "," + datetime + ","
+ userUrl;
reviews.add(review);
}
FileIO.writeList(dvdPath, reviews);
}
}
示例4: run_products
import happy.coding.io.FileIO; //导入方法依赖的package包/类
public void run_products(String url) throws Exception {
String[] data = url.split(": ");
String category = data[0];
// String link = data[1];
String dirPath = FileIO.makeDirPath(desktop, domain, category);
List<String> links = FileIO.readAsList(dirPath + "movies.txt");
int tk = links.size();
for (int k = 0; k < tk; k++) {
String link = links.get(k);
String[] d = link.split("::");
String id = d[0];
String name = d[1];
String productLink = d[2];
int idx = productLink.lastIndexOf("/");
String p1 = productLink.substring(0, idx) + "/Reviews";
String reviewLink = p1 + productLink.substring(idx);
// create folder
String path = FileIO.makeDirectory(dirPath, id);
// product page
String html = null;
String pagePath = path + id + ".html";
if (!FileIO.exist(pagePath)) {
html = read_url(productLink);
FileIO.deleteFile(path + name + ".html");
FileIO.writeString(pagePath, html);
}
// product reviews
// get first page anyway to identify the maximum pages
path = FileIO.makeDirectory(path, "Reviews");
String reviewPath = path + "page_1.html";
if (FileIO.exist(reviewPath)) {
html = FileIO.readAsString(reviewPath);
} else {
html = read_url(reviewLink);
FileIO.writeString(reviewPath, html);
}
Logs.debug(category + ": " + id + " (" + (k + 1) + "/" + tk + ")"
+ ": page " + 1);
Document doc = Jsoup.parse(html);
Elements nav = doc.select("div#Pagination");
if (!nav.isEmpty()) {
int maxPage = 1;
Elements last = nav.select("li.last");
if (!last.isEmpty())
maxPage = Integer.parseInt(last.first().text()); // more
// than
// 11
// pages
else
maxPage = Integer.parseInt(nav.select("li").last().text()); // less
// or
// equal
// 11
// pages
for (int i = 2; i <= maxPage; i++) {
String filePath = path + "page_" + i + ".html";
if (FileIO.exist(filePath))
continue;
reviewLink = reviewLink + "/Start/" + ((i - 1) * 15);
html = read_url(reviewLink);
FileIO.writeString(filePath, html);
Logs.debug(category + ": " + id + " (" + (k + 1) + "/" + tk
+ ")" + ": page " + i + "/" + maxPage);
}
}
}
}
示例5: parseCategoryPages
import happy.coding.io.FileIO; //导入方法依赖的package包/类
public static void parseCategoryPages() throws Exception {
String filePath = FileIO.getResource("dvd.ciao.txt");
List<String> urls = FileIO.readAsList(filePath);
String dir = Systems.getDesktop() + "dvd.ciao.co.uk\\";
for (String url : urls) {
// each category
String[] data = url.split(": ");
String category = data[0];
String dirCate = FileIO.makeDirPath(dir, category);
String dirPath = FileIO.makeDirPath(dir, category, "webPages");
// clear
FileIO.deleteFile(dirCate + "movies.txt");
File dirs = new File(dirPath);
for (File f : dirs.listFiles()) {
// each web page
String html = FileIO.readAsString(dirPath + f.getName());
Document doc = Jsoup.parse(html);
Logs.debug(category + ": " + f.getName());
List<String> movies = new ArrayList<>();
Elements products = doc.select("td.prodInfo");
for (Element product : products) {
// each product
Element prod = product.select("p.prodName").first();
String name = prod.text();
String link = prod.select("a").first().attr("href");
String id = link.substring(link.lastIndexOf("_") + 1);
// number of user reviews
prod = product.select("p.prodRating").first();
String cnt = prod.select(".userReviewsCount").text()
.replace("(", "").replace(")", "");
int count = 0;
if (!cnt.isEmpty())
count = Integer.parseInt(cnt);
// do not consider movies without any reviews
if (count > 0) {
String movie = id + "::" + name + "::" + link;
movies.add(movie);
}
}
FileIO.writeList(dirCate + "movies.txt", movies, null, true);
}
}
}
示例6: getAllTrust
import happy.coding.io.FileIO; //导入方法依赖的package包/类
public static void getAllTrust() throws Exception {
Map<String, String> users = new HashMap<>();
List<String> userLines = FileIO.readAsList(FileIO
.getResource("users.txt"));
for (String line : userLines) {
String[] data = line.split(",");
users.put(data[1], data[0]);
}
String usersPath = FileIO.makeDirPath(desktop, domain,
"users.ciao.co.uk");
FileIO.deleteFile(usersPath + "trust.txt");
File dir = new File(usersPath);
File[] files = dir.listFiles();
for (int i = 0, n = files.length; i < n; i++) {
// for each user
File userFile = files[i];
final String userID = userFile.getName();
String html = null;
Document doc = null;
List<String> friends = new ArrayList<>();
File[] pages = userFile.listFiles();
for (int j = 0, m = pages.length; j < m; j++) {
// for each trust page
File file = pages[j];
String name = file.getName();
if (name.startsWith("friends")) {
html = FileIO.readAsString(file.getPath());
doc = Jsoup.parse(html);
Element trustTable = doc.select("form table.trust").first();
Elements trs = trustTable.select("tbody tr");
for (Element tr : trs) {
Element td = tr.select("td").get(1);
Element a = td.select("a").first();
String link = a.attr("href");
if (users.containsKey(link)) {
friends.add(users.get(link));
}
}
}
}
FileIO.writeList(usersPath + "trust.txt", friends,
new Converter<String, String>() {
@Override
public String transform(String friend) {
return userID + "," + friend + ",1";
}
}, true);
}
}
示例7: parseReviewPages
import happy.coding.io.FileIO; //导入方法依赖的package包/类
public static void parseReviewPages() throws Exception {
String filePath = FileIO.getResource("dvd.ciao.txt");
List<String> urls = FileIO.readAsList(filePath);
String dir = Systems.getDesktop() + "dvd.ciao.co.uk\\";
for (String url : urls) {
// each category
String[] data = url.split(": ");
String category = data[0];
String dirCate = FileIO.makeDirPath(dir, category);
File dirs = new File(dirCate);
for (File f : dirs.listFiles()) {
// each product folder
if (f.getName().equals("webPages"))
continue;
if (!f.isDirectory())
continue;
String prodPath = FileIO.makeDirPath(dirCate, f.getName());
String revwPath = FileIO.makeDirPath(prodPath, "Reviews");
List<String> reviews = new ArrayList<>();
File reviewDirs = new File(revwPath);
for (File rf : reviewDirs.listFiles()) {
// each review page
String html = FileIO.readAsString(rf.getPath());
Document doc = Jsoup.parse(html);
Elements es = doc.select("div.m-shortReviewSnippet");
for (Element e : es) {
Element a = e.select(
"p.m-shet-review-title a.ReviewTitle").first();
if (a == null)
continue; // some reviews do not have specific link
// to detailed contents
// url
String link = a.attr("href");
int idx = link.lastIndexOf("_");
String id = link.substring(idx + 1);
reviews.add(id + "::" + link);
}
}
FileIO.writeList(prodPath + "reviews.txt", reviews, null, false);
}
}
}
示例8: crawl_comments
import happy.coding.io.FileIO; //导入方法依赖的package包/类
public void crawl_comments(String url) throws Exception
{
String html = read_url(url);
Document doc = Jsoup.parse(html);
String name = doc.select("div.detail_head_name h1").first().text();
name = Strings.filterWebString(name, '_');
String val = doc.select("#detail_nav li a").first().attr("href");
String id = val.substring(val.lastIndexOf("/") + 1);
String dirPath = dir + name + "/comments/";
FileIO.makeDirectory(dirPath);
// save rating pages
int max = 1;
boolean maxSet = false;
url = url + "/commentlist";
for (int k = 0; k <= max; k++)
{
String page_file = dirPath + "page_" + (k + 1) + ".html";
Logs.debug(name + " comments with page: " + (k + 1) + "/" + (max + 1));
String contents = null;
if (!FileIO.exist(page_file))
{
String link = "http://www.gewara.com/ajax/common/qryComment.xhtml?pageNumber="
+ k
+ "&relatedid="
+ id
+ "&title=&issue=false&hasMarks=true&tag=movie&isPic=true&isVideo=false&pages=true&maxCount=20&userLogo=";
contents = read_url(link);
FileIO.writeString(page_file, contents);// new String(contents.getBytes("utf-8"), "utf-8"));
} else
{
contents = FileIO.readAsString(page_file);
}
// find the maximum page num;
if (!maxSet)
{
Document doc2 = Jsoup.parse(contents);
Elements es = doc2.select("div#page a");
Element e = es.get(es.size() - 2);
max = Integer.parseInt(e.attr("lang"));
maxSet = true;
}
}
}
示例9: run_reviews
import happy.coding.io.FileIO; //导入方法依赖的package包/类
public void run_reviews(String url) throws Exception
{
url = url.trim();
String html = read_url(url);
Document doc = Jsoup.parse(html);
String name = doc.select("span[property=v:itemreviewed]").text();
name = Strings.filterWebString(name, '_');
String dirPath = dir + name + "/reviews/";
FileIO.makeDirectory(dirPath);
// save rating pages
int k = 0;
url = url + "reviews";
String link = url;
while (true)
{
k++;
String page = null;
String path = dirPath + "page_" + k + ".html";
if (!FileIO.exist(path))
{
page = read_url(link);
FileIO.writeString(path, page);
Logs.debug(name + " reviews with page: " + k);
} else
{
page = FileIO.readAsString(path);
}
// find the next page link;
Document doc2 = Jsoup.parse(page);
Elements es = doc2.select("div#paginator a.next");
if (es == null || es.size() == 0)
{
break;
} else
{
link = url + es.first().attr("href");
}
}
}
示例10: run_comments
import happy.coding.io.FileIO; //导入方法依赖的package包/类
public void run_comments(String url) throws Exception
{
url = url.trim();
String html = read_url(url);
Document doc = Jsoup.parse(html);
String name = doc.select("span[property=v:itemreviewed]").text();
name = Strings.filterWebString(name, '_');
String dirPath = dir + name + "/comments/";
FileIO.makeDirectory(dirPath);
// save rating pages
int k = 0;
url = url + "comments";
String link = url;
while (true)
{
k++;
String page_file = dirPath + "page_" + k + ".html";
String contents = null;
if (!FileIO.exist(page_file))
{
contents = read_url(link);
FileIO.writeString(page_file, contents);
Logs.debug(name + " comments with page: " + k);
} else
{
contents = FileIO.readAsString(page_file);
}
// find the next page link;
Document doc2 = Jsoup.parse(contents);
Elements es = doc2.select("div#paginator a.next");
if (es == null || es.size() == 0)
{
break;
} else
{
link = url + es.first().attr("href");
}
}
}