例如我有一个TXT文件,里面的内容是“price1=50,price2=60,price3=90”,如何确认price2并提取price2的60,而且要是int类型。请教高手。

解决方案 »

  1.   


    package com.monitor1394.test;/**
     *
     * @author monitor
     * Created on 2011-1-9, 12:45:48
     */
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;public class Regex{
       public static void main(String[] args) {
          String str[]={
              "price1=50,price2=60,price3=90"
          };
          Pattern p = Pattern.compile(".*price2=(\\d+).+");
          Matcher m;
          for(String s:str){
              m=p.matcher(s);
              if(m.find()){
                  int value=Integer.parseInt(m.group(1));
                  System.out.println(value);
              }
          }     
       }
    }
      

  2.   

    "price1=50,price2=60,price3=90"存储在如a.txt文本文件中
    观察这个字符串的结构,是一个典型的映射类型,所以可以使用Map来存储它们。import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.Set;public class Test {
        public static void main(String[] args) throws IOException {
            Map<String, Integer> pairs = read("a.txt"); // 从文件中读取
            System.out.println("所有的: " + pairs);
            
            // 输出映射中的所有键值对
            Set<Map.Entry<String, Integer>> entries = pairs.entrySet();
            for (Map.Entry<String, Integer> entry : entries) {
                System.out.printf("%s = %d\n", entry.getKey(), entry.getValue());
            }        // 取得price2的值
            String key = "price2";
            System.out.println(pairs.get(key));
        }    // 从文件中读取成对的price和数字放到映射map中
        public static Map<String, Integer> read(String fileName) {
            Map<String, Integer> pairs = new HashMap<String, Integer>();        Scanner scanner;
            try {
                scanner = new Scanner(new File("a.txt"));
                while (scanner.hasNextLine()) {
                    // 先按行读取,分解成price1=50这样的数组
                    String[] temp = scanner.nextLine().trim().split(",|,");
                    for (String str : temp) {
                        // 再把price1=50这样的字符串分解成price1和50两个字符串
                        // 使用price1作为键,50作为值存储到映射map中
                        String[] entry = str.split("=");
                        pairs.put(entry[0].trim(), intValueOfString(entry[1]));
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }        return pairs;
        }    // 把字符串转换成整数,如" 20" 为20
        public static Integer intValueOfString(String val) {
            try {
                return Integer.valueOf(val.trim());
            } catch (Exception e) {
            }        return Integer.MIN_VALUE;
        }
    }
      

  3.   

    用类java.util.ResourceBundle可以做到需要的功能,用正则表达式也应该可以。详情看java文档吧。  import java.util.*;
      
      public class TestResourceBundle {
          public static void main(String[] args) {
              ResourceBundle resource = ResourceBundle.getBundle("resource");
              System.out.println(resource.getString("price1"));
              System.out.println(resource.getString("price2"));
              System.out.println(resource.getString("price3"));
          }
      }
    resource.properties 文件:    price1=50
        price2=60
        price3=90
    ]
      

  4.   


    String line = "price1=50,price2=60,price3=90";
    String subLine [] = line.split("\\,");
    HashMap<String,Integer> data = new HashMap<String,Integer>();
    for(String s : subLine){
      String d [] = s.split("\\=");
      data.put(d[0],Integer.valueOf(d[1]);
    }int p2 = data.get("price2");
      

  5.   

    上面有个代码要重写一下:
    String subLine [] = line.split("\\,|\\,");
    忘了还有中文的逗号了。
      

  6.   

    虽然正则很给力。。但是我为什么感觉用split更简单呢。。