设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7620|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;4 }) j3 S/ V# Y) j( A
import java.io.BufferedReader;4 u8 `3 c4 u# {. \( c! }/ p% X( g' w
import java.io.FileInputStream;
% {9 F* m0 H/ t/ `import java.io.FileNotFoundException;
7 p) x" n) I" |' Ximport java.io.IOException;2 l, B: d6 ?6 N
import java.io.InputStreamReader;
+ q) Z5 b) h: c& D9 e7 _* c: |! X. limport java.io.UnsupportedEncodingException;
2 f/ _5 t' A  Rimport java.util.StringTokenizer;
( D) o5 i" d' c/ e* B: f3 G9 z% N2 j9 lpublic class TXTReader {
, H8 W1 k) U2 O protected String matrix[][];7 [5 k( O9 B! x1 Y  M
protected int xSize;+ N( o5 C$ _( |3 F' G- C
protected int ySize;
7 c& |  X9 D& F8 H public TXTReader(String sugarFile) {* V' A3 ?' @5 u. Y9 G9 e
  java.io.InputStream stream = null;
' ?; d2 @, t9 b) w: W. A  try {0 P: ?+ E, }  W6 E
   stream = new FileInputStream(sugarFile);- p6 s1 ]4 S& m6 i3 a
  } catch (FileNotFoundException e) {4 [2 F+ `6 O( O/ K* {: n
   e.printStackTrace();
* p# [1 W. H, s  l! ~) ^  }5 }; [- H4 U2 c" h$ `- D/ f2 D7 X
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
' x9 m; L! ~: C7 g0 E+ A% F$ z  init(in);
: X8 }& W) e  [) a }
) \! }2 t% F; \* u private void init(BufferedReader in) {. h4 K9 [1 V7 [/ v
  try {
, O0 I( n  l! x& M2 O* j6 U   String str = in.readLine();5 ?& i1 X! \( N
   if (!str.equals("b2")) {5 P0 k# d5 Q1 `
    throw new UnsupportedEncodingException(! {0 u5 p# D. d7 G9 x
      "File is not in TXT ascii format");
9 E8 ~/ Z1 B& X3 w% s4 o   }$ |% ~, v! N) O# n- K! o5 o
   str = in.readLine();  h+ |; C3 F; A, _6 R# f
   String tem[] = str.split("[\\t\\s]+");$ F4 ^) X" e; k
   xSize = Integer.valueOf(tem[0]).intValue();
! o8 N; K+ I0 |' ?/ G   ySize = Integer.valueOf(tem[1]).intValue();' _  f0 a  }) j4 s
   matrix = new String[xSize][ySize];' }$ v( j' n6 d" a0 p% D
   int i = 0;
9 G( |  Q- X- M   str = "";
$ M. T" _7 G0 R" u' d4 M, n   String line = in.readLine();
, r  J, S) [) k8 ?6 O( o0 f   while (line != null) {
$ c1 {2 g( s! w* b8 d( i. K. p! |    String temp[] = line.split("[\\t\\s]+");
) u4 T! x4 r1 d- Q: y    line = in.readLine();
' c, v- Z' D' ~( `* h    for (int j = 0; j < ySize; j++) {
$ ~) y, u4 j8 S     matrix[i][j] = temp[j];8 l2 f# M) M2 Z& s# ^( z
    }! |$ l: I/ R7 d! C4 R2 O0 z
    i++;
! f5 l2 B+ K$ x   }
' O, K! h5 P4 ~% t4 r/ R   in.close();
% k# e" ?' A& b4 I* C, u  } catch (IOException ex) {
! d, q* f) g- Y! T" u1 d  ~) F   System.out.println("Error Reading file");
/ e( p1 S# }9 \2 O5 T# o   ex.printStackTrace();
# {* L, ?. w' Z   System.exit(0);
2 U! f! y4 N( k  }$ e2 s* }) m! v  _7 p* O% ?
}
( A) b& {3 N, h" `( y public String[][] getMatrix() {
- h, R! g+ r  P) r4 m* F  return matrix;7 G8 W5 Q3 Q$ I6 l7 [% E" E5 j- |  b
}
* ]: \" u' E& `5 W" ~}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-4-2 02:53 , Processed in 0.020680 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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