设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6384|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;4 M: _2 j; H  h* U/ V0 i
import java.io.BufferedReader;7 y, j# t. g7 a+ _! ?6 _+ Y, ^, E
import java.io.FileInputStream;  Y- l" b8 R; K3 U9 y0 p1 T- U2 E
import java.io.FileNotFoundException;/ J- C; j1 P5 V
import java.io.IOException;
  p! p% V2 t- _! o! }5 U" b8 aimport java.io.InputStreamReader;
2 @( h! q4 S0 b. |" _5 Jimport java.io.UnsupportedEncodingException;
+ E% I# \$ i% H" yimport java.util.StringTokenizer;
) y9 o. B1 C9 z8 @public class TXTReader {
5 ]* Z9 P( a; o# J! `9 ~4 L protected String matrix[][];4 s! ]4 g/ J' ^. D/ g; s
protected int xSize;" n- D) x- |" Q1 R& c3 R
protected int ySize;; a6 _$ E: z3 R, q
public TXTReader(String sugarFile) {
, W9 R) X# V: T. w% Z" I) k. Z  java.io.InputStream stream = null;
  g% q# \, [9 q3 _9 W  G  try {
* [4 R6 G  p+ g% f   stream = new FileInputStream(sugarFile);. V2 ^5 Q9 P4 Q0 g( _3 S* i1 U, G5 S
  } catch (FileNotFoundException e) {
5 S1 R9 Z+ D+ M   e.printStackTrace();( y( J. {; |5 v, `' A3 k. r- b
  }4 h( X  o  e' s! \. V
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
. G1 W7 Y* }. M  init(in);
+ B& T' l- I, i( S }
- b: F* E& h7 s9 {$ f  ]3 ]& g private void init(BufferedReader in) {1 Q  I5 w2 P9 W. D( c6 a. m
  try {
/ {0 T+ @, l  d* T" K& h7 D   String str = in.readLine();5 N, c2 M9 F3 }5 U# j
   if (!str.equals("b2")) {. M5 j  M7 ~8 a* m% v1 U
    throw new UnsupportedEncodingException(
. C  w; S. \/ C9 Z" O      "File is not in TXT ascii format");
, d, @# t5 w5 T9 \0 I   }
4 ?, v* R7 G/ L1 Y+ s1 H4 U   str = in.readLine();) ~1 ~8 p1 _- j( h  D) r7 J
   String tem[] = str.split("[\\t\\s]+");. Q8 P" `& H& p8 O
   xSize = Integer.valueOf(tem[0]).intValue();; H  z! U1 ^/ y( U! S5 F; P
   ySize = Integer.valueOf(tem[1]).intValue();; y' W0 p2 y9 k3 t7 o- J( b
   matrix = new String[xSize][ySize];
, c! g, @6 p7 L7 O% u   int i = 0;
1 c& m9 w' E6 f2 Z( f8 F1 [! v' \. l   str = "";
1 R6 Z/ G1 l& }$ X3 [   String line = in.readLine();
" P$ j, i$ W9 T' h' e   while (line != null) {/ M$ D$ M4 _7 q1 y7 L/ m
    String temp[] = line.split("[\\t\\s]+");% P2 M# {* _% m4 p( r* p- ?
    line = in.readLine();
$ O/ r, Q/ N& E9 ^8 h5 Y    for (int j = 0; j < ySize; j++) {
0 B6 n7 \4 W# o+ X. q     matrix[i][j] = temp[j];' `3 v: m8 G, ]* r4 I2 [
    }5 n( J: q8 j1 M8 g* E; V0 O* @. @) r
    i++;. s6 _0 r. N0 A6 C9 U' a" M7 S
   }) z6 N  y# y7 j2 s5 |$ }9 }
   in.close();* T2 l. {; H/ f. K
  } catch (IOException ex) {6 Y3 H: h8 s1 K' X
   System.out.println("Error Reading file");6 |6 h+ R0 Z" U6 n
   ex.printStackTrace();! ?7 y* w, ?$ ]+ R
   System.exit(0);
! M  x4 J: E" ~( O: o5 N$ u  }1 J. H' v( i9 l1 D  Y" U# ]
}
. K6 O6 \2 \: {2 K) D0 t6 d) O public String[][] getMatrix() {
+ B* r# v* q3 H! T  return matrix;
, o% s8 y) \+ S0 u- M8 H9 Y6 v8 ? }0 e6 H6 ?4 Y; }! U% b
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-3 01:49 , Processed in 0.018259 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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