[Java]从一个Updater中学到的

来源:互联网 发布:最全的化合物数据库 编辑:程序博客网 时间:2024/04/27 23:25

10年5月,在做作业的闲暇时间利用Intelligent Web上学到的知识,学了一个Updater,可以从某网站上获取新更新的图片,读取到本地后可以选择浏览或下载。虽然这个工具现在基本没有使用了,但是从中还是学习到很多基础技术要点。


在Java中使用正则表达式

String rgxTitle = "<span class=\"filetitle\">(.+?)<";Pattern ptnTitle = Pattern.compile(rgxTitle);Matcher mtcTitle = ptnTitle.matcher(this.content);if (mtcTitle.find())return mtcTitle.group(1);elsereturn "";


建立一个网络连接并读取URL页面内容

URL u = new URL("http://www.google.com");BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream()));

读取图片并显示

URL u = new URL("http://www.google.com/title.jpg");Image image=ImageIO.read(u);ImageIcon ic = new ImageIcon(image);JLabel label =new JLabel(ic);

下载

FileOutputStream fos = null;BufferedInputStream bis = null;HttpURLConnection httpUrl = null;byte[] buf = new byte[8096];int size = 0;httpUrl = (HttpURLConnection) u.openConnection();httpUrl.connect();bis = new BufferedInputStream(httpUrl.getInputStream());fos = new FileOutputStream("Save\\" + filename + ".jpg");while ((size = bis.read(buf)) != -1)fos.write(buf, 0, size);fos.close();bis.close();httpUrl.disconnect();


将内容复制至剪切板

Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();Transferable tText = new StringSelection(new String("Sample Text"));   sysc.setContents(tText, null);