小弟学着在做一个发送邮件的小程序
将收件人地址放在一个单独的TXT文件中,要添加收件人的时候直接改TXT的内容就行了。然后打包成.bat用WIN7计划任务定时执行就行了。
现在想把收件人地址TXT中加入收件人名字等信息。
比如之前是[email protected]  改成  张三<[email protected]>
怎么实现只读取TXT中<>括起来的内容呢?
我之前是引用TXT中所有的内容作为收件人地址:File tfile = new File("e:/info/sendto.txt");
FileInputStream tfis = null;
BufferedReader br = null;
String sendto = null;
try {
        tfis = new FileInputStream(tfile);
InputStreamReader treader = new InputStreamReader(tfis);
BufferedReader tbr = new BufferedReader(treader );
sendto = tbr.readLine();
} catch (IOException e) {
}
email.addTo(""+sendto+"");

解决方案 »

  1.   

    最简单的方法是用split拆分字符串
    复杂一点写个正则匹配
      

  2.   

    sendTo=sendTo.substring(sendTo.indexOf("<")+1,sendTo.substring(sendTo.indexOf(">")));
      

  3.   

    支持2楼方法 sendTo=sendTo.substring(sendTo.indexOf("<")+1,sendTo.substring(sendTo.indexOf(">"))); 
      

  4.   

    [code]
    String[] mailList = { "张三<[email protected]>", "张飞<[email protected]>",
    "张四<[email protected]>", };
    for(String mail : mailList){
    System.out.println(mail.replaceAll(".*<(.*)>.*", "$1"));
    }
    [/code]
      

  5.   

    用正则表达式过滤
    public static void main(String[] args) {
    String s =  "张三<[email protected]>张三<[email protected]>张三<[email protected]>张三<[email protected]>";
    Matcher m = Pattern.compile("\\<(.+?)\\>").matcher(s);
    while(m.find()){
    System.out.println(m.group(1));
    }
    }