diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7deaf69 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +out +/.idea/workspace.xml + +*.class diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..52a48c3 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +jHTMLToMd \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..a852314 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..d821048 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/jHTMLToMd.iml b/.idea/jHTMLToMd.iml new file mode 100644 index 0000000..be0fe7f --- /dev/null +++ b/.idea/jHTMLToMd.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/jsoup_1_8_1.xml b/.idea/libraries/jsoup_1_8_1.xml new file mode 100644 index 0000000..67dd851 --- /dev/null +++ b/.idea/libraries/jsoup_1_8_1.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d3d882e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,158 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3117ddb --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md index fba65cc..13f0cd4 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,38 @@ jHTMLToMd ========= -java版的HTML转换为Markdown文件工具 -author robin chia -------- - -使用方法: - -一、转换url页面为.md文件 - String aurl = "your url"; - URL url = new URL(aurl); - String parsedtext1 = HtmlToMd.convert(url, 60000); - String mdpath = "/home/robin/Desktop/";//your md file path - FilesUtil.newFile("mdname", mdpath+parsedtext1, "utf-8"); - -二、转换本地的html文件为.md文件 - String pathfile = "/var/www/html/wiznote/推荐给开发人员的实用命令行工具.html"; - File f = new File(pathfile); - String parsedtext2 = HtmlToMd.convertfile(f, "gbk"); - String mdpath = "/home/robin/Desktop/";//your md file path - FilesUtil.newFile("mdname", mdpath+parsedtext2, "utf-8"); -三、可以根据相应的api来转换(深度探索...:)) - HtmlToMd.converthtml(String html, String charset); - HtmlToMd.htmlToJeykyllmd(String htmlpath,String mdpath,String charset); - HtmlToMd.htmlToHexoMd(String htmlpath,String mdpath,String charset); - ...... +java版的HTML转换为Markdown文件工具 +author robin chia +------- +***基于[https://github.com/pnikosis/jHTML2Md](https://github.com/pnikosis/jHTML2Md)项目修改而来*** -------- -注: 本工具使用了[jSoup](http://jsoup.org/)作为解析HTML的jar库文件 +使用方法: +一、转换url页面为.md文件 +```java +String url = "your url"; +URL url = new URL(url); +String parsedText = HtmlToMd.convert(url, 60000); +String mdPath = "/home/robin/Desktop/"; // your md file path +FilesUtil.newFile("mdName", mdPath + parsedText, "utf-8"); +``` +二、转换本地的html文件为.md文件 +```java +String pathFile = "test.html"; +File f = new File(pathFile); +String parsedText = HtmlToMd.convertFile(f, "gbk"); +String mdPath = "/home/robin/Desktop/"; // your md file path +FilesUtil.newFile("mdName", mdPath + parsedText, "utf-8"); +``` +三、可以根据相应的api来转换(深度探索...:)) +```java +HtmlToMd.convertHtml(String html, String charset); +HtmlToMd.htmlToJekyllMd(String htmlPath, String mdPath, String charset); +HtmlToMd.htmlToHexoMd(String htmlPath, String mdPath, String charset); +...... +``` +------- +注: 本工具使用了[jSoup](http://jsoup.org/)作为解析HTML的jar库文件 \ No newline at end of file diff --git a/lib/jsoup-1.7.3.jar b/lib/jsoup-1.7.3.jar deleted file mode 100755 index aa5c798..0000000 Binary files a/lib/jsoup-1.7.3.jar and /dev/null differ diff --git a/lib/jsoup-1.8.1.jar b/lib/jsoup-1.8.1.jar new file mode 100644 index 0000000..ae717d4 Binary files /dev/null and b/lib/jsoup-1.8.1.jar differ diff --git a/src/com/itsolife/htmltomarkdown/DateUtil.java b/src/com/itsolife/htmltomarkdown/DateUtil.java index b84eeae..303fb0d 100755 --- a/src/com/itsolife/htmltomarkdown/DateUtil.java +++ b/src/com/itsolife/htmltomarkdown/DateUtil.java @@ -10,553 +10,528 @@ /** * 日期操作工具类 - * @author robin * + * @author robin */ public class DateUtil { - public DateUtil() { - - } - - /** - * 得到当前时间值,格式:yyyy年MM月dd日HH时mm分ss秒 - * - * @return String值 - */ - public static String getNowTime() { - try { - Date date = new Date(); - SimpleDateFormat simpledateformat = new SimpleDateFormat( - "yyyy年MM月dd日HH时mm分ss秒sss"); - String s = simpledateformat.format(date); - return s; - } catch (Exception e) { - return ""; - } - } - - /** - * 得到当前日期值,格式:yyyy年MM月dd日 - * - * @return String值 - */ - public static String getNowDate() { - try { - Date date = new Date(); - SimpleDateFormat simpledateformat = new SimpleDateFormat( - "yyyy年MM月dd日"); - String s = simpledateformat.format(date); - return s; - } catch (Exception e) { - return ""; - } - } - - /** - * 返回指定Date值的时间值,格式:yyyy年MM月dd日HH时mm分ss秒 - * - * @param date - * 需要转换的日期 - * @return String值 - */ - public static String dateToString(Date date) { - try { - SimpleDateFormat simpledateformat = new SimpleDateFormat( - "yyyy年MM月dd日HH时mm分ss秒"); - String s = simpledateformat.format(date); - return s; - } catch (Exception e) { - return ""; - } - } - - /** - * - * 返回指定Date值的时间值 - * - * @param date - * 需要转换的日期 - * @param pattern - * 时间格式 - * @return String值 - */ - public static String dateToStringCus(Date date, String pattern) { - try { - SimpleDateFormat simpledateformat = new SimpleDateFormat(pattern); - String s = simpledateformat.format(date); - return s; - } catch (Exception e) { - return ""; - } - } - - /** - * 返回指定Date值的时间值,格式:yyyy-MM-dd HH:mm - * - * @param date - * 需要转换的日期 - * @return String值 - */ - public static String dateToCode(Date date) { - try { - SimpleDateFormat simpledateformat = new SimpleDateFormat( - "yyyy-MM-dd HH:mm"); - String s = simpledateformat.format(date); - return s; - } catch (Exception e) { - return ""; - } - } - - /** - * 返回指定Date值的时间值,格式:yyyy-MM-dd HH:mm:ss - * - * @param date - * 需要转换的日期 - * @return String值 - */ - public static String dateToAllCode(Date date) { - try { - SimpleDateFormat simpledateformat = new SimpleDateFormat( - "yyyy-MM-dd HH:mm:ss"); - String s = simpledateformat.format(date); - return s; - } catch (Exception e) { - return ""; - } - } - - /** - * 返回指定Date值的时间值,格式:HH:mm:ss - * - * @param date - * 需要转换的日期 - * @return String值 - */ - public static String dateToTimeCode(Date date) { - try { - SimpleDateFormat simpledateformat = new SimpleDateFormat("HH:mm:ss"); - String s = simpledateformat.format(date); - return s; - } catch (Exception e) { - return ""; - } - } - - /** - * 返回指定Date值的时间值,格式:yyyy年MM月dd日 - * - * @param date - * 需要转换的日期 - * @return String值 - */ - public static String dateToShortString(Date date) { - try { - SimpleDateFormat simpledateformat = new SimpleDateFormat( - "yyyy年MM月dd日"); - String s = simpledateformat.format(date); - return s; - } catch (Exception e) { - return ""; - } - } - - /** - * 返回指定Date值的时间值,格式:yyyy-MM-dd - * - * @param date - * 需要转换的日期 - * @return String值 - */ - public static String dateToShortCode(Date date) { - try { - SimpleDateFormat simpledateformat = new SimpleDateFormat( - "yyyy-MM-dd"); - String s = simpledateformat.format(date); - return s; - } catch (Exception e) { - return ""; - } - } - - /** - * 按“-”分割指定Date值 - * - * @param date - * 需要转换的日期 - * @return String[]值 - */ - public static String[] SplitDate(Date date) { - String s = dateToShortCode(date); - String[] temp = new String[3]; - StringTokenizer st = new StringTokenizer(s, "-"); - int i = 0; - while (st.hasMoreTokens()) { - temp[i] = st.nextToken(); - i++; - } - return temp; - } - - /** - * 转换字符串为Date值,字符串格式:yyyy-MM-dd HH:mm:ss - * - * @param s - * 需要转换的字符串,字符串格式:yyyy-MM-dd HH:mm:ss - * @return Date值 - */ - public static Date strToDate(String s) { - try { - SimpleDateFormat simpledateformat = new SimpleDateFormat( - "yyyy-MM-dd HH:mm:ss"); - Date date = simpledateformat.parse(s); - return date; - } catch (Exception e) { - return null; - } - } - - /** - * 转换字符串为Date值,字符串格式:HH:mm:ss - * - * @param s - * 需要转换的字符串,字符串格式:HH:mm:ss - * @return Date值 - */ - public static Date timeCodeToDate(String s) { - try { - SimpleDateFormat simpledateformat = new SimpleDateFormat("HH:mm:ss"); - Date date = simpledateformat.parse(s); - return date; - } catch (Exception e) { - return null; - } - } - - /** - * 转换字符串为Date值,字符串格式:yyyy-MM-dd - * - * @param s - * 需要转换的字符串,字符串格式:yyyy-MM-dd - * @return Date值 - */ - public static Date strToShortday(String s) { - try { - SimpleDateFormat simpledateformat = new SimpleDateFormat( - "yyyy-MM-dd"); - ParsePosition parseposition = new ParsePosition(0); - Date date = simpledateformat.parse(s, parseposition); - return date; - } catch (Exception e) { - return null; - } - } - - /** - * 转换字符串为Date值,字符串格式:yyyyMMdd - * - * @param s - * 需要转换的字符串,字符串格式:yyyyMMdd - * @return Date值 - */ - public static Date strToShortNoday(String s) { - try { - SimpleDateFormat simpledateformat = new SimpleDateFormat( - "yyyyMMdd"); - ParsePosition parseposition = new ParsePosition(0); - Date date = simpledateformat.parse(s, parseposition); - return date; - } catch (Exception e) { - return null; - } - } - /** - * 返回当前Date对象 - * - * @return Date值 - */ - public static Date getNow() { - Date date = new Date(); - return date; - } - - /** - * 转换日期字符串为long值,字符串格式:yyyy-MM-dd - * - * @param s - * 需要转换的字符串,字符串格式:yyyy-MM-dd - * @return long值 - */ - public static long getS(String s) { - try { - SimpleDateFormat simpledateformat = new SimpleDateFormat( - "yyyy-MM-dd"); - ParsePosition parseposition = new ParsePosition(0); - Date date = simpledateformat.parse(s, parseposition); - return date.getTime(); - } catch (Exception e) { - return -1; - } - } - - /** - * 转换日期字符串为long值,字符串格式:yyyy-MM-dd HH:mm:ss - * - * @param s - * 需要转换的字符串,字符串格式:yyyy-MM-dd HH:mm:ss - * @return long值 - */ - public static long getStrToLong(String s) { - try { - SimpleDateFormat simpledateformat = new SimpleDateFormat( - "yyyy-MM-dd HH:mm:ss"); - ParsePosition parseposition = new ParsePosition(0); - Date date = simpledateformat.parse(s, parseposition); - return date.getTime(); - } catch (Exception e) { - return -1; - } - } - - /** - * 得到long类型日期值 - * - * @return long值 - */ - public static long getLongTime() { - return (new Date()).getTime(); - } - - /** - * 得到某个时间距离现在的分钟数 - * - * @param l - * 日期long类型值 - * @return long值 - */ - public static long getOffMinutes(long l) { - return getOffMinutes(l, System.currentTimeMillis()); - } - - /** - * 得到两个时间之间的时间差,单位为:分钟 - * - * @param l - * 第一个日期long类型值 - * @param l1 - * 第二个日期long类型值 - * @return long值 - */ - public static long getOffMinutes(long l, long l1) { - return (long) ((l1 - l) / 60000L); - } - - /** - * 根据指定时间格式字符串(格式:yyyy-MM-dd),取得该时间对应的周一至周日Date对象 Date first = - * DateUtil.getMonday(today,Calendar.SUNDAY); Date last = - * DateUtil.getMonday(today,Calendar.SATURDAY); - * - * @param date - * String类型,格式:yyyy-MM-dd - * @param weekDay - * 一周的第几天,第一天为Calendar.SUNDAY,第七天为Calendar.SATURDAY - * @return Date值 - */ - public static Date getMonday(String date, int weekDay) { - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - Date d = null; - try { - d = format.parse(date); - } catch (Exception e) { - e.printStackTrace(); - } - Calendar cal = Calendar.getInstance(); - cal.setTime(d); - // ����DAY_OF_WEEK��˵�� - // Field number for get and set indicating - // the day of the week. This field takes values - // SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, - // and SATURDAY - cal.set(Calendar.DAY_OF_WEEK, weekDay); - cal.add(Calendar.DATE, 1); - return cal.getTime(); - } - - /** - * 根据指定时间格式字符串(格式:yyyy-MM),取得该时间对应月份第一天Date对象 - * - * @param date - * String类型,格式:yyyy-MM - * @return Date值 - */ - public static Date getMonthFirstDay(String date) { - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); - Date d = null; - try { - d = format.parse(date); - } catch (Exception e) { - e.printStackTrace(); - } - Calendar cal = Calendar.getInstance(); - cal.setTime(d); - cal.add(Calendar.DAY_OF_MONTH, 0); - return cal.getTime(); - } - - /** - * 根据指定时间格式字符串(格式:yyyy-MM),取得该时间对应月份最后一天Date对象 - * - * @param date - * String类型,格式:yyyy-MM - * @return Date值 - */ - public static Date getMonthLastDay(String date) { - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); - Date d = null; - try { - d = format.parse(date); - } catch (Exception e) { - e.printStackTrace(); - } - Calendar cal = Calendar.getInstance(); - cal.setTime(d); - cal.add(Calendar.MONTH, 1); - cal.add(Calendar.DATE, -1); - return cal.getTime(); - } - - /** - * 根据指定时间格式字符串(格式:yyyy),取得该时间对应年份第一天Date对象 - * - * @param date - * String类型,格式:yyyy - * @return Date值 - */ - public static Date getYearFirstDay(String date) { - SimpleDateFormat format = new SimpleDateFormat("yyyy"); - Date d = null; - try { - d = format.parse(date); - } catch (Exception e) { - e.printStackTrace(); - } - Calendar cal = Calendar.getInstance(); - cal.setTime(d); - cal.add(Calendar.DAY_OF_YEAR, 0); - return cal.getTime(); - } - - /** - * 根据指定时间格式字符串(格式:yyyy),取得该时间对应年份最后一天Date对象 - * - * @param date - * String类型,格式:yyyy - * @return Date值 - */ - public static Date getYearLastDay(String date) { - SimpleDateFormat format = new SimpleDateFormat("yyyy"); - Date d = null; - try { - d = format.parse(date); - } catch (Exception e) { - e.printStackTrace(); - } - Calendar cal = Calendar.getInstance(); - cal.setTime(d); - cal.add(Calendar.YEAR, 1); - cal.add(Calendar.DATE, -1); - return cal.getTime(); - } - /** - * 指定时间对象,根据日历的规则,为给定的日历字段添加或减去指定的时间量,返回时间对象 - * @param date Date类型 - * @param field int 日历字段,如:Calendar.DAY_OF_MONTH - * @param amount int 为字段添加的日期或时间量。 - * @return Date值 - */ - public static Date getDate(Date date,int field, int amount) { - Calendar cal = Calendar.getInstance(); - cal.setTime(date); - cal.add(field, amount); - return cal.getTime(); - } - /** - * 转换long值为字符串类型日期格式 - * - * @param l - * long值 - * @return String值 - */ - public static String LongToDateString(long l) { - DateFormat mediumDateFormat = null; - Date sDate = null; - try { - mediumDateFormat = DateFormat.getDateTimeInstance(); - String date = String.valueOf(l); - sDate = new Date(Long.parseLong(date)); - } catch (Exception ex) { - ex.printStackTrace(); - return null; - } - return mediumDateFormat.format(sDate); - } - - /** - * 将String类型数据转换为Date类型 - * @param dt String类型日期数值 - * @return Date类型的日期 - */ - public static Date convertStringToDate(String dt) { - if (dt == null || dt.equals("")) { - return null; - } - return convertStringToDate("yyyy-MM-dd", dt); - } - - public static Date convertStringToDate(String style, String dt) { - if (dt == null || dt.equals("")) { - return null; - } - if (style == null || style.equals("")) { - style = "yyyy-MM-dd"; - } - SimpleDateFormat dateformat = new SimpleDateFormat(style); - dateformat.setLenient(false); - try { - return dateformat.parse(dt); - } catch (ParseException ex) { - return null; - } - - } - - /** - * 返回指定Date值的时间值,格式:yyyy-MM-ddTHH:mm:ss - * - * @param date - * 需要转换的日期 - * @return String值 - */ - public static String dateToBOMCStrDate(Date date) { - try { - SimpleDateFormat simpledateformat = new SimpleDateFormat( - "yyyy-MM-dd HH:mm:ss"); - String s = simpledateformat.format(date); - s=StrToBOMCStrDate(s); - return s; - } catch (Exception e) { - return ""; - } - } - /** - * 返回指定Date值的时间值,格式:yyyy-MM-ddTHH:mm:ss - * - * @param date 格式:yyyy-MM-dd HH:mm:ss - * 需要转换的日期 - * @return String值 - */ - public static String StrToBOMCStrDate(String date) { - return date.replace(" ", "T"); - } + public DateUtil() { + + } + + /** + * 得到当前时间值,格式:yyyy年MM月dd日HH时mm分ss秒 + * + * @return String值 + */ + public static String getNowTime() { + try { + Date date = new Date(); + SimpleDateFormat simpledateformat = new SimpleDateFormat( + "yyyy年MM月dd日HH时mm分ss秒sss"); + return simpledateformat.format(date); + } catch (Exception e) { + return ""; + } + } + + /** + * 得到当前日期值,格式:yyyy年MM月dd日 + * + * @return String值 + */ + public static String getNowDate() { + try { + Date date = new Date(); + SimpleDateFormat simpledateformat = new SimpleDateFormat( + "yyyy年MM月dd日"); + return simpledateformat.format(date); + } catch (Exception e) { + return ""; + } + } + + /** + * 返回指定Date值的时间值,格式:yyyy年MM月dd日HH时mm分ss秒 + * + * @param date 需要转换的日期 + * @return String值 + */ + public static String dateToString(Date date) { + try { + SimpleDateFormat simpledateformat = new SimpleDateFormat( + "yyyy年MM月dd日HH时mm分ss秒"); + return simpledateformat.format(date); + } catch (Exception e) { + return ""; + } + } + + /** + * 返回指定Date值的时间值 + * + * @param date 需要转换的日期 + * @param pattern 时间格式 + * @return String值 + */ + public static String dateToStringCus(Date date, String pattern) { + try { + SimpleDateFormat simpledateformat = new SimpleDateFormat(pattern); + return simpledateformat.format(date); + } catch (Exception e) { + return ""; + } + } + + /** + * 返回指定Date值的时间值,格式:yyyy-MM-dd HH:mm + * + * @param date 需要转换的日期 + * @return String值 + */ + public static String dateToCode(Date date) { + try { + SimpleDateFormat simpledateformat = new SimpleDateFormat( + "yyyy-MM-dd HH:mm"); + return simpledateformat.format(date); + } catch (Exception e) { + return ""; + } + } + + /** + * 返回指定Date值的时间值,格式:yyyy-MM-dd HH:mm:ss + * + * @param date 需要转换的日期 + * @return String值 + */ + public static String dateToAllCode(Date date) { + try { + SimpleDateFormat simpledateformat = new SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss"); + return simpledateformat.format(date); + } catch (Exception e) { + return ""; + } + } + + /** + * 返回指定Date值的时间值,格式:HH:mm:ss + * + * @param date 需要转换的日期 + * @return String值 + */ + public static String dateToTimeCode(Date date) { + try { + SimpleDateFormat simpledateformat = new SimpleDateFormat("HH:mm:ss"); + return simpledateformat.format(date); + } catch (Exception e) { + return ""; + } + } + + /** + * 返回指定Date值的时间值,格式:yyyy年MM月dd日 + * + * @param date 需要转换的日期 + * @return String值 + */ + public static String dateToShortString(Date date) { + try { + SimpleDateFormat simpledateformat = new SimpleDateFormat( + "yyyy年MM月dd日"); + return simpledateformat.format(date); + } catch (Exception e) { + return ""; + } + } + + /** + * 返回指定Date值的时间值,格式:yyyy-MM-dd + * + * @param date 需要转换的日期 + * @return String值 + */ + public static String dateToShortCode(Date date) { + try { + SimpleDateFormat simpledateformat = new SimpleDateFormat( + "yyyy-MM-dd"); + return simpledateformat.format(date); + } catch (Exception e) { + return ""; + } + } + + /** + * 按“-”分割指定Date值 + * + * @param date 需要转换的日期 + * @return String[]值 + */ + public static String[] SplitDate(Date date) { + String s = dateToShortCode(date); + String[] temp = new String[3]; + StringTokenizer st = new StringTokenizer(s, "-"); + int i = 0; + while (st.hasMoreTokens()) { + temp[i] = st.nextToken(); + i++; + } + return temp; + } + + /** + * 转换字符串为Date值,字符串格式:yyyy-MM-dd HH:mm:ss + * + * @param s 需要转换的字符串,字符串格式:yyyy-MM-dd HH:mm:ss + * @return Date值 + */ + public static Date strToDate(String s) { + try { + SimpleDateFormat simpledateformat = new SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss"); + return simpledateformat.parse(s); + } catch (Exception e) { + return null; + } + } + + /** + * 转换字符串为Date值,字符串格式:HH:mm:ss + * + * @param s 需要转换的字符串,字符串格式:HH:mm:ss + * @return Date值 + */ + public static Date timeCodeToDate(String s) { + try { + SimpleDateFormat simpledateformat = new SimpleDateFormat("HH:mm:ss"); + return simpledateformat.parse(s); + } catch (Exception e) { + return null; + } + } + + /** + * 转换字符串为Date值,字符串格式:yyyy-MM-dd + * + * @param s 需要转换的字符串,字符串格式:yyyy-MM-dd + * @return Date值 + */ + public static Date strToShortday(String s) { + try { + SimpleDateFormat simpledateformat = new SimpleDateFormat( + "yyyy-MM-dd"); + ParsePosition parseposition = new ParsePosition(0); + return simpledateformat.parse(s, parseposition); + } catch (Exception e) { + return null; + } + } + + /** + * 转换字符串为Date值,字符串格式:yyyyMMdd + * + * @param s 需要转换的字符串,字符串格式:yyyyMMdd + * @return Date值 + */ + public static Date strToShortNoday(String s) { + try { + SimpleDateFormat simpledateformat = new SimpleDateFormat( + "yyyyMMdd"); + ParsePosition parseposition = new ParsePosition(0); + return simpledateformat.parse(s, parseposition); + } catch (Exception e) { + return null; + } + } + + /** + * 返回当前Date对象 + * + * @return Date值 + */ + public static Date getNow() { + return new Date(); + } + + /** + * 转换日期字符串为long值,字符串格式:yyyy-MM-dd + * + * @param s 需要转换的字符串,字符串格式:yyyy-MM-dd + * @return long值 + */ + public static long getS(String s) { + try { + SimpleDateFormat simpledateformat = new SimpleDateFormat( + "yyyy-MM-dd"); + ParsePosition parseposition = new ParsePosition(0); + Date date = simpledateformat.parse(s, parseposition); + return date.getTime(); + } catch (Exception e) { + return -1; + } + } + + /** + * 转换日期字符串为long值,字符串格式:yyyy-MM-dd HH:mm:ss + * + * @param s 需要转换的字符串,字符串格式:yyyy-MM-dd HH:mm:ss + * @return long值 + */ + public static long getStrToLong(String s) { + try { + SimpleDateFormat simpledateformat = new SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss"); + ParsePosition parseposition = new ParsePosition(0); + Date date = simpledateformat.parse(s, parseposition); + return date.getTime(); + } catch (Exception e) { + return -1; + } + } + + /** + * 得到long类型日期值 + * + * @return long值 + */ + public static long getLongTime() { + return (new Date()).getTime(); + } + + /** + * 得到某个时间距离现在的分钟数 + * + * @param l 日期long类型值 + * @return long值 + */ + public static long getOffMinutes(long l) { + return getOffMinutes(l, System.currentTimeMillis()); + } + + /** + * 得到两个时间之间的时间差,单位为:分钟 + * + * @param l 第一个日期long类型值 + * @param l1 第二个日期long类型值 + * @return long值 + */ + public static long getOffMinutes(long l, long l1) { + return (l1 - l) / 60000L; + } + + /** + * 根据指定时间格式字符串(格式:yyyy-MM-dd),取得该时间对应的周一至周日Date对象 Date first = + * DateUtil.getMonday(today,Calendar.SUNDAY); Date last = + * DateUtil.getMonday(today,Calendar.SATURDAY); + * + * @param date String类型,格式:yyyy-MM-dd + * @param weekDay 一周的第几天,第一天为Calendar.SUNDAY,第七天为Calendar.SATURDAY + * @return Date值 + */ + public static Date getMonday(String date, int weekDay) { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + Date d = null; + try { + d = format.parse(date); + } catch (Exception e) { + e.printStackTrace(); + } + Calendar cal = Calendar.getInstance(); + if (d != null) { + cal.setTime(d); + } + // DAY_OF_WEEK + // Field number for get and set indicating + // the day of the week. This field takes values + // SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, + // and SATURDAY + cal.set(Calendar.DAY_OF_WEEK, weekDay); + cal.add(Calendar.DATE, 1); + return cal.getTime(); + } + + /** + * 根据指定时间格式字符串(格式:yyyy-MM),取得该时间对应月份第一天Date对象 + * + * @param date String类型,格式:yyyy-MM + * @return Date值 + */ + public static Date getMonthFirstDay(String date) { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); + Date d = null; + try { + d = format.parse(date); + } catch (Exception e) { + e.printStackTrace(); + } + Calendar cal = Calendar.getInstance(); + if (d != null) { + cal.setTime(d); + } + cal.add(Calendar.DAY_OF_MONTH, 0); + return cal.getTime(); + } + + /** + * 根据指定时间格式字符串(格式:yyyy-MM),取得该时间对应月份最后一天Date对象 + * + * @param date String类型,格式:yyyy-MM + * @return Date值 + */ + public static Date getMonthLastDay(String date) { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); + Date d = null; + try { + d = format.parse(date); + } catch (Exception e) { + e.printStackTrace(); + } + Calendar cal = Calendar.getInstance(); + if (d != null) { + cal.setTime(d); + } + cal.add(Calendar.MONTH, 1); + cal.add(Calendar.DATE, -1); + return cal.getTime(); + } + + /** + * 根据指定时间格式字符串(格式:yyyy),取得该时间对应年份第一天Date对象 + * + * @param date String类型,格式:yyyy + * @return Date值 + */ + public static Date getYearFirstDay(String date) { + SimpleDateFormat format = new SimpleDateFormat("yyyy"); + Date d = null; + try { + d = format.parse(date); + } catch (Exception e) { + e.printStackTrace(); + } + Calendar cal = Calendar.getInstance(); + if (d != null) { + cal.setTime(d); + } + cal.add(Calendar.DAY_OF_YEAR, 0); + return cal.getTime(); + } + + /** + * 根据指定时间格式字符串(格式:yyyy),取得该时间对应年份最后一天Date对象 + * + * @param date String类型,格式:yyyy + * @return Date值 + */ + public static Date getYearLastDay(String date) { + SimpleDateFormat format = new SimpleDateFormat("yyyy"); + Date d = null; + try { + d = format.parse(date); + } catch (Exception e) { + e.printStackTrace(); + } + Calendar cal = Calendar.getInstance(); + if (d != null) { + cal.setTime(d); + } + cal.add(Calendar.YEAR, 1); + cal.add(Calendar.DATE, -1); + return cal.getTime(); + } + + /** + * 指定时间对象,根据日历的规则,为给定的日历字段添加或减去指定的时间量,返回时间对象 + * + * @param date Date类型 + * @param field int 日历字段,如:Calendar.DAY_OF_MONTH + * @param amount int 为字段添加的日期或时间量。 + * @return Date值 + */ + public static Date getDate(Date date, int field, int amount) { + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + cal.add(field, amount); + return cal.getTime(); + } + + /** + * 转换long值为字符串类型日期格式 + * + * @param l long值 + * @return String值 + */ + public static String LongToDateString(long l) { + DateFormat mediumDateFormat; + Date sDate; + try { + mediumDateFormat = DateFormat.getDateTimeInstance(); + String date = String.valueOf(l); + sDate = new Date(Long.parseLong(date)); + } catch (Exception ex) { + ex.printStackTrace(); + return null; + } + return mediumDateFormat.format(sDate); + } + + /** + * 将String类型数据转换为Date类型 + * + * @param dt String类型日期数值 + * @return Date类型的日期 + */ + public static Date convertStringToDate(String dt) { + if (dt == null || dt.equals("")) { + return null; + } + return convertStringToDate("yyyy-MM-dd", dt); + } + + public static Date convertStringToDate(String style, String dt) { + if (dt == null || dt.equals("")) { + return null; + } + if (style == null || style.equals("")) { + style = "yyyy-MM-dd"; + } + SimpleDateFormat dateFormat = new SimpleDateFormat(style); + dateFormat.setLenient(false); + try { + return dateFormat.parse(dt); + } catch (ParseException ex) { + return null; + } + + } + + /** + * 返回指定Date值的时间值,格式:yyyy-MM-ddTHH:mm:ss + * + * @param date 需要转换的日期 + * @return String值 + */ + public static String dateToBOMCStrDate(Date date) { + try { + SimpleDateFormat simpledateformat = new SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss"); + String s = simpledateformat.format(date); + s = StrToBOMCStrDate(s); + return s; + } catch (Exception e) { + return ""; + } + } + + /** + * 返回指定Date值的时间值,格式:yyyy-MM-ddTHH:mm:ss + * + * @param date 格式:yyyy-MM-dd HH:mm:ss + * 需要转换的日期 + * @return String值 + */ + public static String StrToBOMCStrDate(String date) { + return date.replace(" ", "T"); + } } \ No newline at end of file diff --git a/src/com/itsolife/htmltomarkdown/FilesUtil.java b/src/com/itsolife/htmltomarkdown/FilesUtil.java index d237862..927e524 100755 --- a/src/com/itsolife/htmltomarkdown/FilesUtil.java +++ b/src/com/itsolife/htmltomarkdown/FilesUtil.java @@ -1,913 +1,846 @@ package com.itsolife.htmltomarkdown; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.PrintWriter; +import java.io.*; import java.net.URLDecoder; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.StringTokenizer; +import java.util.*; /** * File文件工具 - * @author robin * + * @author robin */ public class FilesUtil { - /** - * 复制文件 - * @param fileName 文件名 - * @param fountainheadDirs 源文件路径 - * @param armDirs 目的文件路径 - * @return boolean - */ - public boolean copyTheFile(String fileName, String fountainheadDirs, - String armDirs) { - boolean isSucceed = false; - - InputStream inStream = null; - FileOutputStream fs = null; - try { - int byteread = 0; - File oldFile = new File(fountainheadDirs + "/" + fileName); - File armFileDirs = new File(armDirs); - if (!armFileDirs.exists()) { - armFileDirs.mkdirs(); - } - if (!oldFile.exists()) { - throw new Exception("要复制的文件不存在"); - } - if (oldFile.exists()) { // 文件存在时 - inStream = new FileInputStream(fountainheadDirs + "/" - + fileName); // 读入原文件 - fs = new FileOutputStream(armDirs + "/" + fileName); - byte[] buffer = new byte[1444]; - while ((byteread = inStream.read(buffer)) != -1) { - fs.write(buffer, 0, byteread); - } - } - isSucceed = true; - } catch (Exception e) { - System.out.println("读取的文件不存在或复制单个文件操作出错"); - e.printStackTrace(); - } finally { - try { - if (fs != null) - fs.close(); - if (inStream != null) - inStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - return isSucceed; - } - - /** - * 剪切文件 - * @param fileName 文件名称 - * @param fountainheadDirs 源文件路径 - * @param armDirs 目的文件路径 - * @return boolean - */ - public boolean cutTheFile(String fileName, String fountainheadDirs, - String armDirs) { - boolean isSucceed = false; - - InputStream inStream = null; - FileOutputStream fs = null; - try { - int byteread = 0; - File oldFile = new File(fountainheadDirs + "/" + fileName); - File armFileDirs = new File(armDirs); - if (!armFileDirs.exists()) { - armFileDirs.mkdirs(); - } - if (!oldFile.exists()) { - throw new Exception("要复制的文件不存在"); - } - if (oldFile.exists()) { // 文件存在时 - inStream = new FileInputStream(fountainheadDirs + "/" - + fileName); // 读入原文件 - fs = new FileOutputStream(armDirs + "/" + fileName); - byte[] buffer = new byte[1444]; - while ((byteread = inStream.read(buffer)) != -1) { - fs.write(buffer, 0, byteread); - } - } - // System.out.println(deleteFile.delete()); - isSucceed = true; - } catch (Exception e) { - System.out.println("读取的文件不存在或复制单个文件操作出错"); - e.printStackTrace(); - } finally { - try { - if (fs != null) - fs.close(); - if (inStream != null) - inStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - File deleteFile = new File(fountainheadDirs + "/" + fileName); - if(isSucceed) - isSucceed = deleteFile.delete(); - } - return isSucceed; - } - /** - * 读取文本文件内容,返回文本文件的内容字符串,以"\n"分割 - * - * @param filePathAndName - * String 带有完整绝对路径的文件名 - * @return String 返回文本文件的内容 - */ - public static String readAll(String filePathAndName) { - return readAll(filePathAndName, null); - } - - /** - * 读取文本文件内容,返回文本文件的内容字符串,以"\n"分割 - * - * @param filePathAndName - * String 带有完整绝对路径的文件名 - * @param encoding - * String 文本文件打开的编码方式 - * @return String 返回文本文件的内容 - */ - public static String readAll(String filePathAndName, String encoding) { - StringBuffer str = new StringBuffer(""); - String st = ""; - FileInputStream fs =null; - try { - fs = new FileInputStream(URLDecoder.decode(filePathAndName)); - InputStreamReader isr; - if (encoding == null || "".equals(encoding)) { - isr = new InputStreamReader(fs); - } else { - isr = new InputStreamReader(fs, encoding); - } - try { - String data = ""; - BufferedReader br = new BufferedReader(isr); - while ((data = br.readLine()) != null) { - str.append(data + "\n"); - } - } catch (Exception e) { - return ""; - } - st = str.toString(); - } catch (IOException es) { - st = ""; - }finally{ - try { - if(fs!=null)fs.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - return st; - } - - /** - * 读取第n行文本文件内容 - * - * @param filePathAndName - * String 带有完整绝对路径的文件名 - * @param encoding - * String 文本文件打开的编码方式 - * @return String 返回第一行文本文件内容 - */ - public static String readLine(String filePathAndName, long rowIndex, - String encoding) { - String st = ""; - StringBuffer str = new StringBuffer(""); - long i = 0; - FileInputStream fs =null; - try { - fs = new FileInputStream(URLDecoder.decode(filePathAndName)); - InputStreamReader isr; - if (encoding == null || "".equals(encoding)) { - isr = new InputStreamReader(fs); - } else { - isr = new InputStreamReader(fs, encoding); - } - BufferedReader br = new BufferedReader(isr); - try { - String data = ""; - while ((data = br.readLine()) != null) { - if (rowIndex == i) { - str.append(data); - break; - } else { - i++; - } - } - } catch (Exception e) { - return ""; - } - st = str.toString(); - } catch (IOException es) { - return ""; - }finally{ - try { - if(fs!=null)fs.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - return st; - } - - /** - * 读取第一行文本文件内容 - * - * @param filePathAndName - * String 带有完整绝对路径的文件名 - * @return String 返回第一行文本文件内容 - */ - public static String readLine(String filePathAndName) { - return readLine(filePathAndName, 0, null); - } - - /** - * 读取第n行文本文件内容 - * - * @param filePathAndName - * String 带有完整绝对路径的文件名 - * @return String 返回第n行文本文件内容 - */ - public static String readLine(String filePathAndName, long rowIndex) { - return readLine(filePathAndName, rowIndex, null); - } - - /** - * 新建文件 - * - * @param filePathAndName - * String 文件路径及名称 如c:/fqf.txt - * @param fileContent - * String 文件内容 - * @return boolean 是否创建成功 - */ - public static boolean newFile(String filePathAndName, String fileContent) { - return newFile(filePathAndName, fileContent, false); - } - - /** - * 新建文件 - * - * @param filePathAndName - * String 文件路径及名称 如c:/fqf.txt - * @param fileContent - * String 文件内容 - * @param flag - * boolean 是否追加内容,true为追加,false为新建 - * @return boolean 是否创建成功 - */ - public static boolean newFile(String filePathAndName, String fileContent, - boolean flag) { - - String filePath = filePathAndName; - try { - filePath = filePath.toString(); - File myFilePath = new File(filePath); - if (!myFilePath.exists()) { - myFilePath.createNewFile(); - } - FileWriter resultFile = new FileWriter(myFilePath, flag); - PrintWriter myFile = new PrintWriter(resultFile); - String strContent = fileContent; - myFile.println(strContent); - resultFile.close(); - return true; - } catch (Exception e) { - filePath = ""; - System.out.println("新建文件操作出错"); - e.printStackTrace(); - } - return false; - } - - /** - * 有编码方式的文件创建 - * - * @param filePathAndName - * 文本文件完整绝对路径及文件名 - * @param fileContent - * 文本文件内容 - * @param encoding - * 编码方式 例如 GBK 或者 UTF-8 - * @return boolean 是否创建成功 - */ - public static boolean newFile(String filePathAndName, String fileContent, - String encoding) { - - try { - String filePath = filePathAndName; - filePath = filePath.toString(); - File myFilePath = new File(filePath); - if (!myFilePath.exists()) { - myFilePath.createNewFile(); - } - PrintWriter myFile = new PrintWriter(myFilePath, encoding); - String strContent = fileContent; - myFile.println(strContent); - myFile.close(); - return true; - } catch (Exception e) { - System.out.println("创建文件操作出错"); - e.printStackTrace(); - } - return false; - } - - /** - * 删除文件 - * - * @param filePathAndName - * String 文件路径及名称 如c:/fqf.txt - * @param fileContent - * String - */ - public static boolean delFile(String filePathAndName) { - try { - String filePath = filePathAndName; - filePath = filePath.toString(); - java.io.File myDelFile = new java.io.File(filePath); - return myDelFile.delete(); - } catch (Exception e) { - System.out.println("删除文件操作出错"); - e.printStackTrace(); - return false; - } - - } - - /** - * 新建目录 - * - * @param folderPath - * String 如 c:/fqf - * @return String - */ - public static String newFolder(String folderPath) { - String filePath = folderPath; - try { - filePath = filePath.toString(); - java.io.File myFilePath = new java.io.File(filePath); - if (!myFilePath.exists()) { - myFilePath.mkdirs(); - } - } catch (Exception e) { - System.out.println("新建目录操作出错"); - filePath = ""; - e.printStackTrace(); - } - return filePath; - } - - /** - * 删除文件夹 - * - * @param filePathAndName - * String 文件夹路径及名称 如c:/fqf - * @param fileContent - * String - */ - public static void delFolder(String folderPath) { - try { - delAllFile(folderPath); // 删除完里面所有内容 - String filePath = folderPath; - filePath = filePath.toString(); - java.io.File myFilePath = new java.io.File(filePath); - myFilePath.delete(); // 删除空文件夹 - } catch (Exception e) { - System.out.println("删除文件夹操作出错"); - e.printStackTrace(); - - } - - } - - /** - * 删除文件夹里面的所有文件 - * - * @param path - * String 文件夹路径 如 c:/fqf - */ - public static void delAllFile(String path) { - File file = new File(path); - if (!file.exists()) { - return; - } - if (!file.isDirectory()) { - return; - } - if(file.getAbsolutePath().equalsIgnoreCase("/")){ - System.out.println("此目录为根目录,不能删除所有文件!!!"); - System.out.println("请及时修改此目录!!!"); - return; - } - if(file.getAbsolutePath().equalsIgnoreCase("/root")){ - System.out.println("此目录为根目录,不能删除所有文件!!!"); - System.out.println("请及时修改此目录!!!"); - return; - } - if(file.getAbsolutePath().equalsIgnoreCase("/usr")||file.getAbsolutePath().equalsIgnoreCase("/opt") - ||file.getAbsolutePath().equalsIgnoreCase("/bin")||file.getAbsolutePath().equalsIgnoreCase("/sbin") - ||file.getAbsolutePath().equalsIgnoreCase("/etc")||file.getAbsolutePath().equalsIgnoreCase("/selinux") - ||file.getAbsolutePath().equalsIgnoreCase("/sys")||file.getAbsolutePath().equalsIgnoreCase("/var") - ||file.getAbsolutePath().equalsIgnoreCase("/home")||file.getAbsolutePath().equalsIgnoreCase("/net")){ - System.out.println("此目录为根目录,不能删除所有文件!!!"); - System.out.println("请及时修改此目录!!!"); - return; - } - if(file.getAbsolutePath().equalsIgnoreCase("C://")||file.getAbsolutePath().equalsIgnoreCase("C:\\\\")){ - System.out.println("此目录为C盘根目录,不能删除所有文件!!!"); - System.out.println("请及时修改此目录!!!"); - return; - } - String[] tempList = file.list(); - File temp = null; - if(tempList==null){ - return; - } - for (int i = 0; i < tempList.length; i++) { - if (path.endsWith(File.separator)) { - temp = new File(path + tempList[i]); - } else { - temp = new File(path + File.separator + tempList[i]); - } - if (temp.isFile()) { - temp.delete(); - } - if (temp.isDirectory()) { - delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件 - delFolder(path + "/" + tempList[i]);// 再删除空文件夹 - } - } - } - - /** - * 复制单个文件 - * - * @param oldPath - * String 原文件路径 如:c:/fqf.txt - * @param newPath - * String 复制后路径 如:f:/fqf.txt - */ - public static void copyFile(String oldPath, String newPath) { - InputStream inStream = null; - FileOutputStream fs = null; - try { - int byteread = 0; - File oldfile = new File(oldPath); - - if (oldfile.exists()) { // 文件存在时 - inStream = new FileInputStream(oldPath); // 读入原文件 - fs = new FileOutputStream(newPath); - byte[] buffer = new byte[1444]; - while ((byteread = inStream.read(buffer)) != -1) { - fs.write(buffer, 0, byteread); - } - - } - } catch (Exception e) { - System.out.println("复制单个文件操作出错"); - e.printStackTrace(); - } finally { - try { - if (fs != null) - fs.close(); - if (inStream != null) - inStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - - } - - } - - /** - * 复制整个文件夹内容 - * - * @param oldPath - * String 原文件路径 如:c:/fqf - * @param newPath - * String 复制后路径 如:f:/fqf/ff - */ - public static void copyFolder(String oldPath, String newPath) { - - try { - (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹 - File a = new File(oldPath); - String[] file = a.list(); - File temp = null; - for (int i = 0; i < file.length; i++) { - if (oldPath.endsWith(File.separator)) { - temp = new File(oldPath + file[i]); - } else { - temp = new File(oldPath + File.separator + file[i]); - } - - if (temp.isFile()) { - FileInputStream input = new FileInputStream(temp); - FileOutputStream output = new FileOutputStream(newPath - + "/" + (temp.getName()).toString()); - byte[] b = new byte[1024 * 5]; - int len; - while ((len = input.read(b)) != -1) { - output.write(b, 0, len); - } - output.flush(); - output.close(); - input.close(); - } - if (temp.isDirectory()) {// 如果是子文件夹 - copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]); - } - } - } catch (Exception e) { - System.out.println("复制整个文件夹内容操作出错"); - e.printStackTrace(); - } - } - - /** - * 移动文件到指定目录 - * - * @param oldPath - * String 如:c:/fqf.txt - * @param newPath - * String 如:d:/fqf.txt - */ - public static void moveFile(String oldPath, String newPath) { - copyFile(oldPath, newPath); - delFile(oldPath); - } - - /** - * 移动文件到指定目录 - * - * @param oldPath - * String 如:c:/fqf.txt - * @param newPath - * String 如:d:/fqf.txt - */ - public static void moveFolder(String oldPath, String newPath) { - copyFolder(oldPath, newPath); - delFolder(oldPath); - } - - /** - * 多级目录创建 - * - * @param folderPath - * 准备要在本级目录下创建新目录的目录路径 例如 c:myf - * @param paths - * 无限级目录参数,各级目录以单数线区分 例如 a|b|c - * @return String 返回创建文件后的路径 例如 c:myfac - */ - public static String createFolders(String folderPath, String paths) { - String txts = folderPath; - try { - String txt; - txts = folderPath; - StringTokenizer st = new StringTokenizer(paths, "|"); - for (int i = 0; st.hasMoreTokens(); i++) { - txt = st.nextToken(); - if (txts.lastIndexOf("/") != -1) { - txts = newFolder(txts + txt); - } else { - txts = newFolder(txts + txt + "/"); - } - } - } catch (Exception e) { - System.out.println("创建多级目录操作出错"); - txts = ""; - e.printStackTrace(); - } - return txts; - } - - /** - * 多级目录创建 - * - * @param folderPath - * 建立多级目录,一次创建 - * @return String 返回创建文件后的路径 例如 c:\aa\bbb\cc - */ - public static String createFolders(String folderPath) { - String txts = folderPath; - try { - File file = new File(txts); - if (!file.exists()) { - file.mkdirs(); - } - } catch (Exception e) { - System.out.println("创建多级目录操作出错"); - txts = ""; - e.printStackTrace(); - } - return txts; - } - - /** - * 判断指定的文件是否存在。 - * - * @param fileName - * 要判断的文件的文件名 - * @return 存在时返回true,否则返回false。 - */ - public static boolean isFileExist(String fileName) { - return new File(fileName).isFile(); - } - - /** - * 得到文件夹里面的所有文件 - * - * @param path - * String 文件夹路径 如 c:/fqf - * @return List - */ - public static List GetAllFile(String path) { - List arrList = new ArrayList(); - File file = new File(path); - if (!file.exists()) { - return arrList; - } - if (!file.isDirectory()) { - return arrList; - } - String[] tempList = file.list(); - File temp = null; - for (int i = 0; i < tempList.length; i++) { - if (path.endsWith(File.separator)) { - temp = new File(path + tempList[i]); - } else { - temp = new File(path + File.separator + tempList[i]); - } - if (temp.isFile()) { - arrList.add(temp); - } - if(temp.isDirectory()){ - List s=GetAllFile(temp.getAbsolutePath()); - arrList.addAll(s); - } - } - return arrList; - } - /** - * 得到文件夹里面的所有文件 - * - * @param path - * String 文件夹路径 如 c:/fqf - * @return List - */ - public static List GetAllFile(String path,String suffix) { - List arrList = new ArrayList(); - File file = new File(path); - if (!file.exists()) { - return arrList; - } - if (!file.isDirectory()) { - return arrList; - } - String[] tempList = file.list(); - File temp = null; - for (int i = 0; i < tempList.length; i++) { - if (path.endsWith(File.separator)) { - temp = new File(path + tempList[i]); - } else { - temp = new File(path + File.separator + tempList[i]); - } - if (temp.isFile()) { - if(suffix==null||"".equals(suffix)) - arrList.add(temp); - else { - String filePath = temp.getAbsolutePath(); - if (!suffix.equals("")) { - int begIndex = filePath.lastIndexOf("."); // 最后一个.(即后缀名前面的.)的索引 - String tempsuffix = ""; - - if (begIndex != -1) { - tempsuffix = filePath.substring(begIndex + 1, filePath.length()); - if (tempsuffix.equals(suffix)) { - arrList.add(temp); - } - } - } - } - } - if(temp.isDirectory()){ - List s=GetAllFile(temp.getAbsolutePath(),suffix); - arrList.addAll(s); - } - } - return arrList; - } - /** - * @param path - * 文件路径 - * @param suffix - * 后缀名, 为空则表示所有文件 - * @param isdepth - * 是否遍历子目录 - * @return list - */ - public static List getListFilesName(String path, String suffix, boolean isdepth) { - List lstFileNames = new ArrayList(); - File file = new File(path); - return listFileName(lstFileNames, file, suffix, isdepth); - } - - private static List listFileName(List lstFileNames, File f, String suffix, boolean isdepth) { - // 若是目录, 采用递归的方法遍历子目录 - if (f.isDirectory()) { - File[] t = f.listFiles(); - - for (int i = 0; i < t.length; i++) { - if (isdepth || t[i].isFile()) { - listFileName(lstFileNames, t[i], suffix, isdepth); - } - } - } else { - String filePath = f.getAbsolutePath(); - if (!suffix.equals("")) { - int begIndex = filePath.lastIndexOf("."); // 最后一个.(即后缀名前面的.)的索引 - String tempsuffix = ""; - - if (begIndex != -1) { - tempsuffix = filePath.substring(begIndex + 1, filePath.length()); - if (tempsuffix.equals(suffix)) { - lstFileNames.add(filePath); - } - } - } else { - lstFileNames.add(filePath); - } - } - return lstFileNames; - } - /** - * 得到文件夹里面的所有文件名 - * - * @param path - * String 文件夹路径 如 c:/fqf - * @return List - */ - public static List GetAllFileName(String path) { - List arrList = new ArrayList (); - File file = new File(path); - if (!file.exists()) { - return arrList; - } - if (!file.isDirectory()) { - return arrList; - } - String[] tempList = file.list(); - File temp = null; - for (int i = 0; i < tempList.length; i++) { - if (path.endsWith(File.separator)) { - temp = new File(path + tempList[i]); - } else { - temp = new File(path + File.separator + tempList[i]); - } - if (temp.isFile()) { - arrList.add(temp.getName()); - } - } - return arrList; - } - /** - * 得到文件夹里面的所有文件名 - * - * @param path - * String 文件夹路径 如 c:/fqf - * @return Map - */ - public static Map GetAllFileNameByMap(String path) { - Map arrList = new HashMap (); - File file = new File(path); - if (!file.exists()) { - return arrList; - } - if (!file.isDirectory()) { - return arrList; - } - String[] tempList = file.list(); - File temp = null; - for (int i = 0; i < tempList.length; i++) { - if (path.endsWith(File.separator)) { - temp = new File(path + tempList[i]); - } else { - temp = new File(path + File.separator + tempList[i]); - } - if (temp.isFile()) { - arrList.put(temp.getName(), temp.getName()); - } - } - return arrList; - } - - /** - * 得到文件夹里面的所有文件名 - * - * @param path - * String 文件夹路径 如 c:/fqf - * @return List - */ - public static String[] GetAllFileNameByPath(String path) { - - File file = new File(path); - if (!file.exists()) { - return null; - } - if (!file.isDirectory()) { - return null; - } - String[] tempList = file.list(); -// String[] arrList = null; - List flist = new ArrayList(); - File temp = null; - for (int i = 0; i < tempList.length; i++) { - if (path.endsWith(File.separator)) { - temp = new File(path + tempList[i]); - } else { - temp = new File(path + File.separator + tempList[i]); - } - if (temp.isFile()) { - flist.add(temp.getName()); - } - } - return (String[])flist.toArray(new String[flist.size()]); - } - /** - * 去掉文件名的后缀 - * @param fileName - * @return String - */ - public static String getNameNoSuffix(String fileName){ - if(fileName.lastIndexOf(".")>=0) - return fileName.substring(0,fileName.lastIndexOf(".")); - else - return fileName; - } - /** - * 返回带扩展名的文件名 - * - * @param fileName - * 文件全路径名 - * @return shortFileName 文件名称 - */ - public static String getFileName(String fileName) { - String shortFileName = fileName; - shortFileName = shortFileName.replace("\\", "/"); - if (shortFileName.indexOf("/") > -1) - shortFileName = shortFileName.substring(shortFileName - .lastIndexOf("/") + 1, shortFileName.length()); - return shortFileName; - } - /** - * 判断文件夹是否存在,不存在在创建,如果创建失败,返回false - * @param path 文件夹路径 - */ - public static boolean isExist(String path) { - File file = new File(path); - //判断文件夹是否存在,如果不存在则创建文件夹 - if (!file.exists()) { - boolean bn=file.mkdir(); - if(!bn) FilesUtil.createFolders(path); - return bn; - }else{ - return true; - } - } - /** - * 判断文件夹是否存在,不存在在创建,如果创建失败,返回false - * @param path 文件夹路径 - */ - public static boolean isExistNotCreate(String path) { - File file = new File(path); - //判断文件夹是否存在,如果不存在则创建文件夹 - if (!file.exists()) { - return false; - }else{ - return true; - } - } + /** + * 读取文本文件内容,返回文本文件的内容字符串,以"\n"分割 + * + * @param filePathAndName String 带有完整绝对路径的文件名 + * @return String 返回文本文件的内容 + */ + public static String readAll(String filePathAndName) { + return readAll(filePathAndName, null); + } + + /** + * 读取文本文件内容,返回文本文件的内容字符串,以"\n"分割 + * + * @param filePathAndName String 带有完整绝对路径的文件名 + * @param encoding String 文本文件打开的编码方式 + * @return String 返回文本文件的内容 + */ + public static String readAll(String filePathAndName, String encoding) { + StringBuilder str = new StringBuilder(""); + String st = ""; + FileInputStream fs = null; + try { + fs = new FileInputStream(URLDecoder.decode(filePathAndName)); + InputStreamReader isr; + if (encoding == null || "".equals(encoding)) { + isr = new InputStreamReader(fs); + } else { + isr = new InputStreamReader(fs, encoding); + } + try { + String data; + BufferedReader br = new BufferedReader(isr); + while ((data = br.readLine()) != null) { + str.append(data).append("\n"); + } + } catch (Exception e) { + return ""; + } + st = str.toString(); + } catch (IOException es) { + st = ""; + } finally { + try { + if (fs != null) fs.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st; + } + + /** + * 读取第n行文本文件内容 + * + * @param filePathAndName String 带有完整绝对路径的文件名 + * @param encoding String 文本文件打开的编码方式 + * @return String 返回第一行文本文件内容 + */ + public static String readLine(String filePathAndName, long rowIndex, + String encoding) { + String st = ""; + StringBuilder str = new StringBuilder(""); + long i = 0; + FileInputStream fs = null; + try { + fs = new FileInputStream(URLDecoder.decode(filePathAndName)); + InputStreamReader isr; + if (encoding == null || "".equals(encoding)) { + isr = new InputStreamReader(fs); + } else { + isr = new InputStreamReader(fs, encoding); + } + BufferedReader br = new BufferedReader(isr); + try { + String data; + while ((data = br.readLine()) != null) { + if (rowIndex == i) { + str.append(data); + break; + } else { + i++; + } + } + } catch (Exception e) { + return ""; + } + st = str.toString(); + } catch (IOException es) { + return ""; + } finally { + try { + if (fs != null) fs.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return st; + } + + /** + * 读取第一行文本文件内容 + * + * @param filePathAndName String 带有完整绝对路径的文件名 + * @return String 返回第一行文本文件内容 + */ + public static String readLine(String filePathAndName) { + return readLine(filePathAndName, 0, null); + } + + /** + * 读取第n行文本文件内容 + * + * @param filePathAndName String 带有完整绝对路径的文件名 + * @return String 返回第n行文本文件内容 + */ + public static String readLine(String filePathAndName, long rowIndex) { + return readLine(filePathAndName, rowIndex, null); + } + + /** + * 新建文件 + * + * @param filePathAndName String 文件路径及名称 如c:/fqf.txt + * @param fileContent String 文件内容 + * @return boolean 是否创建成功 + */ + public static boolean newFile(String filePathAndName, String fileContent) { + return newFile(filePathAndName, fileContent, false); + } + + /** + * 新建文件 + * + * @param filePathAndName String 文件路径及名称 如c:/fqf.txt + * @param fileContent String 文件内容 + * @param flag boolean 是否追加内容,true为追加,false为新建 + * @return boolean 是否创建成功 + */ + public static boolean newFile(String filePathAndName, String fileContent, + boolean flag) { + + try { + File myFilePath = new File(filePathAndName); + if (!myFilePath.exists()) { + myFilePath.createNewFile(); + } + FileWriter resultFile = new FileWriter(myFilePath, flag); + PrintWriter myFile = new PrintWriter(resultFile); + myFile.println(fileContent); + resultFile.close(); + return true; + } catch (Exception e) { + System.out.println("新建文件操作出错"); + e.printStackTrace(); + } + return false; + } + + /** + * 有编码方式的文件创建 + * + * @param filePathAndName 文本文件完整绝对路径及文件名 + * @param fileContent 文本文件内容 + * @param encoding 编码方式 例如 GBK 或者 UTF-8 + * @return boolean 是否创建成功 + */ + public static boolean newFile(String filePathAndName, String fileContent, + String encoding) { + try { + File myFilePath = new File(filePathAndName); + if (!myFilePath.exists()) { + myFilePath.createNewFile(); + } + PrintWriter myFile = new PrintWriter(myFilePath, encoding); + myFile.println(fileContent); + myFile.close(); + return true; + } catch (Exception e) { + System.out.println("创建文件操作出错"); + e.printStackTrace(); + } + return false; + } + + /** + * 删除文件 + * + * @param filePathAndName String 文件路径及名称 如c:/fqf.txt + */ + public static boolean delFile(String filePathAndName) { + try { + java.io.File myDelFile = new java.io.File(filePathAndName); + return myDelFile.delete(); + } catch (Exception e) { + System.out.println("删除文件操作出错"); + e.printStackTrace(); + return false; + } + + } + + /** + * 新建目录 + * + * @param folderPath String 如 c:/fqf + * @return String + */ + public static String newFolder(String folderPath) { + String filePath = folderPath; + try { + java.io.File myFilePath = new java.io.File(filePath); + if (!myFilePath.exists()) { + myFilePath.mkdirs(); + } + } catch (Exception e) { + System.out.println("新建目录操作出错"); + filePath = ""; + e.printStackTrace(); + } + return filePath; + } + + /** + * 删除文件夹 + * + * @param folderPath String 文件夹路径及名称 如c:/fqf + */ + public static void delFolder(String folderPath) { + try { + delAllFile(folderPath); // 删除完里面所有内容 + java.io.File myFilePath = new java.io.File(folderPath); + myFilePath.delete(); // 删除空文件夹 + } catch (Exception e) { + System.out.println("删除文件夹操作出错"); + e.printStackTrace(); + } + } + + /** + * 删除文件夹里面的所有文件 + * + * @param path String 文件夹路径 如 c:/fqf + */ + public static void delAllFile(String path) { + File file = new File(path); + if (!file.exists()) { + return; + } + if (!file.isDirectory()) { + return; + } + if (file.getAbsolutePath().equalsIgnoreCase("/")) { + System.out.println("此目录为根目录,不能删除所有文件!!!"); + System.out.println("请及时修改此目录!!!"); + return; + } + if (file.getAbsolutePath().equalsIgnoreCase("/root")) { + System.out.println("此目录为根目录,不能删除所有文件!!!"); + System.out.println("请及时修改此目录!!!"); + return; + } + if (file.getAbsolutePath().equalsIgnoreCase("/usr") || file.getAbsolutePath().equalsIgnoreCase("/opt") + || file.getAbsolutePath().equalsIgnoreCase("/bin") || file.getAbsolutePath().equalsIgnoreCase("/sbin") + || file.getAbsolutePath().equalsIgnoreCase("/etc") || file.getAbsolutePath().equalsIgnoreCase("/selinux") + || file.getAbsolutePath().equalsIgnoreCase("/sys") || file.getAbsolutePath().equalsIgnoreCase("/var") + || file.getAbsolutePath().equalsIgnoreCase("/home") || file.getAbsolutePath().equalsIgnoreCase("/net")) { + System.out.println("此目录为根目录,不能删除所有文件!!!"); + System.out.println("请及时修改此目录!!!"); + return; + } + if (file.getAbsolutePath().equalsIgnoreCase("C://") || file.getAbsolutePath().equalsIgnoreCase("C:\\\\")) { + System.out.println("此目录为C盘根目录,不能删除所有文件!!!"); + System.out.println("请及时修改此目录!!!"); + return; + } + String[] tempList = file.list(); + File temp; + if (tempList == null) { + return; + } + for (String aTempList : tempList) { + if (path.endsWith(File.separator)) { + temp = new File(path + aTempList); + } else { + temp = new File(path + File.separator + aTempList); + } + if (temp.isFile()) { + temp.delete(); + } + if (temp.isDirectory()) { + delAllFile(path + "/" + aTempList);// 先删除文件夹里面的文件 + delFolder(path + "/" + aTempList);// 再删除空文件夹 + } + } + } + + /** + * 复制单个文件 + * + * @param oldPath String 原文件路径 如:c:/fqf.txt + * @param newPath String 复制后路径 如:f:/fqf.txt + */ + public static void copyFile(String oldPath, String newPath) { + InputStream inStream = null; + FileOutputStream fs = null; + try { + int byteRead; + File oldFile = new File(oldPath); + + if (oldFile.exists()) { // 文件存在时 + inStream = new FileInputStream(oldPath); // 读入原文件 + fs = new FileOutputStream(newPath); + byte[] buffer = new byte[1444]; + while ((byteRead = inStream.read(buffer)) != -1) { + fs.write(buffer, 0, byteRead); + } + } + } catch (Exception e) { + System.out.println("复制单个文件操作出错"); + e.printStackTrace(); + } finally { + try { + if (fs != null) + fs.close(); + if (inStream != null) + inStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + /** + * 复制整个文件夹内容 + * + * @param oldPath String 原文件路径 如:c:/fqf + * @param newPath String 复制后路径 如:f:/fqf/ff + */ + public static void copyFolder(String oldPath, String newPath) { + + try { + (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹 + File a = new File(oldPath); + String[] file = a.list(); + File temp; + for (String aFile : file) { + if (oldPath.endsWith(File.separator)) { + temp = new File(oldPath + aFile); + } else { + temp = new File(oldPath + File.separator + aFile); + } + + if (temp.isFile()) { + FileInputStream input = new FileInputStream(temp); + FileOutputStream output = new FileOutputStream(newPath + + "/" + (temp.getName())); + byte[] b = new byte[1024 * 5]; + int len; + while ((len = input.read(b)) != -1) { + output.write(b, 0, len); + } + output.flush(); + output.close(); + input.close(); + } + if (temp.isDirectory()) { // 如果是子文件夹 + copyFolder(oldPath + "/" + aFile, newPath + "/" + aFile); + } + } + } catch (Exception e) { + System.out.println("复制整个文件夹内容操作出错"); + e.printStackTrace(); + } + } + + /** + * 移动文件到指定目录 + * + * @param oldPath String 如:c:/fqf.txt + * @param newPath String 如:d:/fqf.txt + */ + public static void moveFile(String oldPath, String newPath) { + copyFile(oldPath, newPath); + delFile(oldPath); + } + + /** + * 移动文件到指定目录 + * + * @param oldPath String 如:c:/fqf.txt + * @param newPath String 如:d:/fqf.txt + */ + public static void moveFolder(String oldPath, String newPath) { + copyFolder(oldPath, newPath); + delFolder(oldPath); + } + + /** + * 多级目录创建 + * + * @param folderPath 准备要在本级目录下创建新目录的目录路径 例如 c:myf + * @param paths 无限级目录参数,各级目录以单数线区分 例如 a|b|c + * @return String 返回创建文件后的路径 例如 c:myfac + */ + public static String createFolders(String folderPath, String paths) { + String pathString; + try { + String path; + pathString = folderPath; + StringTokenizer st = new StringTokenizer(paths, "|"); + for (; st.hasMoreTokens(); ) { + path = st.nextToken(); + if (pathString.lastIndexOf("/") != -1) { + pathString = newFolder(pathString + path); + } else { + pathString = newFolder(pathString + path + "/"); + } + } + } catch (Exception e) { + System.out.println("创建多级目录操作出错"); + pathString = ""; + e.printStackTrace(); + } + return pathString; + } + + /** + * 多级目录创建 + * + * @param folderPath 建立多级目录,一次创建 + * @return String 返回创建文件后的路径 例如 c:\aa\bbb\cc + */ + public static String createFolders(String folderPath) { + String pathString = folderPath; + try { + File file = new File(pathString); + if (!file.exists()) { + file.mkdirs(); + } + } catch (Exception e) { + System.out.println("创建多级目录操作出错"); + pathString = ""; + e.printStackTrace(); + } + return pathString; + } + + /** + * 判断指定的文件是否存在。 + * + * @param fileName 要判断的文件的文件名 + * @return 存在时返回true,否则返回false。 + */ + public static boolean isFileExist(String fileName) { + return new File(fileName).isFile(); + } + + /** + * 得到文件夹里面的所有文件 + * + * @param path String 文件夹路径 如 c:/fqf + * @return List + */ + public static List GetAllFile(String path) { + List arrList = new ArrayList(); + File file = new File(path); + if (!file.exists()) { + return arrList; + } + if (!file.isDirectory()) { + return arrList; + } + String[] tempList = file.list(); + File temp; + for (String aTempList : tempList) { + if (path.endsWith(File.separator)) { + temp = new File(path + aTempList); + } else { + temp = new File(path + File.separator + aTempList); + } + if (temp.isFile()) { + arrList.add(temp); + } + if (temp.isDirectory()) { + List s = GetAllFile(temp.getAbsolutePath()); + arrList.addAll(s); + } + } + return arrList; + } + + /** + * 得到文件夹里面的所有文件 + * + * @param path String 文件夹路径 如 c:/fqf + * @return List + */ + public static List GetAllFile(String path, String suffix) { + List arrList = new ArrayList(); + File file = new File(path); + if (!file.exists()) { + return arrList; + } + if (!file.isDirectory()) { + return arrList; + } + String[] tempList = file.list(); + File temp; + for (int i = 0; i < tempList.length; i++) { + if (path.endsWith(File.separator)) { + temp = new File(path + tempList[i]); + } else { + temp = new File(path + File.separator + tempList[i]); + } + if (temp.isFile()) { + if (suffix == null || "".equals(suffix)) + arrList.add(temp); + else { + String filePath = temp.getAbsolutePath(); + if (!suffix.equals("")) { + int begIndex = filePath.lastIndexOf("."); // 最后一个.(即后缀名前面的.)的索引 + String tempSuffix; + + if (begIndex != -1) { + tempSuffix = filePath.substring(begIndex + 1, filePath.length()); + if (tempSuffix.equals(suffix)) { + arrList.add(temp); + } + } + } + } + } + if (temp.isDirectory()) { + List s = GetAllFile(temp.getAbsolutePath(), suffix); + arrList.addAll(s); + } + } + return arrList; + } + + /** + * @param path 文件路径 + * @param suffix 后缀名, 为空则表示所有文件 + * @param isDepth 是否遍历子目录 + * @return list + */ + public static List getListFilesName(String path, String suffix, boolean isDepth) { + List lstFileNames = new ArrayList(); + File file = new File(path); + return listFileName(lstFileNames, file, suffix, isDepth); + } + + private static List listFileName(List lstFileNames, File f, String suffix, boolean isDepth) { + // 若是目录, 采用递归的方法遍历子目录 + if (f.isDirectory()) { + File[] t = f.listFiles(); + + if (t != null) { + for (File aT : t) { + if (isDepth || aT.isFile()) { + listFileName(lstFileNames, aT, suffix, isDepth); + } + } + } + } else { + String filePath = f.getAbsolutePath(); + if (!suffix.equals("")) { + int begIndex = filePath.lastIndexOf("."); // 最后一个.(即后缀名前面的.)的索引 + String tempSuffix; + + if (begIndex != -1) { + tempSuffix = filePath.substring(begIndex + 1, filePath.length()); + if (tempSuffix.equals(suffix)) { + lstFileNames.add(filePath); + } + } + } else { + lstFileNames.add(filePath); + } + } + return lstFileNames; + } + + /** + * 得到文件夹里面的所有文件名 + * + * @param path String 文件夹路径 如 c:/fqf + * @return List + */ + public static List GetAllFileName(String path) { + List arrList = new ArrayList(); + File file = new File(path); + if (!file.exists()) { + return arrList; + } + if (!file.isDirectory()) { + return arrList; + } + String[] tempList = file.list(); + File temp; + for (String aTempList : tempList) { + if (path.endsWith(File.separator)) { + temp = new File(path + aTempList); + } else { + temp = new File(path + File.separator + aTempList); + } + if (temp.isFile()) { + arrList.add(temp.getName()); + } + } + return arrList; + } + + /** + * 得到文件夹里面的所有文件名 + * + * @param path String 文件夹路径 如 c:/fqf + * @return Map + */ + public static Map GetAllFileNameByMap(String path) { + Map arrList = new HashMap(); + File file = new File(path); + if (!file.exists()) { + return arrList; + } + if (!file.isDirectory()) { + return arrList; + } + String[] tempList = file.list(); + File temp; + for (String aTempList : tempList) { + if (path.endsWith(File.separator)) { + temp = new File(path + aTempList); + } else { + temp = new File(path + File.separator + aTempList); + } + if (temp.isFile()) { + arrList.put(temp.getName(), temp.getName()); + } + } + return arrList; + } + + /** + * 得到文件夹里面的所有文件名 + * + * @param path String 文件夹路径 如 c:/fqf + * @return List + */ + public static String[] GetAllFileNameByPath(String path) { + File file = new File(path); + if (!file.exists()) { + return null; + } + if (!file.isDirectory()) { + return null; + } + String[] tempList = file.list(); + List fileList = new ArrayList(); + File temp; + for (int i = 0; i < tempList.length; i++) { + if (path.endsWith(File.separator)) { + temp = new File(path + tempList[i]); + } else { + temp = new File(path + File.separator + tempList[i]); + } + if (temp.isFile()) { + fileList.add(temp.getName()); + } + } + return fileList.toArray(new String[fileList.size()]); + } + + /** + * 去掉文件名的后缀 + * + * @param fileName + * @return String + */ + public static String getNameNoSuffix(String fileName) { + if (fileName.lastIndexOf(".") >= 0) + return fileName.substring(0, fileName.lastIndexOf(".")); + else + return fileName; + } + + /** + * 返回带扩展名的文件名 + * + * @param fileName 文件全路径名 + * @return shortFileName 文件名称 + */ + public static String getFileName(String fileName) { + String shortFileName = fileName; + shortFileName = shortFileName.replace("\\", "/"); + if (shortFileName.contains("/")) + shortFileName = shortFileName.substring(shortFileName + .lastIndexOf("/") + 1, shortFileName.length()); + return shortFileName; + } + + /** + * 判断文件夹是否存在,不存在在创建,如果创建失败,返回false + * + * @param path 文件夹路径 + */ + public static boolean isExist(String path) { + File file = new File(path); + // 判断文件夹是否存在,如果不存在则创建文件夹 + if (!file.exists()) { + boolean bn = file.mkdir(); + if (!bn) FilesUtil.createFolders(path); + return bn; + } else { + return true; + } + } + + /** + * 判断文件夹是否存在,不存在在创建,如果创建失败,返回false + * + * @param path 文件夹路径 + */ + public static boolean isExistNotCreate(String path) { + File file = new File(path); + // 判断文件夹是否存在,如果不存在则创建文件夹 + return file.exists(); + } + + /** + * 复制文件 + * + * @param fileName 文件名 + * @param fountainheadDirs 源文件路径 + * @param armDirs 目的文件路径 + * @return boolean + */ + public boolean copyTheFile(String fileName, String fountainheadDirs, + String armDirs) { + boolean isSucceed = false; + + InputStream inStream = null; + FileOutputStream fs = null; + try { + int byteRead; + File oldFile = new File(fountainheadDirs + "/" + fileName); + File armFileDirs = new File(armDirs); + if (!armFileDirs.exists()) { + armFileDirs.mkdirs(); + } + if (!oldFile.exists()) { + throw new Exception("要复制的文件不存在"); + } + if (oldFile.exists()) { // 文件存在时 + inStream = new FileInputStream(fountainheadDirs + "/" + + fileName); // 读入原文件 + fs = new FileOutputStream(armDirs + "/" + fileName); + byte[] buffer = new byte[1444]; + while ((byteRead = inStream.read(buffer)) != -1) { + fs.write(buffer, 0, byteRead); + } + } + isSucceed = true; + } catch (Exception e) { + System.out.println("读取的文件不存在或复制单个文件操作出错"); + e.printStackTrace(); + } finally { + try { + if (fs != null) + fs.close(); + if (inStream != null) + inStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return isSucceed; + } + + /** + * 剪切文件 + * + * @param fileName 文件名称 + * @param fountainheadDirs 源文件路径 + * @param armDirs 目的文件路径 + * @return boolean + */ + public boolean cutTheFile(String fileName, String fountainheadDirs, + String armDirs) { + boolean isSucceed = false; + + InputStream inStream = null; + FileOutputStream fs = null; + try { + int byteRead; + File oldFile = new File(fountainheadDirs + "/" + fileName); + File armFileDirs = new File(armDirs); + if (!armFileDirs.exists()) { + armFileDirs.mkdirs(); + } + if (!oldFile.exists()) { + throw new Exception("要复制的文件不存在"); + } + if (oldFile.exists()) { // 文件存在时 + inStream = new FileInputStream(fountainheadDirs + "/" + + fileName); // 读入原文件 + fs = new FileOutputStream(armDirs + "/" + fileName); + byte[] buffer = new byte[1444]; + while ((byteRead = inStream.read(buffer)) != -1) { + fs.write(buffer, 0, byteRead); + } + } + isSucceed = true; + } catch (Exception e) { + System.out.println("读取的文件不存在或复制单个文件操作出错"); + e.printStackTrace(); + } finally { + try { + if (fs != null) + fs.close(); + if (inStream != null) + inStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + File deleteFile = new File(fountainheadDirs + "/" + fileName); + if (isSucceed) + isSucceed = deleteFile.delete(); + } + return isSucceed; + } } diff --git a/src/com/itsolife/htmltomarkdown/HtmlToMd.java b/src/com/itsolife/htmltomarkdown/HtmlToMd.java index 3356815..6df61ae 100755 --- a/src/com/itsolife/htmltomarkdown/HtmlToMd.java +++ b/src/com/itsolife/htmltomarkdown/HtmlToMd.java @@ -1,16 +1,6 @@ package com.itsolife.htmltomarkdown; -import java.io.File; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; - +import com.itsolife.htmltomarkdown.MDLine.MDLineType; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -21,385 +11,376 @@ import org.jsoup.safety.Cleaner; import org.jsoup.safety.Whitelist; -import com.itsolife.htmltomarkdown.MDLine.MDLineType; +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.*; /** * HTML转MD工具 - * */ public class HtmlToMd { - private static int indentation = -1; - private static boolean orderedList = false; - - public static String convert(String theHTML, String baseURL) { - Document doc = Jsoup.parse(theHTML, baseURL); - - String md = parseDocument(doc); - return md; - } - - public static String convert(URL url, int timeoutMillis) throws IOException { - Document doc = Jsoup.parse(url, timeoutMillis); - - String md = parseDocument(doc); - return md; - } - - public static String converthtml(String html, String charset) throws IOException { - Document doc = Jsoup.parse(html, charset); - - String md = parseDocument(doc); - return md; - } - public static String convertfile(File f, String charset) throws IOException { - Document doc = Jsoup.parse(f, charset); - - String md = parseDocument(doc); - return md; - } - - public static void htmlToJeykyllmd(String htmlpath,String mdpath,String charset){ - try { - List filelist = FilesUtil.GetAllFile(htmlpath,"html"); - for(int i=0;i filelist = FilesUtil.GetAllFile(htmlpath,"html"); - for(int i=0;i m = new TreeMap(); - for(int j=0;j it =m.keySet().iterator();it.hasNext();){ - String tag =it.next(); - blog.append(" - "+tag+separator); - } - categories.append(s[0]); - String tou ="---"+separator+ - "layout: post"+separator+ - "title: \""+f.getName().replace(".html", "").split("-")[0]+"\""+separator+ - "date: "+datestr+separator+ - "categories: "+categories+separator+ - "tags: "+separator+ - blog.toString()+ - "--- "+separator+ - separator; - FilesUtil.isExist(hmpath); - String parsedtext = HtmlToMd.convertfile(f, "utf-8"); - String newname = datename+"-"+hmpath.replace(mdpath, "").replace("/", "-")+"-"+f.getName(); - String mmname =(hmpath+newname.replace("html", "md")).replaceAll("\\s*", ""); - FilesUtil.newFile(mmname, tou+parsedtext, charset); - } - }catch (MalformedURLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - private static String parseDocument(Document dirtyDoc) { - indentation = -1; - - String title = dirtyDoc.title(); - - Whitelist whitelist = Whitelist.relaxed(); - Cleaner cleaner = new Cleaner(whitelist); - - Document doc = cleaner.clean(dirtyDoc); - doc.outputSettings().escapeMode(EscapeMode.xhtml); - - - if (!title.trim().equals("")) { - return "# " + title + "\n\n" + getTextContent(doc); - } else { - return getTextContent(doc); - } - } - - private static String getTextContent(Element element) { - ArrayList lines = new ArrayList(); - - List children = element.childNodes(); - for (Node child : children) { - if (child instanceof TextNode) { - TextNode textNode = (TextNode) child; - MDLine line = getLastLine(lines); - if (line.getContent().equals("")) { - if (!textNode.isBlank()) { - line.append(textNode.text().replaceAll("#", "/#").replaceAll("\\*", "/\\*")); - } - } else { - line.append(textNode.text().replaceAll("#", "/#").replaceAll("\\*", "/\\*")); - } - - } else if (child instanceof Element) { - Element childElement = (Element) child; - processElement(childElement, lines); - } else { - System.out.println(); - } - } - - int blanklines = 0; - StringBuilder result = new StringBuilder(); - for (int i = 0; i < lines.size(); i++) { - String line = lines.get(i).toString().trim(); - if (line.equals("")) { - blanklines++; - } else { - blanklines = 0; - } - if (blanklines < 2) { - result.append(line); - if (i < lines.size() - 1) { - result.append("\n"); - } - } - } - - return result.toString(); - - } - - private static void processElement(Element element, ArrayList lines) { - Tag tag = element.tag(); - - String tagName = tag.getName(); - if (tagName.equals("div")) { - div(element, lines); - } else if (tagName.equals("p")) { - p(element, lines); - } else if (tagName.equals("br")) { - br(element, lines); - } else if (tagName.matches("^h[0-9]+$")) { - h(element, lines); - } else if (tagName.equals("strong") || tagName.equals("b")) { - strong(element, lines); - } else if (tagName.equals("em")) { - em(element, lines); - } else if (tagName.equals("hr")) { - hr(element, lines); - } else if (tagName.equals("a")) { - a(element, lines); - } else if (tagName.equals("img")) { - img(element, lines); - } else if (tagName.equals("code")) { - code(element, lines); - } else if (tagName.equals("ul")) { - ul(element, lines); - } else if (tagName.equals("ol")) { - ol(element, lines); - } else if (tagName.equals("li")) { - li(element, lines); - } else { - MDLine line = getLastLine(lines); - line.append(getTextContent(element)); - } - } - - private static MDLine getLastLine(ArrayList lines) { - MDLine line; - if (lines.size() > 0) { - line = lines.get(lines.size() - 1); - } else { - line = new MDLine(MDLineType.None, 0, ""); - lines.add(line); - } - - return line; - } - - private static void div(Element element, ArrayList lines) { - MDLine line = getLastLine(lines); - String content = getTextContent(element); - if (!content.equals("")) { - if (!line.getContent().trim().equals("")) { - lines.add(new MDLine(MDLineType.None, 0, "")); - lines.add(new MDLine(MDLineType.None, 0, content)); - lines.add(new MDLine(MDLineType.None, 0, "")); - } else { - if (!content.trim().equals("")) - line.append(content); - } - } - } - - private static void p(Element element, ArrayList lines) { - MDLine line = getLastLine(lines); - if (!line.getContent().trim().equals("")) - lines.add(new MDLine(MDLineType.None, 0, "")); - lines.add(new MDLine(MDLineType.None, 0, "")); - lines.add(new MDLine(MDLineType.None, 0, getTextContent(element))); - lines.add(new MDLine(MDLineType.None, 0, "")); - if (!line.getContent().trim().equals("")) - lines.add(new MDLine(MDLineType.None, 0, "")); - } - - private static void br(Element element, ArrayList lines) { - MDLine line = getLastLine(lines); - if (!line.getContent().trim().equals("")) - lines.add(new MDLine(MDLineType.None, 0, "")); - } - - private static void h(Element element, ArrayList lines) { - MDLine line = getLastLine(lines); - if (!line.getContent().trim().equals("")) - lines.add(new MDLine(MDLineType.None, 0, "")); - - int level = Integer.valueOf(element.tagName().substring(1)); - switch (level) { - case 1: - lines.add(new MDLine(MDLineType.Head1, 0, getTextContent(element))); - break; - case 2: - lines.add(new MDLine(MDLineType.Head2, 0, getTextContent(element))); - break; - default: - lines.add(new MDLine(MDLineType.Head3, 0, getTextContent(element))); - break; - } - - lines.add(new MDLine(MDLineType.None, 0, "")); - lines.add(new MDLine(MDLineType.None, 0, "")); - } - - private static void strong(Element element, ArrayList lines) { - MDLine line = getLastLine(lines); - line.append("**"); - line.append(getTextContent(element)); - line.append("**"); - } - - private static void em(Element element, ArrayList lines) { - MDLine line = getLastLine(lines); - line.append("*"); - line.append(getTextContent(element)); - line.append("*"); - } - - private static void hr(Element element, ArrayList lines) { - lines.add(new MDLine(MDLineType.None, 0, "")); - lines.add(new MDLine(MDLineType.HR, 0, "")); - lines.add(new MDLine(MDLineType.None, 0, "")); - } - - private static void a(Element element, ArrayList lines) { - MDLine line = getLastLine(lines); - line.append("["); - line.append(getTextContent(element)); - line.append("]"); - line.append("("); - String url = element.attr("href"); - line.append(url); - String title = element.attr("title"); - if (!title.equals("")) { - line.append(" \""); - line.append(title); - line.append("\""); - } - line.append(")"); - } - - private static void img(Element element, ArrayList lines) { - MDLine line = getLastLine(lines); - /* - * if (!line.isList() && !line.getContent().equals("")) { lines.add(new - * MDLine(MDLineType.None, 0, "")); line = lines.get(lines.size() - 1); - * } - */ - - line.append("!["); - String alt = element.attr("alt"); - line.append(alt); - line.append("]"); - line.append("("); - String url = element.attr("src"); - line.append(url); - String title = element.attr("title"); - if (!title.equals("")) { - line.append(" \""); - line.append(title); - line.append("\""); - } - line.append(")"); - } - - private static void code(Element element, ArrayList lines) { - lines.add(new MDLine(MDLineType.None, 0, "")); - MDLine line = new MDLine(MDLineType.None, 0, " "); - line.append(getTextContent(element).replace("\n", " ")); - lines.add(line); - lines.add(new MDLine(MDLineType.None, 0, "")); - } - - private static void ul(Element element, ArrayList lines) { - lines.add(new MDLine(MDLineType.None, 0, "")); - indentation++; - orderedList = false; - MDLine line = new MDLine(MDLineType.None, 0, ""); - line.append(getTextContent(element)); - lines.add(line); - indentation--; - lines.add(new MDLine(MDLineType.None, 0, "")); - } - - private static void ol(Element element, ArrayList lines) { - lines.add(new MDLine(MDLineType.None, 0, "")); - indentation++; - orderedList = true; - MDLine line = new MDLine(MDLineType.None, 0, ""); - line.append(getTextContent(element)); - lines.add(line); - indentation--; - lines.add(new MDLine(MDLineType.None, 0, "")); - } - - private static void li(Element element, ArrayList lines) { - MDLine line; - if (orderedList) { - line = new MDLine(MDLineType.Ordered, indentation, - getTextContent(element)); - } else { - line = new MDLine(MDLineType.Unordered, indentation, - getTextContent(element)); - } - lines.add(line); - } + private static int indentation = -1; + private static boolean orderedList = false; + + public static String convert(String theHTML, String baseURL) { + Document doc = Jsoup.parse(theHTML, baseURL); + + return parseDocument(doc); + } + + public static String convert(URL url, int timeoutMillis) throws IOException { + Document doc = Jsoup.parse(url, timeoutMillis); + + return parseDocument(doc); + } + + public static String convertHtml(String html, String charset) throws IOException { + Document doc = Jsoup.parse(html, charset); + + return parseDocument(doc); + } + + public static String convertFile(File f, String charset) throws IOException { + Document doc = Jsoup.parse(f, charset); + + return parseDocument(doc); + } + + public static void htmlToJekyllMd(String htmlPath, String mdPath, String charset) { + try { + List fileList = FilesUtil.GetAllFile(htmlPath, "html"); + for (File f : fileList) { + String mdName = f.getAbsolutePath().replace(htmlPath, mdPath).replace("html", "md"); + String hmPath = mdName.substring(0, mdName.lastIndexOf("/")) + "/"; + String separator = System.getProperty("line.separator"); + String tou = "---" + separator + + "layout: post" + separator + + "title: \"" + f.getName() + "\"" + separator + + "description: \"" + f.getName() + "\"" + separator + + "category: pages\"" + separator + + "tags: [blog]\"" + separator + + "--- " + separator + + "{% include JB/setup %}" + separator + + separator; + FilesUtil.isExist(hmPath); + String parsedText = convertFile(f, charset); + Calendar c = Calendar.getInstance(); + String dateName = DateUtil.dateToShortCode(c.getTime()); + String newName = dateName + "-" + hmPath.replace(mdPath, "").replace("/", "-") + "-" + f.getName(); + String mmName = (hmPath + newName.replace("html", "md")).replaceAll("\\s*", ""); + FilesUtil.newFile(mmName, tou + parsedText, charset); + } + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void htmlToHexoMd(String htmlPath, String mdPath, String charset) { + try { + List fileList = FilesUtil.GetAllFile(htmlPath, "html"); + for (File f : fileList) { + String mdName = f.getAbsolutePath().replace(htmlPath, mdPath).replace("html", "md"); + String hmPath = mdName.substring(0, mdName.lastIndexOf("/")) + "/"; + String separator = System.getProperty("line.separator"); + String[] s = hmPath.replace(mdPath, "").split("/"); + Calendar c = Calendar.getInstance(); + String dateName = DateUtil.dateToShortCode(c.getTime()); + String dateStr = DateUtil.dateToAllCode(c.getTime()); + StringBuilder blog = new StringBuilder(); + StringBuilder categories = new StringBuilder(); + Map m = new TreeMap(); + for (String value : s) { + m.put(value, value); + } + for (String tag : m.keySet()) { + blog.append(" - ").append(tag).append(separator); + } + categories.append(s[0]); + String tou = "---" + separator + + "layout: post" + separator + + "title: \"" + f.getName().replace(".html", "").split("-")[0] + "\"" + separator + + "date: " + dateStr + separator + + "categories: " + categories + separator + + "tags: " + separator + + blog.toString() + + "--- " + separator + + separator; + FilesUtil.isExist(hmPath); + String parsedText = HtmlToMd.convertFile(f, "utf-8"); + String newName = dateName + "-" + hmPath.replace(mdPath, "").replace("/", "-") + "-" + f.getName(); + String mmName = (hmPath + newName.replace("html", "md")).replaceAll("\\s*", ""); + FilesUtil.newFile(mmName, tou + parsedText, charset); + } + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static String parseDocument(Document dirtyDoc) { + indentation = -1; + + String title = dirtyDoc.title(); + + Whitelist whitelist = Whitelist.relaxed(); + Cleaner cleaner = new Cleaner(whitelist); + + Document doc = cleaner.clean(dirtyDoc); + doc.outputSettings().escapeMode(EscapeMode.xhtml); + + if (!title.trim().equals("")) { + return "# " + title + "\n\n" + getTextContent(doc); + } else { + return getTextContent(doc); + } + } + + private static String getTextContent(Element element) { + ArrayList lines = new ArrayList(); + + List children = element.childNodes(); + for (Node child : children) { + if (child instanceof TextNode) { + TextNode textNode = (TextNode) child; + MDLine line = getLastLine(lines); + if (line.getContent().equals("")) { + if (!textNode.isBlank()) { + line.append(textNode.text().replaceAll("#", "/#").replaceAll("\\*", "/\\*")); + } + } else { + line.append(textNode.text().replaceAll("#", "/#").replaceAll("\\*", "/\\*")); + } + + } else if (child instanceof Element) { + Element childElement = (Element) child; + processElement(childElement, lines); + } else { + System.out.println(); + } + } + + int blankLines = 0; + StringBuilder result = new StringBuilder(); + for (int i = 0; i < lines.size(); i++) { + String line = lines.get(i).toString().trim(); + if (line.equals("")) { + blankLines++; + } else { + blankLines = 0; + } + if (blankLines < 2) { + result.append(line); + if (i < lines.size() - 1) { + result.append("\n"); + } + } + } + + return result.toString(); + } + + private static void processElement(Element element, ArrayList lines) { + Tag tag = element.tag(); + + String tagName = tag.getName(); + if (tagName.equals("div")) { + div(element, lines); + } else if (tagName.equals("p")) { + p(element, lines); + } else if (tagName.equals("br")) { + br(lines); + } else if (tagName.matches("^h[0-9]+$")) { + h(element, lines); + } else if (tagName.equals("strong") || tagName.equals("b")) { + strong(element, lines); + } else if (tagName.equals("em")) { + em(element, lines); + } else if (tagName.equals("hr")) { + hr(lines); + } else if (tagName.equals("a")) { + a(element, lines); + } else if (tagName.equals("img")) { + img(element, lines); + } else if (tagName.equals("code")) { + code(element, lines); + } else if (tagName.equals("ul")) { + ul(element, lines); + } else if (tagName.equals("ol")) { + ol(element, lines); + } else if (tagName.equals("li")) { + li(element, lines); + } else { + MDLine line = getLastLine(lines); + line.append(getTextContent(element)); + } + } + + private static MDLine getLastLine(ArrayList lines) { + MDLine line; + if (lines.size() > 0) { + line = lines.get(lines.size() - 1); + } else { + line = new MDLine(MDLineType.None, 0, ""); + lines.add(line); + } + + return line; + } + + private static void div(Element element, ArrayList lines) { + MDLine line = getLastLine(lines); + String content = getTextContent(element); + if (!content.equals("")) { + if (!line.getContent().trim().equals("")) { + lines.add(new MDLine(MDLineType.None, 0, "")); + lines.add(new MDLine(MDLineType.None, 0, content)); + lines.add(new MDLine(MDLineType.None, 0, "")); + } else { + if (!content.trim().equals("")) + line.append(content); + } + } + } + + private static void p(Element element, ArrayList lines) { + MDLine line = getLastLine(lines); + if (!line.getContent().trim().equals("")) + lines.add(new MDLine(MDLineType.None, 0, "")); + lines.add(new MDLine(MDLineType.None, 0, "")); + lines.add(new MDLine(MDLineType.None, 0, getTextContent(element))); + lines.add(new MDLine(MDLineType.None, 0, "")); + if (!line.getContent().trim().equals("")) + lines.add(new MDLine(MDLineType.None, 0, "")); + } + + private static void br(ArrayList lines) { + MDLine line = getLastLine(lines); + if (!line.getContent().trim().equals("")) + lines.add(new MDLine(MDLineType.None, 0, "")); + } + + private static void h(Element element, ArrayList lines) { + MDLine line = getLastLine(lines); + if (!line.getContent().trim().equals("")) + lines.add(new MDLine(MDLineType.None, 0, "")); + + int level = Integer.valueOf(element.tagName().substring(1)); + switch (level) { + case 1: + lines.add(new MDLine(MDLineType.Head1, 0, getTextContent(element))); + break; + case 2: + lines.add(new MDLine(MDLineType.Head2, 0, getTextContent(element))); + break; + default: + lines.add(new MDLine(MDLineType.Head3, 0, getTextContent(element))); + break; + } + + lines.add(new MDLine(MDLineType.None, 0, "")); + lines.add(new MDLine(MDLineType.None, 0, "")); + } + + private static void strong(Element element, ArrayList lines) { + MDLine line = getLastLine(lines); + line.append("**"); + line.append(getTextContent(element)); + line.append("**"); + } + + private static void em(Element element, ArrayList lines) { + MDLine line = getLastLine(lines); + line.append("*"); + line.append(getTextContent(element)); + line.append("*"); + } + + private static void hr(ArrayList lines) { + lines.add(new MDLine(MDLineType.None, 0, "")); + lines.add(new MDLine(MDLineType.HR, 0, "")); + lines.add(new MDLine(MDLineType.None, 0, "")); + } + + private static void a(Element element, ArrayList lines) { + MDLine line = getLastLine(lines); + line.append("["); + line.append(getTextContent(element)); + line.append("]"); + line.append("("); + String url = element.attr("href"); + line.append(url); + String title = element.attr("title"); + if (!title.equals("")) { + line.append(" \""); + line.append(title); + line.append("\""); + } + line.append(")"); + } + + private static void img(Element element, ArrayList lines) { + MDLine line = getLastLine(lines); + + line.append("!["); + String alt = element.attr("alt"); + line.append(alt); + line.append("]"); + line.append("("); + String url = element.attr("src"); + line.append(url); + String title = element.attr("title"); + if (!title.equals("")) { + line.append(" \""); + line.append(title); + line.append("\""); + } + line.append(")"); + } + + private static void code(Element element, ArrayList lines) { + lines.add(new MDLine(MDLineType.None, 0, "")); + MDLine line = new MDLine(MDLineType.None, 0, " "); + line.append(getTextContent(element).replace("\n", " ")); + lines.add(line); + lines.add(new MDLine(MDLineType.None, 0, "")); + } + + private static void ul(Element element, ArrayList lines) { + lines.add(new MDLine(MDLineType.None, 0, "")); + indentation++; + orderedList = false; + MDLine line = new MDLine(MDLineType.None, 0, ""); + line.append(getTextContent(element)); + lines.add(line); + indentation--; + lines.add(new MDLine(MDLineType.None, 0, "")); + } + + private static void ol(Element element, ArrayList lines) { + lines.add(new MDLine(MDLineType.None, 0, "")); + indentation++; + orderedList = true; + MDLine line = new MDLine(MDLineType.None, 0, ""); + line.append(getTextContent(element)); + lines.add(line); + indentation--; + lines.add(new MDLine(MDLineType.None, 0, "")); + } + + private static void li(Element element, ArrayList lines) { + MDLine line; + if (orderedList) { + line = new MDLine(MDLineType.Ordered, indentation, + getTextContent(element)); + } else { + line = new MDLine(MDLineType.Unordered, indentation, + getTextContent(element)); + } + lines.add(line); + } } diff --git a/src/com/itsolife/htmltomarkdown/MDLine.java b/src/com/itsolife/htmltomarkdown/MDLine.java index 39a59e8..b7986f7 100755 --- a/src/com/itsolife/htmltomarkdown/MDLine.java +++ b/src/com/itsolife/htmltomarkdown/MDLine.java @@ -1,139 +1,136 @@ package com.itsolife.htmltomarkdown; public class MDLine { - public enum MDLineType { - Ordered, Unordered, None, Head1, Head2, Head3, HR; - } - - private int level = 0; - private MDLineType type; - private StringBuilder content; - - public MDLine(MDLineType type, int level, String content) { - this.type = type; - this.level = level; - this.content = new StringBuilder(content); - } - - public MDLine create(String line) { - int spaces = 0; - while ((spaces < line.length()) && (line.charAt(spaces) == ' ')) { - spaces++; - } - String content = line.substring(spaces); - - int newLevel = spaces / 4; - - if (content.length() > 0) { - if (content.matches("^[0-9]+\\.\\s.*")) { - int c = 0; - while ((c < content.length()) - && (Character.isDigit(content.charAt(c)))) { - c++; - } - return new MDLine(MDLineType.Ordered, newLevel, - content.substring(c + 2)); - } else if (content.matches("^(\\*|\\+|\\-)\\s.*")) { - return new MDLine(MDLineType.Unordered, newLevel, - content.substring(2)); - } else if (content.matches("^[#]+.*")) { - int c = 0; - while ((c < content.length()) && (content.charAt(c) == '#')) { - c++; - } - MDLineType headerType; - switch (c) { - case 1: - headerType = MDLineType.Head1; - break; - case 2: - headerType = MDLineType.Head2; - break; - default: - headerType = MDLineType.Head3; - break; - } - - while ((c < content.length()) && (content.charAt(c) == ' ')) { - c++; - } - - return new MDLine(headerType, newLevel, content.substring(c)); - } - } - - content = line.substring(4 * newLevel); - - return new MDLine(MDLineType.None, newLevel, content); - } - - public MDLineType getListTypeName() { - return type; - } - - public int getLevel() { - return level; - } - - public String toString() { - StringBuilder newLine = new StringBuilder(); - for (int j = 0; j < getLevel(); j++) { - newLine.append(" "); - } - - if (type.equals(MDLineType.Ordered)) { - newLine.append(String.valueOf(1) + ". "); - } else if (type.equals(MDLineType.Unordered)) { - newLine.append("* "); - } else if (type.equals(MDLineType.Head1)) { - newLine.append("# "); - } else if (type.equals(MDLineType.Head2)) { - newLine.append("## "); - } else if (type.equals(MDLineType.Head3)) { - newLine.append("### "); - } else if (type.equals(MDLineType.HR)) { - newLine.append("----"); - } - - newLine.append(getContent()); - - return newLine.toString(); - } - - public String getContent() { - return content.toString(); - } - - public void append(String appendContent) { - if (content.length() == 0) { - int i = 0; - while (i < appendContent.length() - && Character.isWhitespace(appendContent.charAt(i))) { - i++; - } - content.append(appendContent.substring(i)); - } else - content.append(appendContent); - } - - @Override - public boolean equals(Object o) { - if (o instanceof MDLine) - return ((MDLine) o).type.equals(this.type); - - return false; - } - - public boolean isList() { - return (type.equals(MDLineType.Ordered) || type - .equals(MDLineType.Unordered)); - } - - public void setLevel(int i) { - level = Math.max(i, 0); - } - - public void setListType(MDLineType type2) { - type = type2; - } + private int level = 0; + private MDLineType type; + private StringBuilder content; + + public MDLine(MDLineType type, int level, String content) { + this.type = type; + this.level = level; + this.content = new StringBuilder(content); + } + + public MDLine create(String line) { + int spaces = 0; + while ((spaces < line.length()) && (line.charAt(spaces) == ' ')) { + spaces++; + } + String content = line.substring(spaces); + + int newLevel = spaces / 4; + + if (content.length() > 0) { + if (content.matches("^[0-9]+\\.\\s.*")) { + int c = 0; + while ((c < content.length()) + && (Character.isDigit(content.charAt(c)))) { + c++; + } + return new MDLine(MDLineType.Ordered, newLevel, + content.substring(c + 2)); + } else if (content.matches("^(\\*|\\+|\\-)\\s.*")) { + return new MDLine(MDLineType.Unordered, newLevel, + content.substring(2)); + } else if (content.matches("^[#]+.*")) { + int c = 0; + while ((c < content.length()) && (content.charAt(c) == '#')) { + c++; + } + MDLineType headerType; + switch (c) { + case 1: + headerType = MDLineType.Head1; + break; + case 2: + headerType = MDLineType.Head2; + break; + default: + headerType = MDLineType.Head3; + break; + } + + while ((c < content.length()) && (content.charAt(c) == ' ')) { + c++; + } + + return new MDLine(headerType, newLevel, content.substring(c)); + } + } + + content = line.substring(4 * newLevel); + + return new MDLine(MDLineType.None, newLevel, content); + } + + public MDLineType getListTypeName() { + return type; + } + + public int getLevel() { + return level; + } + + public void setLevel(int i) { + level = Math.max(i, 0); + } + + public String toString() { + StringBuilder newLine = new StringBuilder(); + for (int j = 0; j < getLevel(); j++) { + newLine.append(" "); + } + + if (type.equals(MDLineType.Ordered)) { + newLine.append(String.valueOf(1)).append(". "); + } else if (type.equals(MDLineType.Unordered)) { + newLine.append("* "); + } else if (type.equals(MDLineType.Head1)) { + newLine.append("# "); + } else if (type.equals(MDLineType.Head2)) { + newLine.append("## "); + } else if (type.equals(MDLineType.Head3)) { + newLine.append("### "); + } else if (type.equals(MDLineType.HR)) { + newLine.append("----"); + } + + newLine.append(getContent()); + + return newLine.toString(); + } + + public String getContent() { + return content.toString(); + } + + public void append(String appendContent) { + if (content.length() == 0) { + int i = 0; + while (i < appendContent.length() + && Character.isWhitespace(appendContent.charAt(i))) { + i++; + } + content.append(appendContent.substring(i)); + } else + content.append(appendContent); + } + + @Override + public boolean equals(Object o) { + return o instanceof MDLine && ((MDLine) o).type.equals(this.type); + } + + public boolean isList() { + return (type.equals(MDLineType.Ordered) || type + .equals(MDLineType.Unordered)); + } + + public void setListType(MDLineType type2) { + type = type2; + } + + public enum MDLineType { + Ordered, Unordered, None, Head1, Head2, Head3, HR + } } \ No newline at end of file diff --git a/src/com/itsolife/htmltomarkdown/TestMain.java b/src/com/itsolife/htmltomarkdown/TestMain.java index f4ba4fe..8292d4a 100755 --- a/src/com/itsolife/htmltomarkdown/TestMain.java +++ b/src/com/itsolife/htmltomarkdown/TestMain.java @@ -4,30 +4,24 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; -import java.util.Calendar; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; public class TestMain { - public static void main(String[] args) { - URL url; - try { - url = new URL("http://en.wikipedia.org/wiki/Text_editor"); - url = new URL("http://www.rockpapershotgun.com/2013/12/02/wot-i-think-journey-of-a-roach/"); - String parsedtext = HtmlToMd.convert(url, 30000); - System.out.println(parsedtext); - String pathfile = "/var/www/html/wiznote/工具文章/推荐给开发人员的实用命令行工具.html"; - File f = new File(pathfile); - String parsedtext1 = HtmlToMd.convertfile(f, "gbk"); - System.out.println(parsedtext1); - - System.out.println("done"); - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } + public static void main(String[] args) { + URL url; + try { + url = new URL("http://jsoup.org/"); + String parsedText = HtmlToMd.convert(url, 30000); + System.out.println(parsedText); + String pathFile = "test.html"; + File f = new File(pathFile); + String parsedFileText = HtmlToMd.convertFile(f, "gbk"); + System.out.println(parsedFileText); + + System.out.println("done"); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } } \ No newline at end of file