Java parsing epub Format ebook, HelloWorld program, with source program and related jar package

Source: Internet
Author: User

One, epub ebook format

Related materials and source code are available for download in the link: Http://pan.baidu.com/s/1bnm8YXT

Including

1, Java Project Engineering Test_epub, which includes a jar package and an epub ebook mybook.epub

2. epub Related JAR Package

3, e-book Mybook.epub

The EPUB format is not carefully described here, it is emphasized here that the EPUB format is a compressed format, just change the suffix to. zip or. rar, unzip to see the contents of the file.

Second, Java parsing. epub Format ebook, the specific implementation code is as follows. Wrote a simple HelloWorld program, added the corresponding jar package.

Program Description:

1, read the Epub/mybook.epub file.

2, modify the title in the metadata

3. Export the new. epub File to the project directory. File name Mynewbook.epub

By extracting the Mynewbook.epub file, you can see that the <dc:title> tag content in the <docTitle> and Content.opf in the Toc.ncx file has been modified.

Package Com.hk;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.FileOutputStream ; Import Java.io.ioexception;import java.io.inputstream;import java.io.outputstream;import java.util.ArrayList; Import Java.util.list;import Nl.siegmann.epublib.domain.book;import Nl.siegmann.epublib.epub.epubreader;import Nl.siegmann.epublib.epub.epubwriter;public class Test {public static void main (string[] args) {System.out.println (" Hello World "); Epubreader Epubreader = new Epubreader ();//Processing IO stream path String currentpath = Thread.CurrentThread (). GetClass (). GetResource (    "/"). ToString ();    String Epubpath = Currentpath + "Epub/mybook.epub";    Epubpath = epubpath.substring (6, Epubpath.length ());    Epubpath = Epubpath.replace ("/", "//");    System.out.println (Epubpath); Read epub file Book book = null;try {inputstream inputstr = new FileInputStream (epubpath); book = Epubreader.readepub (INPUTSTR) ;} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {E.printStackTrace ();} Set the title.//in the epub file to modify the <dc:title> tag content in <docTitle> and Content.opf in the Toc.ncx file. list<string> titleslist = new arraylist<string> (); Titleslist.add ("Test book"); Book.getmetadata (). Settitles (titleslist);//write epubepubwriter epubwriter = new Epubwriter (); try {outputstream ouput = new FileOutputStream ("Mynewbook.epub"); Epubwriter.write (book, ouput);} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}}}

If you do not understand and write the wrong place, please comment.

Java parsing epub Format ebook, HelloWorld program, with source program and related jar package

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.