String的分割

String的分割

使用split函数,加上正则式来完成分割的目的,或者使用StringTokenizer类来完成相同的功能,也可以使用Pattern达到相同的目的。

下面以获取数据流量为例。

手机流量在proc/net/dev文件里能读取到,但是每次开机会清零,若是有严格的需要,可以实时读取这个值并记录下来。

该文件内容格式如下:

Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo:  265184    3680    0    0    0     0          0         0   265184    3680    0    0    0     0       0          0
rmnet0:     790       4    0    0    0     0          0         0      890       7    0    0    0     0       0          0
rmnet1:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
rmnet2:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
rmnet3:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
rmnet4:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
rmnet5:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
rmnet6:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
rmnet7:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
  sit0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
ip6tnl0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
 wlan0: 1186842    6474    0    0    0     0          0         0   296439   16999    0    0    0     0       0          0

public long[] getFlowBytes() {

		File devFile = new File("proc/net/dev");//存放信息的文件。关机会清除
		FileReader mFileReader = null;
		try {
			mFileReader = new FileReader(devFile);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (mFileReader != null) {
			BufferedReader in = new BufferedReader(mFileReader, 500);
			String line;
			String[] segs;
			long dataFlow = 0;
			long wifiFlow = 0;
			long RxBytes = 0;
			long TxBytes = 0;
			try {

				while ((line = in.readLine()) != null) {
					segs = line.trim().split(":");
//					Log.v("line", line);

					if (line.startsWith("rmnet0")) {
						Pattern p = Pattern.compile(" \\d+ ");  //使用数字来匹配
					    Matcher matcher = p.matcher(segs[1]);
					    int count = 0;
					    if(matcher.find()){
					    	RxBytes = Long.parseLong(matcher.group().trim());
					    }
					    
					    while(matcher.find()){
					    	if(count == 7)
					    	{
					    		TxBytes = Long.parseLong(matcher.group().trim());
						    	break;
					    	}
					    	count++;
					    	
					    }
					    
						Log.v("2g/3g", RxBytes + "   " + TxBytes);
						
						if (RxBytes != 0 && TxBytes != 0) {
							dataFlow += (RxBytes + TxBytes);
						}
					}

					else if (line.startsWith(" wlan0"))

					{
						Pattern p = Pattern.compile(" \\d+ ");
					    Matcher matcher = p.matcher(segs[1]);
					    int count = 0;
					    if(matcher.find()){
					    	RxBytes = Long.parseLong(matcher.group().trim());
					    }
					    
					    while(matcher.find()){
					    	if(count == 7)
					    	{
					    		TxBytes = Long.parseLong(matcher.group().trim());
						    	break;
					    	}
					    	count++;
					    	
					    }
					    
						Log.v("wifi", RxBytes + "   " + TxBytes);
						
						if (RxBytes != 0 && TxBytes != 0) {
							wifiFlow += (RxBytes + TxBytes);
						}
					}
				}

				mFileReader.close();

				return new long[]{dataFlow,wifiFlow};
			} catch (IOException e) {
			}
		}

		return new long[]{0,0};
	}

使用split和上面差不多,其实都是用到了正则式来匹配。

netdata = segs[1].trim().split("" +"");    //由于文件中空格的数量不一致,所以通过空格来分割
Log.v("2g/3g", Arrays.toString(netdata));
long RxBytes = Long.parseLong(netdata[0]);
long TxBytes = Long.parseLong(netdata[8]);

下面是StringTokenizer的例子,是java的api


构造函数
public StringTokenizer(String str)
public StringTokenizer(String str, String delim)
public StringTokenizer(String str, String delim, boolean returnDelims)
第一个参数就是要分隔的String,第二个是分隔字符集合,第三个参数表示分隔符号是否作为标记返回,如果不指定分隔字符,默认的是:”\t\n\r\f”

String s = new String("The Java platform is the ideal platform for network computing");
StringTokenizer st = new StringTokenizer(s);
System.out.println( "Token Total: " + st.countTokens() );
while( st.hasMoreElements() ){
System.out.println( st.nextToken() );
           }
结果为:
Token Total: 10
The
Java
platform
is
the
ideal
platform
for
network
computing


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值