设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10597|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
2 _- i6 E5 e! N4 z4 Ximport java.io.BufferedReader;
  |! ]; C0 R/ \5 y% Y+ Fimport java.io.FileInputStream;2 C- y  ]8 H! e
import java.io.FileNotFoundException;
; R4 ^$ M& k" v( O, }import java.io.IOException;. S$ w, `1 M8 d" M1 n- i
import java.io.InputStreamReader;. ^: m6 O2 |) F+ x
import java.io.UnsupportedEncodingException;0 \& _" t$ E( k: E  @2 U1 ?1 P) A
import java.util.StringTokenizer;
& h( q! W, ]$ t* C1 d" upublic class TXTReader {& D/ C& [+ I  B5 f
protected String matrix[][];) O! w7 M" k; R( n! A2 W  X# V
protected int xSize;
- `, W6 p0 H  s3 P protected int ySize;
. b; C1 f5 b: l- ^& [; m public TXTReader(String sugarFile) {
" N+ F9 N" k  T. x% w3 m  java.io.InputStream stream = null;# @5 S+ K3 ^! S
  try {! ~7 J0 ]9 y9 o' w/ z, Y
   stream = new FileInputStream(sugarFile);, v3 t4 A0 U; J5 t
  } catch (FileNotFoundException e) {: a5 u  ?3 ?6 h' k: Y
   e.printStackTrace();
# ]- D% e7 z& R' x  }
9 g- J% z. k2 C2 |) L/ w  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
0 I9 s( }! u, t  init(in);
' O$ V5 L) f, Y5 }8 f' T# Z3 A }' u3 C3 J* R/ }( K
private void init(BufferedReader in) {
' P" ~0 O6 L5 O  try {7 b( o% y3 R# u5 `* y, y
   String str = in.readLine();5 R% B. y* m2 ]1 O9 p9 f0 S
   if (!str.equals("b2")) {% _. y% n0 ^( t1 o6 f8 I9 A
    throw new UnsupportedEncodingException(
4 u, o  |2 q* L& H) Q3 C      "File is not in TXT ascii format");
, n7 \+ N* [3 L# |2 X8 t   }
( P# b0 N; V- L/ t8 C, O   str = in.readLine();4 x* a. v( i! v
   String tem[] = str.split("[\\t\\s]+");1 z- T% E, }7 I; m) Y# A
   xSize = Integer.valueOf(tem[0]).intValue();
. o4 N; K0 x% R9 }9 ]! q   ySize = Integer.valueOf(tem[1]).intValue();" m' j: X/ ?) {1 w5 x
   matrix = new String[xSize][ySize];% R4 J6 z& @) f4 k1 a# @
   int i = 0;1 ^: Y; L! y: R, `/ ?
   str = "";
" M1 l7 @5 A! A, Y2 z   String line = in.readLine();9 Z" L. L/ y4 [) c& T( H' U
   while (line != null) {7 D; R* ]+ D0 o9 p# n6 S
    String temp[] = line.split("[\\t\\s]+");$ Z. g4 T, P% w$ n9 y- o- m
    line = in.readLine();
' g# ~7 m& _- a, p    for (int j = 0; j < ySize; j++) {7 D! k% c6 t0 J" Y
     matrix[i][j] = temp[j];
. v0 z( O2 u. c- B    }
% D' a- O2 k! G! h# z    i++;2 K5 Y# G* Q! j! W5 t( V
   }
( s% d# V/ E  r7 o& K   in.close();
# w4 M6 l4 z0 x/ |& R4 x& q  } catch (IOException ex) {
" F# `, [0 H* h5 W% T  Z- B/ ?   System.out.println("Error Reading file");) }+ A$ S" O  t- ~
   ex.printStackTrace();- m1 h& ^) S0 V
   System.exit(0);
" V4 v% s6 f) r+ p+ r  }
) O# i  o5 n' A' A( k+ X }
& ]- C: p9 p8 O! L' ^) _ public String[][] getMatrix() {6 p1 N8 q/ I* b
  return matrix;
/ g! q1 I2 g2 _ }
  ]' I3 V! A9 b- b}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-18 15:03 , Processed in 0.014098 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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