java中txt文件读取的一段代码

主要逻辑是从txt文件中抽取词:
  1. artists = new HashMap();   
  2.         InputStream is = this.getClass().getClassLoader().getResourceAsStream("org/richfaces/demo/tree/data.txt");   
  3.         ByteArrayOutputStream os = new ByteArrayOutputStream();   
  4.         byte[] rb = new byte[1024];   
  5.         int read;   
  6.         try {   
  7.             do {   
  8.                 read = is.read(rb);   
  9.                 if (read>0) {   
  10.                     os.write(rb, 0, read);   
  11.                 }   
  12.             } while (read>0);   
  13.             String buf = os.toString();   
  14.             StringTokenizer toc1 = new StringTokenizer(buf,"\n");   
  15.             while (toc1.hasMoreTokens()) {   
  16.                 String str = toc1.nextToken();   
  17.                 StringTokenizer toc2 = new StringTokenizer(str, "\t");   
  18.                 String songTitle = toc2.nextToken();   
  19.                 String artistName = toc2.nextToken();   
  20.                 String albumTitle = toc2.nextToken();   
  21.                 toc2.nextToken();   
  22.                 toc2.nextToken();   
  23.                 String albumYear = toc2.nextToken();   
  24.                 Artist artist = getArtistByName(artistName,this);   
  25.                 Album album = getAlbumByTitle(albumTitle, artist);   
  26.                 album.setYear(new Integer(albumYear));   
  27.                 Song song = new Song(getNextId());   
  28.                 song.setTitle(songTitle);   
  29.                 album.addSong(song);   
  30.             }   
  31.         } catch (IOException e) {   
  32.             throw new RuntimeException(e);   
  33.         }  

 

txt文件见附件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值