设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6291|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;7 O  B# c6 l6 i% B1 e' f
import java.io.BufferedReader;( _0 W8 m4 L& J7 i: n  |
import java.io.FileInputStream;! J. V: H( x" g7 t5 [3 p+ v  S; t
import java.io.FileNotFoundException;8 {' @9 R" Y* @7 P! @$ T6 i3 \
import java.io.IOException;5 k/ X8 {+ [. z5 t" x; }8 [, _4 d# _
import java.io.InputStreamReader;
& X! o( w: ~; F3 limport java.io.UnsupportedEncodingException;' N; Q$ }5 y9 ^" H- _% w5 M
import java.util.StringTokenizer;8 V/ B+ Q3 s; B9 J" V  O+ C! I
public class TXTReader {
. l" ~2 u# M* \- _7 r. C( i protected String matrix[][];
8 A! j$ N+ n, N5 R( L protected int xSize;
& F2 b' Q8 E5 B' p* F& [ protected int ySize;2 s  z! A* g% M& p  R
public TXTReader(String sugarFile) {7 n7 b4 j! ^) |, X, b) f8 t3 }
  java.io.InputStream stream = null;
# _2 T6 E( I$ d* R0 R1 w  try {
0 n, J4 u+ Q: U- G, V   stream = new FileInputStream(sugarFile);6 B3 ?7 B. Q; z& `
  } catch (FileNotFoundException e) {) l- M* I5 b1 B
   e.printStackTrace();
, B2 J0 A; K% K  }
+ u  r7 ]- f9 B! {% a1 m4 D: X0 L$ A  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
9 @7 O. ^% Y* r) ~( }) o, k  init(in);
3 N# ], p. C' |  E' v }
4 I2 L: w1 a& A private void init(BufferedReader in) {3 d* l6 w( a& U9 O
  try {
' k8 Y1 y. p- c, @1 O" X# H   String str = in.readLine();
. B( p  v2 h" c+ T- l0 z) k8 I   if (!str.equals("b2")) {
8 M) b5 `; a& ]0 {5 F    throw new UnsupportedEncodingException(
3 l7 D, Y. _6 U* Q- o4 E( H) X      "File is not in TXT ascii format");
9 T, f9 N2 M/ I3 s   }
( \4 j4 `! B+ J6 S  u% P, P* b4 S   str = in.readLine();* W. q% P6 l' t& v. Z
   String tem[] = str.split("[\\t\\s]+");' O9 D6 Q% F& X& b/ b& D- y; A
   xSize = Integer.valueOf(tem[0]).intValue();3 ~! \+ t4 |8 k9 w- x1 p, [
   ySize = Integer.valueOf(tem[1]).intValue();
6 p  j. @8 @3 @* ]: c* w# s   matrix = new String[xSize][ySize];
/ N# A$ w) `# C   int i = 0;" h) k& k) ~& l
   str = "";$ X0 q3 A1 V3 I' c
   String line = in.readLine();7 X/ R5 U/ T+ Y6 V: [% U
   while (line != null) {4 n4 F+ r# h) Z1 P
    String temp[] = line.split("[\\t\\s]+");
6 R) L, s9 C7 Q& d2 k& W    line = in.readLine();' {% J6 P5 n5 F3 N, ^. m5 h: W
    for (int j = 0; j < ySize; j++) {
! l8 ]1 G$ A$ t$ g5 ?     matrix[i][j] = temp[j];
$ P- }) n+ O0 u4 m5 a: L  p    }
* @* U" Y! L2 S! [* o& z. p    i++;- I8 e6 t/ }8 G  N& N
   }
! S8 k; ]6 D. o8 H- K8 L   in.close();
3 O& i4 R: d$ t  } catch (IOException ex) {
' J+ K3 ]' n0 ~# L& r3 L. T1 a   System.out.println("Error Reading file");
6 N, l+ d4 o7 B2 ?   ex.printStackTrace();* @3 R6 O4 U& {8 J* b* q  H
   System.exit(0);, s* W: g8 Y" P& ?$ L( w- {5 l
  }& ]! n* Q7 e0 s. h4 x1 L
}
# ?4 K6 X% Q$ T, L7 M3 M8 x public String[][] getMatrix() {
& U, d5 d  p6 z. \" x1 r7 B  return matrix;" |; R" f- [/ f% x# g; F
}) |/ n) w* x* G2 W3 ^3 n, B
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-23 07:16 , Processed in 0.017164 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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