设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6274|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
7 k; W$ @5 f) H$ o* T0 z* Z4 R$ r' bimport java.io.BufferedReader;. y9 d3 U/ g# M, k& l
import java.io.FileInputStream;4 B; }. [6 |: r" |1 y7 [& l
import java.io.FileNotFoundException;
+ O: }) ]- k# b* timport java.io.IOException;
2 @! T: ^  W" u* d" Wimport java.io.InputStreamReader;
4 C+ s  Z/ b; ~. p  Aimport java.io.UnsupportedEncodingException;
( U0 ]; j; q7 i0 t  N* U# {2 kimport java.util.StringTokenizer;
; Y5 [* Z$ d) x" C' t0 i3 Gpublic class TXTReader {
; y. J) l' I4 P, \4 T9 n7 C6 X protected String matrix[][];
0 |/ s% W1 h" g- }" ~8 ^; @ protected int xSize;
, j0 `' m, j3 e: d" |! y% q protected int ySize;! b3 W9 U1 q7 u
public TXTReader(String sugarFile) {
6 R& c3 A" }% J  java.io.InputStream stream = null;% K! O$ I5 A" k! t* Y7 I
  try {5 s% Q1 U4 v0 z4 y  g
   stream = new FileInputStream(sugarFile);
9 ~8 i4 H. k" V  } catch (FileNotFoundException e) {0 r* Y% K0 M: T# z4 A+ M3 P
   e.printStackTrace();& [# C+ k: ?# X) P9 E* A
  }
8 [# t6 z$ p" V2 c  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
  a9 E4 ?7 g  [. u" c# f% A  init(in);
0 o: h9 j/ {8 D4 s0 s+ L }
* |0 _8 p$ {2 i9 S% m private void init(BufferedReader in) {3 r4 k, k) K  W7 t6 p- ?) E
  try {
" r# {, l( q4 f; q   String str = in.readLine();) A) i& ]$ |! P) S( g
   if (!str.equals("b2")) {! c$ M8 r# {. h& q" c4 J
    throw new UnsupportedEncodingException(
, B5 Y: y" [/ P2 z8 I. g      "File is not in TXT ascii format");' K2 F$ ]$ `" t3 s
   }
# ?" y. F# a7 m( c' y4 n/ g   str = in.readLine();, x+ n9 V* a! K; h7 I. D# X
   String tem[] = str.split("[\\t\\s]+");2 f) G( o& ^/ R
   xSize = Integer.valueOf(tem[0]).intValue();
1 K5 ?+ d' _$ n5 _( i) l   ySize = Integer.valueOf(tem[1]).intValue();
7 M/ Q+ S& [7 c* h& X   matrix = new String[xSize][ySize];
3 ~/ E1 Y) Q2 Q9 d6 L$ Y   int i = 0;3 S+ k6 N/ ^" z$ y/ J) x# f
   str = "";
8 |  F4 p4 N' z* @1 {   String line = in.readLine();
/ b( @: ~3 s5 q4 {, ]/ b   while (line != null) {. V) t, U. [& p2 H( o3 C9 \& \
    String temp[] = line.split("[\\t\\s]+");
- c- j4 H2 F1 O5 R* N# T+ t8 c    line = in.readLine();
6 B( ^3 C/ C: y+ M    for (int j = 0; j < ySize; j++) {
( q/ D- y; \' d/ N     matrix[i][j] = temp[j];
2 J: _! S, ]4 n' ~. l* r    }
  d+ F+ d2 ^+ h  n    i++;
7 A  G3 P. w' _* }+ S   }
2 K4 W8 ]9 m6 T/ d: I  X7 s0 S% }   in.close();, N: j7 |2 H" U, d( h2 t5 b" D
  } catch (IOException ex) {# \" P1 O. E- q# H: H% w4 \
   System.out.println("Error Reading file");
. j! m- @" J; B$ m   ex.printStackTrace();
2 y7 a* N, m; p. M& p8 E   System.exit(0);% j; [7 F6 e+ F
  }
# |6 Z3 a) S2 i# h- M }
8 {( w- t6 _8 s public String[][] getMatrix() {
1 n; h9 {3 `* s) H' a  return matrix;, c7 F. _8 x1 T8 d$ E& r6 r
}; v$ {0 Y' f5 D- I1 S8 R6 Z
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-21 22:31 , Processed in 0.020391 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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