设为首页收藏本站

最大的系统仿真与系统优化公益交流社区

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6843|回复: 1

[原创] 发一个读取TXT文件数据的程序代码

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
2 y# N- _- R) w. V( ~import java.io.BufferedReader;5 i( O7 t: [8 x, ^. w3 s" e
import java.io.FileInputStream;2 a; }% i6 ?2 L8 U+ w9 n' l
import java.io.FileNotFoundException;7 {4 l! y& E0 [6 h. w+ {3 _& Q
import java.io.IOException;
+ ]* I. j5 p7 I) r5 Bimport java.io.InputStreamReader;1 a& @; a3 f$ H4 p$ t
import java.io.UnsupportedEncodingException;0 ?' B; f2 v: c+ P1 V  w; y
import java.util.StringTokenizer;
0 i+ L2 N; I. T1 G2 C0 ?/ y; Xpublic class TXTReader {
( G7 F0 Z7 z9 h' j protected String matrix[][];, y2 w  W+ y4 K  J5 d( @
protected int xSize;
& {4 @$ U+ K% D4 n# Z  y$ Z( C protected int ySize;
5 _; N8 S( h) }+ P+ @ public TXTReader(String sugarFile) {6 r) H( J, H7 n1 N$ `+ h
  java.io.InputStream stream = null;1 y. H+ x) @$ y1 E
  try {
, K& y- F3 o2 d, L   stream = new FileInputStream(sugarFile);
3 {. D" t1 i4 ?  } catch (FileNotFoundException e) {
, x4 T& a" E3 k& y+ P3 c7 d   e.printStackTrace();
! u8 b3 q) s5 g9 x  }
. S* u# d0 j- L! s1 n  \  BufferedReader in = new BufferedReader(new InputStreamReader(stream));5 d0 \4 Q4 V) r. P
  init(in);
, g! w' c9 Z5 |, Q( U }( ~+ k" E1 t7 n
private void init(BufferedReader in) {2 \' j% A4 e' h
  try {
' X* ~5 K; ?4 I9 x! q& B  s7 p   String str = in.readLine();4 h9 d) {) M9 d- v) D0 m* @
   if (!str.equals("b2")) {
* F" e, `& d/ `4 q7 y/ W    throw new UnsupportedEncodingException(# v9 u3 i! T+ N+ {& i
      "File is not in TXT ascii format");
2 Z0 R2 G4 ]' C   }  W" o4 Z9 F  G
   str = in.readLine();
( O! L, m+ k1 U1 J$ `$ c, [   String tem[] = str.split("[\\t\\s]+");* B. M, s4 H5 l
   xSize = Integer.valueOf(tem[0]).intValue();
+ G5 R# r6 u6 T( I6 J; I; T   ySize = Integer.valueOf(tem[1]).intValue();' @$ m3 w. }: Y- g
   matrix = new String[xSize][ySize];
' h  ?5 N; p. [1 x   int i = 0;
: a1 p9 A) ?4 n! \& Z   str = "";% j3 m6 H* Q3 r- [3 _  @
   String line = in.readLine();) s' H  k4 z& F/ l
   while (line != null) {
% ~5 L% Q: c9 `" M* c    String temp[] = line.split("[\\t\\s]+");
* f( @0 ?  L" {, F5 F! D    line = in.readLine();
9 m9 S$ ~1 N5 c' v, O    for (int j = 0; j < ySize; j++) {: g( W( R; U( R4 ^& H; s# G
     matrix[i][j] = temp[j];
" ^+ c- J8 v4 d* M$ P) t' n    }
8 W! c& E2 |* \5 _$ Q    i++;
! j' J0 |! Z( A8 }3 J   }
2 r& M6 t2 J/ ~: m   in.close();
) ~. a$ Q3 Y# ?9 P1 B  } catch (IOException ex) {$ m5 ~7 |2 X4 Z9 X) q
   System.out.println("Error Reading file");4 l; B% {, K8 q) T
   ex.printStackTrace();
8 O# P  Z, M% S* T' U" B4 U   System.exit(0);4 x% k: ]8 D$ n3 F
  }
7 P3 r% I' j! _6 R* @% A }
) w9 ?$ r7 i4 p# K% b5 W public String[][] getMatrix() {7 Z4 f# T: U0 l4 X# d
  return matrix;( c$ l& _5 v, `5 E2 J
}4 [& ~6 V# V5 K0 O2 T2 L
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|手机版|SimulWay 道于仿真   

GMT+8, 2026-2-7 01:54 , Processed in 0.022497 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表