设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10033|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
4 x  Z% `& _1 S6 j- x" n$ e% |import java.io.BufferedReader;1 }) n: x7 p+ }: X  u/ D* s
import java.io.FileInputStream;
( t' J( x( v8 `: simport java.io.FileNotFoundException;: n4 l' o3 t# M# N/ `
import java.io.IOException;  b6 q8 O/ y8 g( e
import java.io.InputStreamReader;* D, E8 h& k+ U: z; k  p) l
import java.io.UnsupportedEncodingException;
- k9 b# {& a( qimport java.util.StringTokenizer;
  {5 s, J: F( G5 o, f' Bpublic class TXTReader {& u2 ^/ P4 x- a
protected String matrix[][];
* [' \/ m( f; a, M: C protected int xSize;
  m; E+ U, f$ n- [) q3 E# } protected int ySize;- S' V# m3 N8 p& X, y& o$ O* M8 n
public TXTReader(String sugarFile) {+ i# \/ x: P0 m8 k
  java.io.InputStream stream = null;7 p3 l& ?$ e" H7 I3 k& O  Y
  try {
& c9 w- f2 {5 d. W; {7 V- x   stream = new FileInputStream(sugarFile);
. p: C: Q* S# r  } catch (FileNotFoundException e) {) @7 ?6 \) ]6 N+ l  M
   e.printStackTrace();" l4 E2 O5 \3 t! U
  }3 N' B9 |9 C5 Q- t8 D
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));" T: X/ Z1 W# n5 I3 k! _
  init(in);
& ?3 f) `- J' _8 R! \! J/ Y }
7 _% y- X( M; a9 c# K' I; ~# N6 `) ` private void init(BufferedReader in) {8 T* v8 @" z8 I5 D
  try {
5 W. ~0 |# s9 B6 G: o% U$ _1 F7 P   String str = in.readLine();# i% q' Q  r% v( Z: a
   if (!str.equals("b2")) {
! _# b6 o5 O  y6 ^4 b7 r    throw new UnsupportedEncodingException(
: x  s: [6 _6 t/ u9 c      "File is not in TXT ascii format");
  v8 W2 V8 \' n   }" N- I+ H4 G6 C
   str = in.readLine();, H; N- K; z5 @) R
   String tem[] = str.split("[\\t\\s]+");
9 t6 O/ A. u2 o# g   xSize = Integer.valueOf(tem[0]).intValue();
+ U( z7 M  j! K; S1 i   ySize = Integer.valueOf(tem[1]).intValue();; [2 b* M0 j6 E) u' W+ _
   matrix = new String[xSize][ySize];$ z4 L$ d; \$ V- k  T7 j
   int i = 0;' C; l8 F- _" y* z
   str = "";
/ p0 R; J" U* ^6 Y) ^   String line = in.readLine();
& ], R4 c( v) U- {: f" W3 x# y   while (line != null) {
, j$ Z0 C9 y3 a$ q) t$ I    String temp[] = line.split("[\\t\\s]+");
1 w- s, ~- l$ r5 \! ], o- {3 l    line = in.readLine();
* E5 x; ~" b. G    for (int j = 0; j < ySize; j++) {
$ N" {0 A5 B$ }: X     matrix[i][j] = temp[j];. ^) d% C$ A. Z8 M' {
    }+ R, |- d1 q' v5 p; S# M' j
    i++;8 D4 ?+ r7 D$ Y( O
   }
7 Z7 z3 r5 |# _7 i: f$ [   in.close();
# ]& S6 ^: n! z  } catch (IOException ex) {. l  j/ M3 [* I3 X7 e
   System.out.println("Error Reading file");( K8 c8 V. b/ U
   ex.printStackTrace();7 T( H0 Y5 Y( a9 R
   System.exit(0);
3 ]3 @, `4 P, \  }
% M$ M' B' `1 _ }. j8 r! x& Y4 m# ?' |, z) @
public String[][] getMatrix() {
" j0 ?; f2 W/ Y" d2 M  return matrix;6 n, A  n* L& `$ h6 p  E
}
! F3 K! f# d! O6 S. ]8 k7 _}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-18 00:34 , Processed in 0.015971 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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