设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6812|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
" }+ l4 j( u& @9 F2 u" Fimport java.io.BufferedReader;4 T$ `% W) s2 J, d5 b1 ^6 ]
import java.io.FileInputStream;8 f1 k; d, A9 U) r* K" i
import java.io.FileNotFoundException;( `# r  K9 U" d9 G/ P7 I
import java.io.IOException;* C" E* D  T+ g0 s3 q
import java.io.InputStreamReader;
  q9 i6 O5 H4 u: Wimport java.io.UnsupportedEncodingException;
- Q0 o4 B1 K" Limport java.util.StringTokenizer;0 V2 E2 G2 C+ ^) p" R6 N( w9 a
public class TXTReader {' Z/ [% Q6 T0 k% N9 }# E" g* ]
protected String matrix[][];4 z8 x1 M2 F; k" g
protected int xSize;
- P5 n! c/ W1 h protected int ySize;$ r6 J* v4 L; @' n! Q+ V, y
public TXTReader(String sugarFile) {
9 P* j; p- b0 U  ~3 `' c  java.io.InputStream stream = null;
  |# \) @4 K. X# S& l* N2 P  try {& [. V* i. U5 C3 T  t0 m
   stream = new FileInputStream(sugarFile);4 w7 }$ t( o- D2 \/ k; q: P; y
  } catch (FileNotFoundException e) {% j: e" ?3 [" C/ U& Z
   e.printStackTrace();
0 m4 l6 _% V, E8 A, Z; l  e- u  }
1 g8 c% [! w: p  BufferedReader in = new BufferedReader(new InputStreamReader(stream));3 z' Y; m1 Q$ c" ?* y, e; L
  init(in);2 `' ~$ l- {4 A- W
}6 P  J0 k& e! ~
private void init(BufferedReader in) {% q0 _; p0 ~$ B+ q
  try {
* x7 f# z( _7 n* o   String str = in.readLine();
2 i2 R) ]6 }' j5 R1 p7 a8 ^   if (!str.equals("b2")) {1 C: E5 ]" C3 N0 W
    throw new UnsupportedEncodingException(
, c0 M% K9 w2 L0 |% R4 `# D8 a      "File is not in TXT ascii format");& K2 O- d+ W  W7 ^3 J: j
   }6 _* ?1 s2 A2 k- x
   str = in.readLine();
9 c& v; z% f6 C1 g$ _* e7 v   String tem[] = str.split("[\\t\\s]+");
3 v) X9 f) U4 t$ M+ ], @# Z   xSize = Integer.valueOf(tem[0]).intValue();
- y! ~# t7 r0 z/ G( v% C2 @   ySize = Integer.valueOf(tem[1]).intValue();  f* b  C3 V5 g1 e2 K( k
   matrix = new String[xSize][ySize];
* L7 [7 |' S) ?9 w+ x* f: a0 r   int i = 0;1 ~/ B; T' w. A5 z- s
   str = "";
, O+ }& T. A! @% A: K1 X   String line = in.readLine();
% [  w' a% A6 o, E. h; ]   while (line != null) {- q3 [4 Q% h  i' Q9 ~
    String temp[] = line.split("[\\t\\s]+");1 N$ p+ S; D* j: l- t7 ?/ g5 N
    line = in.readLine();3 c+ J( ]7 j& F' }3 I3 I% X$ {
    for (int j = 0; j < ySize; j++) {
: ?7 v2 ^- X9 }4 |6 n" E* s     matrix[i][j] = temp[j];5 z6 n4 `9 R3 c' C
    }2 i) Z0 Q( }! B
    i++;
( d' S3 Y, S  q0 M, U   }
, w8 C5 h( Y# W6 U& @$ z   in.close();
5 g, i: v) ?7 ~# n5 }  } catch (IOException ex) {
: e% S8 c4 K! a   System.out.println("Error Reading file");6 G/ y5 j6 W( y
   ex.printStackTrace();
  q. e  l" X/ G& G8 L6 V$ T7 {" ^   System.exit(0);
7 W3 k! h% G( i9 f2 H  }& D. q1 g# d+ ?* K9 Q
}. `5 A9 V7 |$ N" P: x5 H+ M
public String[][] getMatrix() {
+ W7 Y) q9 I! M0 y- b$ u  return matrix;, X( J! ^4 e6 M! i* k, T
}
8 l4 _* e3 r3 N' k3 l2 |}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-5 07:33 , Processed in 0.020359 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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