⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookmarklist.java

📁 书籍"Java_面向事件编程"的附带光盘代码
💻 JAVA
字号:
import java.io.*;// class to handle a list of bookmarks// the bookmarks can be saved or loaded from an html formatted filepublic class BookmarkList {    private EmptyBookmarks list;  // the list of bookmarks    public BookmarkList( ){        list = new EmptyBookmarks();    }    // return the number of bookmarks in the list    public int size( ){        return list.getSize();    }    // return the bookmark at index     public Bookmark getSelectedItem( int index ){        return list.getSelectedItem( size() - (index + 1) );    }        // add a bookmark to the list    public void add( Bookmark entry ){        list = list.add(entry);    }        // Print all bookmark entries    public void printBookmarks()    {        for ( int i = 0; i < size(); i++) {            System.out.println( getSelectedItem(i).toString() );        }    }        //  Place all the bookmarks in the list into the bookmarks.html    //  file.  Use the standard format employed by most common    //  browsers.    public void saveBookmarksFile() {                try {                         //  Access the file through a PrintWriter             PrintWriter bookmarksFile =                new PrintWriter( new FileWriter("bookmarks.html")) ;                        //  place prologue in file            bookmarksFile.println("<!DOCTYPE NETSCAPE-Bookmark-file-1>");            bookmarksFile.println(                                  "<!-- This is an automatically generated file.");            bookmarksFile.println("It will be read and overwritten.");            bookmarksFile.println("Do Not Edit! -->");            bookmarksFile.println("<TITLE>Bookmarks</TITLE>");            bookmarksFile.println("<H1>Bookmarks</H1>");            bookmarksFile.println("");            bookmarksFile.println("<DL><p>");                        //  add one <DL> line describing each bookmark            for ( int i = 0; i < size(); i++)                bookmarksFile.println( getSelectedItem(i).toBookmarkFileEntry());                        //  add epilogue and close file            bookmarksFile.println("</DL><p>");            bookmarksFile.close();                    } catch ( IOException e) {            System.out.println("Unable to save bookmarks file - " + e.getMessage());         }    }            //  Add all the bookmarks found in the file "bookmarks.html"    //  to this list of Bookmarks.    public void retrieveBookmarksFile() {                try {            BufferedReader bookmarksFileReader =                new BufferedReader( new FileReader( "bookmarks.html" ));                        //  Read through all the lines of the file                        String curLine = bookmarksFileReader.readLine();            while ( curLine != null ) {                                //  process lines that appear to contain bookmarks                if ( curLine.indexOf( "<A ") >= 0 ) {                                        Bookmark entry = extractBookmark( curLine );                    if ( entry != null ){                        add( entry );                    }                }                                //  move on to the next line                curLine = bookmarksFileReader.readLine();                            }                        bookmarksFileReader.close();                    } catch ( IOException e) {            System.out.println("Unable to retrieve bookmarks - " + e.getMessage());        }    }    // extract the the description and address of a bookmark from a line of    // html formatted text    private Bookmark extractBookmark( String line ) {        final String UrlPreSeq = "HREF=\"";        final String UrlPostSeq = "\"";        final String NamePreSeq = "\">";        final String NamePostSeq = "</A>";                String url, name;        int start, end;                start = line.indexOf( UrlPreSeq );        end = line.indexOf( UrlPostSeq , start + UrlPreSeq.length());                System.out.println(line+","+start+","+end);        if ( end > 0 ) {            url = line.substring( start + UrlPreSeq.length(), end );            System.out.println( url );                        start = line.indexOf( NamePreSeq, end );            end = line.indexOf( NamePostSeq, start + NamePreSeq.length() );                        if ( end > 0 ) {                name = line.substring( start + NamePreSeq.length() , end );                System.out.println( name );                return new Bookmark( name, url );            }        }                return null;    }            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -