设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7187|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
2 @( k1 R0 J  C* aimport java.io.BufferedReader;
% N4 o' S3 T9 [% t- vimport java.io.FileInputStream;
8 r% g7 A7 L8 `* `import java.io.FileNotFoundException;% U0 s5 S( D9 j6 C( D
import java.io.IOException;
. u5 m4 t, C; k- c- yimport java.io.InputStreamReader;' y& j* l6 A( D: t9 |
import java.io.UnsupportedEncodingException;! e" p* S8 h9 X. P8 J/ ?
import java.util.StringTokenizer;6 M- s! E+ s, `! {. a" N1 a" m' Q
public class TXTReader {
4 b& ?/ e, _6 ?+ E6 z protected String matrix[][];7 c+ S" b2 `; V+ u- x
protected int xSize;
( o6 I6 d8 F3 H$ m7 ]7 k1 | protected int ySize;
. \+ M! W# t. M public TXTReader(String sugarFile) {, p2 U! u" e+ ^
  java.io.InputStream stream = null;
7 K, i9 i! C& |9 `! i  try {
  Q, r  L8 e9 z9 d: ~3 z& i1 r   stream = new FileInputStream(sugarFile);% u# M7 \% C7 F# Y: r
  } catch (FileNotFoundException e) {
  S* W( Y; \4 q: b0 Y8 n3 C* ]: F   e.printStackTrace();
+ d4 c6 f: B& a6 T  }% W& ~* k# {% ?
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
) ?5 b2 i3 ]5 g9 s  init(in);" [: k" c- Y! E" y
}6 r# g" I9 @6 `# o
private void init(BufferedReader in) {6 J- d' I+ f. U8 C/ C# o5 F
  try {
6 s$ s! Q3 ?4 D& @0 P  p: a/ y   String str = in.readLine();
: ~$ o, h" m7 B) l% R) t7 e   if (!str.equals("b2")) {
& ]4 W) k) [" w; x% W% _% K    throw new UnsupportedEncodingException(
# D7 ]6 I5 s6 @2 K      "File is not in TXT ascii format");
. W9 y4 I, j3 w" z5 K- k   }
1 {7 X5 ~. `3 z   str = in.readLine();; e& \: I& ?% L6 r7 F/ [1 _
   String tem[] = str.split("[\\t\\s]+");
$ S1 Y7 f0 M2 ]! @4 _6 A5 \  F   xSize = Integer.valueOf(tem[0]).intValue();5 k2 C/ K+ M! F* |7 y- Z
   ySize = Integer.valueOf(tem[1]).intValue();. J, h7 U) Q* \1 J# J
   matrix = new String[xSize][ySize];% \1 u0 P1 Z0 R4 \2 W( z) Q
   int i = 0;
2 F+ ?4 t  M. c* g   str = "";  k0 _- I- o+ M9 R- S! A% ^5 k/ c
   String line = in.readLine();
- D$ T  A. y/ w- N, e8 l   while (line != null) {
" M) n% V& W, `    String temp[] = line.split("[\\t\\s]+");
9 Y1 Q% |) c5 `- Z# w    line = in.readLine();
2 @; k: g6 n+ n8 Y1 ]- p* @    for (int j = 0; j < ySize; j++) {
2 p  y6 R0 W6 R$ }. K- D6 n- O     matrix[i][j] = temp[j];
& O+ C2 \. e* Y    }
7 e% s- V- i3 v2 k, ^    i++;3 h( j7 F# t& b% v0 H* i) s4 c
   }
- x4 f; c2 ~) q; z3 A( H6 r4 @, l   in.close();
6 [5 ]! M2 \8 x4 I9 Y  } catch (IOException ex) {3 x* J# g, R  x5 b8 I+ q" G
   System.out.println("Error Reading file");. G4 J* C  a( p# n2 f* c( ]7 f0 s: z
   ex.printStackTrace();3 \) [( x! d) j, W4 O% T
   System.exit(0);
* L+ d: C5 m! ?/ a7 f  }
5 r$ D$ q1 T% P: T9 u }
3 n6 O7 @& O  g8 X) @ public String[][] getMatrix() {
1 t7 y0 u1 {0 K% {, L! i  W  return matrix;
4 z  D/ S* i3 _) {1 y }% u; o- F  ^7 K
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-26 14:24 , Processed in 0.020737 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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