设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8508|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
  z8 O! Q* L% n! w% mimport java.io.BufferedReader;
% v  F4 j/ a. w5 Y$ F# y( ?. S6 oimport java.io.FileInputStream;! c3 M3 ^5 p6 ?5 W. i; w
import java.io.FileNotFoundException;
* _% n9 {' w5 e5 Z) ^import java.io.IOException;. A7 w) I+ ]* P% L% G8 ^
import java.io.InputStreamReader;
( z# Z) L6 R: [  R' m1 I7 Kimport java.io.UnsupportedEncodingException;
) b$ a# G* B. }0 Y1 O" ~import java.util.StringTokenizer;
; x. t- R) x4 V3 v1 u2 Epublic class TXTReader {
6 @+ X! C, r; e0 E3 ? protected String matrix[][];
" ~: j6 D1 T8 E" q protected int xSize;5 U' d2 V& M( Y3 C
protected int ySize;5 B: N  a% e3 ?) n+ E" V' O
public TXTReader(String sugarFile) {
) ~8 ?# B, |6 A% x5 P  java.io.InputStream stream = null;& B7 f' o, C, b& h: C$ K
  try {0 p# J" o: k3 H6 @& f0 b
   stream = new FileInputStream(sugarFile);
" n, L3 W  K2 M- q$ E& }$ W  } catch (FileNotFoundException e) {
( l' O. U2 |& n! D$ s   e.printStackTrace();! v, W. E' m1 A1 j
  }
6 q! d8 z% I3 N. J9 B* H- |  BufferedReader in = new BufferedReader(new InputStreamReader(stream));6 e% B( h5 j- Q; [) I, O
  init(in);
7 E" y- j; Q' y' Q8 {6 m; l }' u8 R1 r$ d1 x. o9 ~: ^4 a
private void init(BufferedReader in) {
, }4 I6 I% O7 D  J7 C  try {
* A! h% _2 J0 z5 z" t* N1 k   String str = in.readLine();, F  _$ P2 q$ W- G* ]9 f
   if (!str.equals("b2")) {
/ E5 ]% z; D4 X, [. _    throw new UnsupportedEncodingException(
9 X/ B) q% P6 m8 h      "File is not in TXT ascii format");; N' b: u  Q" z  }) p7 X0 i
   }
# j0 C* r1 c6 b6 x9 E" t* P   str = in.readLine();2 m5 C3 X( C* n5 n( |
   String tem[] = str.split("[\\t\\s]+");
2 M) G8 {5 d! N% }$ \) c   xSize = Integer.valueOf(tem[0]).intValue();
, P% P4 r3 H. U1 _, O& m- t   ySize = Integer.valueOf(tem[1]).intValue();
$ ?. ^! e6 O' z   matrix = new String[xSize][ySize];& f2 |" F" l" f+ z/ _
   int i = 0;
" F& S2 r( ]/ {/ M) q& Q9 C2 m   str = "";% p* W" j, x+ b& G5 O
   String line = in.readLine();" e2 e: B1 Z4 f! i2 p; V5 v
   while (line != null) {
) I" X4 D7 w: Q; `1 ]    String temp[] = line.split("[\\t\\s]+");" o) C' Q/ J& A, Z
    line = in.readLine();9 J" |0 K. n6 b$ Z; Q9 T
    for (int j = 0; j < ySize; j++) {
; i7 P4 }# C/ @" @0 z. v     matrix[i][j] = temp[j];
4 F7 s, a! h* s' d6 X4 ?    }' T0 Y, e8 n/ r/ f" x# d( i
    i++;  C5 c; \: R0 i8 ]3 O% C, `8 J
   }, e' L) R/ H. D; E, J- P5 w
   in.close();3 g  Y# h* h4 K2 j0 {
  } catch (IOException ex) {- `1 C" j2 K2 H
   System.out.println("Error Reading file");8 f! e) T; o: _) h+ X2 l. X
   ex.printStackTrace();
8 M9 \" Z6 a; Y- I5 @   System.exit(0);0 i. z  u5 c2 l& U
  }
8 _, @: l3 i3 K! j4 R/ T+ f }; F4 P* k, Q4 M  N/ K2 R% N& V1 l2 t
public String[][] getMatrix() {' p4 s) h  n; c0 T$ j$ _0 a
  return matrix;3 I$ P. Z: _; |3 k
}
9 C5 N3 `& I2 |, r. B}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-26 23:01 , Processed in 0.016223 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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