设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7145|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
* \3 ~! I  I% U7 Pimport java.io.BufferedReader;
- U$ V/ k9 H% f  mimport java.io.FileInputStream;
2 z( |0 \- S- l$ ~% U/ eimport java.io.FileNotFoundException;
* j5 \& A/ h) [. Z( _  R3 Pimport java.io.IOException;
! K: g2 G1 F1 K: eimport java.io.InputStreamReader;
* e. s" {' Y* P; |import java.io.UnsupportedEncodingException;
% ?# X6 P1 g) ]5 Q& yimport java.util.StringTokenizer;2 ]+ c, `9 Q3 L! U
public class TXTReader {
. v) L# W6 I! }/ { protected String matrix[][];, Y% Z7 ?2 h+ O' U) i$ z- b
protected int xSize;( M3 X/ O5 q7 k2 O2 G1 f
protected int ySize;
% N+ v5 @' _$ c public TXTReader(String sugarFile) {
9 k& y# }$ I' ]* f9 r* t: e  java.io.InputStream stream = null;
+ _* k5 ^1 \6 b  try {
5 @, x, e  M9 n5 ?( c   stream = new FileInputStream(sugarFile);6 B) A+ f  R& r+ U3 z2 p
  } catch (FileNotFoundException e) {
- C1 k# u$ p, U% h   e.printStackTrace();
2 @: @: o4 h! g: ]( q5 t$ c  }' a5 ~2 a' r$ j( K& ?5 G
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));) _9 l  W- U% k* Z' }
  init(in);9 N1 W- n8 R4 \# R5 }
}
+ q$ Z3 R7 x/ T- P6 b8 X% l private void init(BufferedReader in) {, d& D4 N: `" ^# ~* O
  try {
5 F/ ~! F% |8 G% R   String str = in.readLine();
8 j3 Y2 h" I2 O5 ~( I1 E) h7 \( k   if (!str.equals("b2")) {
3 w# t  H% x! L3 _    throw new UnsupportedEncodingException(
) X& x  s  @( p      "File is not in TXT ascii format");
* u( u  n% ]/ A7 Q% w5 G! I9 b/ m   }
; C+ U" O8 I6 @# A) p7 H   str = in.readLine();
; h( Q; \8 j4 S" c   String tem[] = str.split("[\\t\\s]+");
. |& }* v2 J- s. O* ^# S1 H- U. P   xSize = Integer.valueOf(tem[0]).intValue();) q8 d5 ]- r" \' x
   ySize = Integer.valueOf(tem[1]).intValue();
7 W& f& K" V0 u& Y   matrix = new String[xSize][ySize];9 z5 v6 V- l" P% ]+ N) r2 u
   int i = 0;9 y( H6 K/ U& i9 M
   str = "";
2 G* p; r( D3 j& k3 }- M   String line = in.readLine();
* {, i: W% E- Q. J   while (line != null) {
1 Y1 n+ u; c2 L% |1 [    String temp[] = line.split("[\\t\\s]+");! C3 u* ]- d0 D* \9 W
    line = in.readLine();
1 s& ], D7 o8 S) G7 _# s    for (int j = 0; j < ySize; j++) {
( ]: n6 p/ Z% y. k- A. a; H" }     matrix[i][j] = temp[j];9 A& J4 a- W* Z, T' ~
    }$ a: ^, F: M# R# p
    i++;
0 D; R" X* q$ w7 g+ h( _   }# j7 O4 X  J- R# P& ~
   in.close();
! k) |3 l, t: X2 F  } catch (IOException ex) {3 v. Z' N; ?( ^; t- U! t6 B
   System.out.println("Error Reading file");- f. s0 p$ `* `$ b: b
   ex.printStackTrace();
  {7 S) A; w0 H% a5 [- w8 o/ R   System.exit(0);7 H, w7 m- T* \, V8 Y* a
  }
1 o8 D5 Q  C/ k( |" K, _- u, g* V4 _ }
0 z" Z) x( Z/ G public String[][] getMatrix() {
4 f4 v* \( l1 i; [* Y/ J5 n. c  return matrix;
/ p/ t% f2 m" y6 ` }+ v' {3 {5 p# Z0 x
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-23 17:59 , Processed in 0.014287 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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