=======================> BookLocator第一版 <============================
遍历电子书文件夹,并按照出版商分类输出。
输出方式包括consile和XML files两种形式。
package cn.mywork.main;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class BookLocator {
private EnumMap<publishers, List<String>> books = new EnumMap<publishers, List<String>>(
publishers.class );
private List<String> oList, apList, mapList, sList, wiList, wrList, mcList,
otList, pList, adList;
private enum publishers {
oreilly, apress, manning, sams, wiley, wrox, mcgraw, prentice, addison, others
}
private enum subjects {
java, python, javascript, css, linux, hibernate,
}
public void sortBooksByPubs( File dir ) {
initPubLishers();
if ( dir.isDirectory() ) {
String filename;
boolean isNamedPub;
for ( File file : dir.listFiles() ) {
filename = file.getName();
isNamedPub = false;
for ( publishers pub : publishers.values() ) {
// TODO consider regex here
if ( filename.toLowerCase().contains( pub.toString() ) ) {
switch ( pub ) {
case oreilly:
books.get( publishers.oreilly ).add( filename );
break;
case apress:
books.get( publishers.apress ).add( filename );
break;
case manning:
books.get( publishers.manning ).add( filename );
break;
case sams:
books.get( publishers.sams ).add( filename );
break;
case wiley:
books.get( publishers.wiley ).add( filename );
break;
case wrox:
books.get( publishers.wrox ).add( filename );
break;
case mcgraw:
books.get( publishers.mcgraw ).add( filename );
break;
case prentice:
books.get( publishers.prentice ).add( filename );
break;
case addison:
books.get( publishers.addison ).add( filename );
break;
default:
break;
}
isNamedPub = true;
}
}
if ( !isNamedPub ) {
books.get( publishers.others ).add( filename );
}
}
}
}
public void exportBooks( String key ) throws IOException {
//output to XML files
Properties prop = new Properties();
for ( Map.Entry<publishers, List<String>> entry : books.entrySet() ) {
int n=1;
prop.clear();
for ( String bookname : entry.getValue() ) {
prop.setProperty( "" + n++, bookname );
}
String publisher = entry.getKey().toString();
FileOutputStream fos = new FileOutputStream("D://temp//i//" + publisher + ".xml");
prop.storeToXML( fos, "Books of " + publisher.toUpperCase() );
}
//output to console
// for ( Map.Entry<publishers, List<String>> entry : books.entrySet() ) {
// System.out.println( "Books of " + entry.getKey() + " :" );
// for ( String bookname : entry.getValue() ) {
// System.out.println( bookname );
// }
// System.out.println();
// }
}
public static void main( String[] args ) {
BookLocator loc = new BookLocator();
loc.sortBooksByPubs( new File( "E://MyDoc//StudyRec//Books" ) );
try {
loc.exportBooks( null );
} catch ( IOException e ) {
e.printStackTrace();
}
}
private void initPubLishers() {
oList = new ArrayList<String>();
apList = new ArrayList<String>();
mapList = new ArrayList<String>();
sList = new ArrayList<String>();
wiList = new ArrayList<String>();
wrList = new ArrayList<String>();
mcList = new ArrayList<String>();
otList = new ArrayList<String>();
pList = new ArrayList<String>();
adList = new ArrayList<String>();
books.put( publishers.oreilly, oList );
books.put( publishers.apress, apList );
books.put( publishers.manning, mapList );
books.put( publishers.sams, sList );
books.put( publishers.wiley, wiList );
books.put( publishers.wrox, wrList );
books.put( publishers.mcgraw, mcList );
books.put( publishers.others, otList );
books.put( publishers.prentice, pList );
books.put( publishers.addison, adList );
}
}
附 输出的XML文件( oreilly.xml )内容样例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Books of OREILLY</comment>
<entry key="18">oreilly[1].jakarta.commons.cookbook.nov.2004.ebook-ddu.chm</entry>
<entry key="17">oreilly[1].bash.quick.reference.jun.2006.pdf</entry>
<entry key="16">OReilly.Programming.Python.3rd.Edition.Aug.2006.chm</entry>
<entry key="15">OReilly.Mastering.Regular.Expressions.3rd.Edition.Aug.2006.chm</entry>
<entry key="14">OReilly.Managing.Projects.with.GNU.make.3rd.Edition.Nov.2004.eBook-DDU.chm</entry>
<entry key="13">OReilly.Learning.UML.2.0.Apr.2006.chm</entry>
<entry key="12">OReilly.Java.Generics.and_Collections.Oct.2006.chm</entry>
<entry key="11">OReilly.Java.and.XML.3rd.Edition.Dec.2006.eBook-BBL.chm</entry>
<entry key="10">OReilly.CSS.The.Definitive.Guide.3rd.Edition.Nov.2006.chm</entry>
<entry key="9">OReilly.CSS.Cookbook.2nd.Edition.Oct.2006.chm</entry>
<entry key="8">OReilly, UML 2.0 in a Nutshell (2007).pdf</entry>
<entry key="7">OReilly - Java Network Programming 3rd Edition(October 2004).chm</entry>
<entry key="6">OReilly - Java 1.5 Tiger, A Developer's Notebook.chm</entry>
<entry key="5">OReilly - Head First Object-Oriented Design and Analysis.pdf</entry>
<entry key="4">OReilly - Head First Design Patterns2.pdf</entry>
<entry key="3">OReilly - Head First Design Patterns.pdf</entry>
<entry key="2">Oreilly - Head First Design Patterns(Good).pdf</entry>
<entry key="1">Oreilly - Ant The Definitive Guide.pdf</entry>
</properties>
=======================> BookLocator第一版 <============================
=======================> BookLocator V2 <============================
package cn.mywork.main;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class BookLocator {
private enum publishers {
oreilly, apress, manning, sams, wiley, wrox, mcgraw, prentice, addison, others
}
private enum subjects {
java, python, javascript, css, linux, hibernate,
}
private EnumMap<subjects,List<String>> initSubjects(){
EnumMap<subjects, List<String>> initBooks = new EnumMap<subjects, List<String>>(subjects.class);
initBooks.put(subjects.css, new ArrayList<String>());
initBooks.put(subjects.python, new ArrayList<String>());
return initBooks;
}
public EnumMap<subjects,List<String>> sortBooksBySubs( File bookDir){
EnumMap<subjects, List<String>> sortedBooks = initSubjects();
for (File file : bookDir.listFiles( new FileFilter(){
//only accept books of type 'chm' or 'pdf'
public boolean accept(File pathname) {
if (pathname.getName().endsWith( ".chm" ) || pathname.getName().endsWith( ".pdf" )) {
return true;
}
return false;
}})) {
for (subjects subject : subjects.values()) {
if (file.getName().toLowerCase().contains( subject.toString() ) ) {
sortedBooks.get( subject ).add( file.getName() );
break;
}
}
}
return sortedBooks;
}
// try to make a general output method here
public <T> void exportSortedBooks( Map< T, List<String>> sortedBooks){
for (Map.Entry<T, List<String>> entry : sortedBooks.entrySet()) {
System.out.println("/n Books about '" + entry.getKey().toString() +"' are:");
for (String bookname : entry.getValue()) {
System.out.println(bookname);
}
}
}
public EnumMap<publishers,List<String>> sortBooksByPubs( File bookDir ) {
EnumMap<publishers, List<String>> sortedBooks = initPubLishers();
for (File file : bookDir.listFiles( new FileFilter(){
public boolean accept(File pathname) {
if (pathname.getName().endsWith(".chm") || pathname.getName().endsWith(".pdf")) {
return true;
}
return false;
}} )) {
for (publishers publisher: publishers.values()) {
if (file.getName().contains( publisher.toString() ) ) {
sortedBooks.get( publisher ).add( file.getName() );
}else{
if (!sortedBooks.get( publishers.others ).contains( file.getName() ) ) {
sortedBooks.get( publishers.others ).add( file.getName() );
}
}
}
}
return sortedBooks;
}
public <T> void exportBooks( Map<T, List<String>> books) throws IOException {
//output to XML files
// Properties prop = new Properties();
// for ( Map.Entry<publishers, List<String>> entry : books.entrySet() ) {
// int n=1;
// prop.clear();
// for ( String bookname : entry.getValue() ) {
// prop.setProperty( "" + n++, bookname );
// }
//
// String publisher = entry.getKey().toString();
// FileOutputStream fos = new FileOutputStream("D://temp//i//" + publisher + ".xml");
// prop.storeToXML( fos, "Books of " + publisher.toUpperCase() );
// }
//output to console
for ( Map.Entry<T, List<String>> entry : books.entrySet() ) {
System.out.println( "Books of " + entry.getKey() + " :" );
for ( String bookname : entry.getValue() ) {
System.out.println( bookname );
}
System.out.println();
}
}
public static void main( String[] args ) {
BookLocator loc = new BookLocator();
loc.exportSortedBooks( loc.sortBooksByPubs( new File( "E://Fitzwilliam" ) ) );
}
private EnumMap<publishers,List<String>> initPubLishers() {
EnumMap<publishers, List<String>> initBooks = new EnumMap<publishers, List<String>>(publishers.class);
initBooks.put( publishers.oreilly, new ArrayList<String>() );
initBooks.put( publishers.apress, new ArrayList<String>() );
initBooks.put( publishers.manning, new ArrayList<String>() );
initBooks.put( publishers.sams, new ArrayList<String>() );
initBooks.put( publishers.wiley, new ArrayList<String>() );
initBooks.put( publishers.wrox, new ArrayList<String>() );
initBooks.put( publishers.mcgraw, new ArrayList<String>() );
initBooks.put( publishers.others, new ArrayList<String>() );
initBooks.put( publishers.prentice, new ArrayList<String>() );
initBooks.put( publishers.addison, new ArrayList<String>() );
return initBooks;
}
}
------------------------------------> Make a simple booklocator here <--------------------------------------
package cn.mywork.main;
import java.io.File;
import java.io.FileFilter;
public class SimpleBookLocator {
public File[] targetBooks(File bookDir, final String keyword){ // must be final, but do not know why. >_<!! somebody can tell me??
return bookDir.listFiles( new FileFilter(){
public boolean accept(File pathname) {
String bookname = pathname.getName().toLowerCase();
if (bookname.endsWith(".chm") || bookname.endsWith(".pdf") && bookname.contains( keyword )) {
return true;
}
return false;
}});
}
public static void main(String[] args) {
for (File file : new SimpleBookLocator().targetBooks( new File("E://Fitzwilliam"), "css")) {
System.out.println( file.getName() );
}
}
}