设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8095|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
; ]3 V& o  R) q' G; Aimport java.io.BufferedReader;& a2 h6 P1 y9 I
import java.io.FileInputStream;
1 W; |, Z' m; D6 r- f& F) W- uimport java.io.FileNotFoundException;
) U* D& D( L8 d7 Q9 v; Nimport java.io.IOException;  o! `4 o2 c% P3 }' J
import java.io.InputStreamReader;
8 L3 l/ O, ~! C/ ~* ], }# ?- G6 A, |import java.io.UnsupportedEncodingException;+ T3 g. @6 X7 g" O! R
import java.util.StringTokenizer;
+ r' m; {0 ~% ~& I; h% Z( i/ zpublic class TXTReader {
; `; v6 a, W5 f, Y( c$ @% w( w protected String matrix[][];0 a: |' E! a6 q1 p, G# V
protected int xSize;! {; ?0 d) Q6 [
protected int ySize;
  j- t7 }7 L5 i9 t9 S0 H1 { public TXTReader(String sugarFile) {2 }! o1 N- j, f( B- ^% q
  java.io.InputStream stream = null;
8 V( Q5 R; _# {) X' y  try {1 b  }( F# j* a# B- z/ s
   stream = new FileInputStream(sugarFile);. ^  m/ P0 d/ o
  } catch (FileNotFoundException e) {9 j- l7 A2 k8 C4 Y2 f$ ~5 L
   e.printStackTrace();
: I: c4 m, ]# d, G9 z  }
7 a! b4 ]$ v. S. ]; s  BufferedReader in = new BufferedReader(new InputStreamReader(stream));, ?* R' w" M7 [* ^( X6 B' z  n
  init(in);
5 K/ W0 q1 s3 y( s7 _9 g }
' n& d" q' p+ j9 X5 e1 w- s% n private void init(BufferedReader in) {; J+ Y; f  z' Y! `8 R
  try {
: [$ B7 o6 f  C+ A   String str = in.readLine();
+ y0 ?2 |$ p7 B7 l& a9 F- q9 c   if (!str.equals("b2")) {
+ G+ ]2 C5 v0 b2 r1 A9 [0 n    throw new UnsupportedEncodingException(4 f+ v6 A- ^; K0 ]2 A/ m( F& ^
      "File is not in TXT ascii format");
  X7 D, l% v3 D% q( d   }. k  @8 T* i; Z
   str = in.readLine();
3 b2 _! A* Y$ F; s   String tem[] = str.split("[\\t\\s]+");
) r& H* K% T& i# v9 V' ~5 V   xSize = Integer.valueOf(tem[0]).intValue();
  [- K( Q2 {/ ~4 A$ E6 `   ySize = Integer.valueOf(tem[1]).intValue();
+ m; W7 v7 w$ Z/ g9 h   matrix = new String[xSize][ySize];
0 a0 C8 N4 U/ b   int i = 0;
# A+ h( C( W# G) u# P! T   str = "";- u+ r9 A: D. b" p) z8 E
   String line = in.readLine();* o8 x+ q! h6 j3 a; u- L
   while (line != null) {+ d5 @1 @6 K) n
    String temp[] = line.split("[\\t\\s]+");
0 j- F% ?" e/ Q. u3 F8 c2 l    line = in.readLine();
" G  O3 v& N1 y7 G4 ^) U; c    for (int j = 0; j < ySize; j++) {
) O8 I$ `7 D" h+ |1 e! W" u     matrix[i][j] = temp[j];6 f6 y4 y* Y# K2 H+ z& a
    }
6 j1 N  S, ]* I; m    i++;  _8 P" }' k! g  P8 k& G2 E4 j) ~& c
   }5 w4 @8 k0 H" }0 t% s; h' O( z
   in.close();8 M5 t  w  ~. @3 n+ l+ s
  } catch (IOException ex) {
1 }6 f# t7 y& @) _+ W, q2 N   System.out.println("Error Reading file");
( ]" }" h9 l0 }+ h, @$ T   ex.printStackTrace();
6 j3 Y2 c5 \. y" b   System.exit(0);
3 r  o: |0 |' w5 t" b  }5 [0 l" H! x+ S# _5 U! |0 u
}
% z8 z( z/ {- q. u public String[][] getMatrix() {* R5 c* m( i+ d5 m3 @/ b4 G9 _
  return matrix;
- V: R, Q" C/ a }6 k6 n3 w5 R( _: K
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-2 01:41 , Processed in 0.017544 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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