- JButton button = new JButton("提取后缀");
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- textArea_1.setText(null);
- String[] lineString = textArea.getText().split("\\n");//对文本区批量的URL切分成行
- for(int i=0;i<lineString.length;i++) {
- String strUrl = lineString[i];
- URL url;
- try {
- url = new URL(strUrl);
- //获取URL的文件路径
- //即http://www.baidu.com/music/index.html的文件路径为music/index.html
- String path = url.getPath();
- if(path.contains(".")) { //将music/index.html的后缀切分出来
- String[] s = path.split("\\\\."); //双斜杠为转义字符
- String str = "."+s[s.length-1];//s[s.lentgh-1]为截取的最后一部分,即后缀
- //System.out.println(textArea_1.getText());
- if(!(textArea_1.getText().contains(str))) {
- textArea_1.append(str);
- textArea_1.append("\\n");
- }
- }
- } catch (MalformedURLException e1) {
- System.out.println(e.toString());
- } catch (Exception x) {
- // TODO Auto-generated catch block
- x.printStackTrace();
- }
- }
- }
- });
- //该片段来自于http://www.codesnippet.cn/detail/0704201512109.html
来源: http://www.codesnippet.cn/detail/0704201512109.html