免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2161 | 回复: 3
打印 上一主题 下一主题

一个关于split的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-10-30 17:17 |只看该作者 |倒序浏览
在我的一个文档a.txt中有2行数据:
ww^123^a^^d^4d
123^xy^99^3

每一行中的数据都用符号“^”隔开,我想打印出这2行数据,结果打印出了3行,因为第一行的第4个数据是空值,如果我想要原样的打印出2行而不是3行,该怎样修改?
我的代码:

import   java.io.*;
import java.util.ArrayList;

  public   class   Test   {  
         
   public   static   void   main(String[]   args)   {   
            
      try   {  
         
          int  nLineCount = 0;//行数   
       File file  = new File("D:\\a.txt");   
          BufferedReader in = new   BufferedReader(new FileReader(file));   
          String   strLine   = "";   
          StringBuffer strBuffer = new StringBuffer(1000);
  
          while ((strLine  = in.readLine()) !=   null){   
            strBuffer.append(strLine);   
            strBuffer.append("^^");   
            ++nLineCount;   
          }  
  
          String[] strTemp = (strBuffer.toString()).split("\\^\\^");   
         
         System.out.println("temp: "+strTemp[0]);
         System.out.println("temp: "+strTemp[1]);
         System.out.println("temp: "+strTemp[2]);
         
        }catch(Exception   e){   
         e.printStackTrace();   
        }  
  
   }   
}

[ 本帖最后由 darkboy 于 2007-10-30 17:21 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-10-30 18:15 |只看该作者
  1. while ((strLine  = in.readLine()) !=   null){   
  2.                     System.out.println(strLine);
  3.                   }  
复制代码

论坛徽章:
0
3 [报告]
发表于 2007-10-30 18:41 |只看该作者
谢谢版主,问题已经解决,是我读取文件每一行的方法错了

[ 本帖最后由 darkboy 于 2007-10-30 18:42 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2007-10-30 19:19 |只看该作者
你想做的处理是什么,读然后打印?那么就直接读,然后打印。
为什么要手动插入^^,然后再split,,,
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP