设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8388|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;+ r2 s4 B- h1 v% S8 L
import java.io.BufferedReader;
& m+ d7 `6 @) e8 s) z/ b% `* }) L3 simport java.io.FileInputStream;3 I6 \0 |, v: U+ `1 @
import java.io.FileNotFoundException;
2 c2 S( G- ^- |' |/ Bimport java.io.IOException;
. J. P4 @- q$ z( O3 M- y7 Uimport java.io.InputStreamReader;
4 x  `+ U7 ^+ rimport java.io.UnsupportedEncodingException;0 w! @; Z* w' O. |; Z# J
import java.util.StringTokenizer;
% K: r# K) H, a& J4 T1 T8 cpublic class TXTReader {
" }& p4 q$ x1 y; }$ z% w$ h5 \ protected String matrix[][];. k" e7 X- U, \
protected int xSize;* s0 n0 E; Q; {
protected int ySize;& J3 N& r6 k' B
public TXTReader(String sugarFile) {' R8 B: l: x5 h! o" I% j2 X
  java.io.InputStream stream = null;% w  k4 N. N2 j7 Q6 e2 Y2 q
  try {7 R6 f3 b8 y6 `# ~
   stream = new FileInputStream(sugarFile);7 v, R: L; u& J) v
  } catch (FileNotFoundException e) {% t2 n. |2 d" ?1 Y
   e.printStackTrace();1 D$ _/ F5 B$ C9 b+ A* f: Z
  }
  t" k1 z: R) f1 k: v  BufferedReader in = new BufferedReader(new InputStreamReader(stream));2 ?: N6 n- `3 z6 \  q6 r
  init(in);
! i' G9 m1 C5 Y* C }
/ C+ m* f) a1 q4 { private void init(BufferedReader in) {
, Z# N5 ?+ M/ @2 p  try {
( q: G& q' t2 F  r, j+ P) _   String str = in.readLine();
: A% }* `4 I, T3 Z   if (!str.equals("b2")) {5 r/ O1 d* T" ]( _
    throw new UnsupportedEncodingException(
9 Z. Z% {- o6 l" t/ p8 q& G6 o      "File is not in TXT ascii format");: T$ B5 L2 l0 j2 ]
   }. o: O8 v+ K/ P: f
   str = in.readLine();9 X3 x1 U* q2 f/ p( L/ b
   String tem[] = str.split("[\\t\\s]+");
1 m9 T( @) n, R, F/ f   xSize = Integer.valueOf(tem[0]).intValue();
( n9 k& p& F/ r, }2 w5 j   ySize = Integer.valueOf(tem[1]).intValue();
8 @/ X9 J/ i8 L+ @! j1 @   matrix = new String[xSize][ySize];- t/ e9 O' c8 O- [, L/ ?% b$ s
   int i = 0;
/ Z  j: o2 ]' ?, T& {8 r2 O   str = "";
$ k9 g; L' Z5 @: n- O& y   String line = in.readLine();( D5 j& v' Y# X  ^# O
   while (line != null) {0 y& @0 k0 V+ g. S* w9 i
    String temp[] = line.split("[\\t\\s]+");
  v; ]3 r1 r' L) Y    line = in.readLine();2 Q8 g2 Q# m( B+ N6 z( K
    for (int j = 0; j < ySize; j++) {
% Y# W! c0 I) L5 f4 @     matrix[i][j] = temp[j];; v& W. C& H  T  L, _; f8 L6 @
    }/ f  y7 [% U- J& M6 V3 b
    i++;
9 w  `* M% O) H   }5 T2 z6 _2 t0 d2 m* ~$ x
   in.close();& ^# ?2 |- @$ r1 J! u' X
  } catch (IOException ex) {% ~6 P1 v: c, a
   System.out.println("Error Reading file");( N  I6 [' q. M% Z6 z  F1 g" L
   ex.printStackTrace();2 P8 W7 O( c( v2 `' O+ B2 {; q/ ]
   System.exit(0);
$ I3 ~9 W) ^8 s+ S  }+ i5 ]1 V& g5 K1 _( p  V
}
$ W5 |* d& X7 z# o% N) l public String[][] getMatrix() {
0 f' T2 C7 ]3 S  return matrix;
. v  Z5 R; d9 c$ L }
) y- }; ^9 Q2 X* J6 F, v7 U$ y# X}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-19 08:39 , Processed in 0.016190 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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