设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5839|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
% I- R0 x; n+ Rimport java.io.BufferedReader;
; W/ L5 k. X; R" k' @, Fimport java.io.FileInputStream;
4 I% y! E0 J3 T" b1 j3 limport java.io.FileNotFoundException;2 v/ R) w7 ]; S- x  E
import java.io.IOException;1 F$ ~- H4 G8 J2 a  P
import java.io.InputStreamReader;
  a. ~# |0 C/ b. B" i0 ~7 ]import java.io.UnsupportedEncodingException;% P( x4 V9 p& O+ e
import java.util.StringTokenizer;
( b4 O6 I5 X& {6 ^' hpublic class TXTReader {
% l+ X- C5 \, @5 L6 ?6 W) ~ protected String matrix[][];+ F* b/ N' i8 \. b. H* y; F" }% N
protected int xSize;/ y) q* O: w! J1 y
protected int ySize;% P! h* b% x3 f- i- l
public TXTReader(String sugarFile) {
3 c, @( w% K2 c7 L: M: M4 E  java.io.InputStream stream = null;
( _' p6 f. R" P9 |4 t2 _; ]7 n0 S  try {
* Z4 x% C( p, \/ m' l5 U8 }9 ^6 `   stream = new FileInputStream(sugarFile);
) D+ ]/ V2 f5 O8 i, i8 u; }  } catch (FileNotFoundException e) {7 w5 q4 L+ C0 d* V
   e.printStackTrace();) h& i3 [9 S, s6 o% D3 z- {& A# A
  }
9 ~; B  N# H3 b) X) o  BufferedReader in = new BufferedReader(new InputStreamReader(stream));7 S" Z, S' Q  v) K& n
  init(in);% I9 W8 o3 |% _& J5 l0 t( {' G
}
! N9 b$ s9 k0 u: d- D4 o private void init(BufferedReader in) {, y2 X5 w  S+ {5 \
  try {9 U# {8 f) e8 L/ A
   String str = in.readLine();
) P. V4 e1 ^" N9 Y( D. b6 P9 x   if (!str.equals("b2")) {6 |, t- V0 N5 Q+ A
    throw new UnsupportedEncodingException(
& Q; S( m: X7 {      "File is not in TXT ascii format");
. u6 O1 }9 ]' w   }6 A6 Y7 {1 g+ Q( ]
   str = in.readLine();
( n& l! N5 w- `0 s. }: O& h   String tem[] = str.split("[\\t\\s]+");  {( e# Z, m) E8 H, Y5 g
   xSize = Integer.valueOf(tem[0]).intValue();
5 Q$ O' h% z& a   ySize = Integer.valueOf(tem[1]).intValue();
6 D5 }+ H$ D. U, D3 i   matrix = new String[xSize][ySize];
! W9 r  g5 S: l% n7 r  v. l) Q   int i = 0;  y, V$ v+ C8 B3 s
   str = "";4 ^* N# W& O8 c: l
   String line = in.readLine();
1 P0 z6 }+ b" @0 q1 j   while (line != null) {0 z( L. X  c. J0 p/ U; I2 O' a
    String temp[] = line.split("[\\t\\s]+");
5 ~. y# E" V9 E. Z& p' z: P: j    line = in.readLine();9 s& D/ l( N9 `- ?+ @. i: x1 P
    for (int j = 0; j < ySize; j++) {6 x6 ~6 J8 O/ `7 w
     matrix[i][j] = temp[j];
$ ?2 H. S+ W( y% G  L; N    }
7 V2 E( v9 g( {3 _5 }8 Z    i++;
- H+ \) L% L" G- P   }1 g' [. J+ @" @2 @5 t* E& a
   in.close();
. \, f! k5 ?6 z4 Q! I7 {  } catch (IOException ex) {$ a" N* `$ _+ x" a' {
   System.out.println("Error Reading file");3 ~# w! @. x! v: ]1 Z* X
   ex.printStackTrace();
6 u1 w1 Z; g- W* j6 Y5 f* P   System.exit(0);4 t3 w" p7 n8 }8 @* U0 h/ o; R$ Q
  }7 `& S( E! f" L* F& o0 S
}  i$ H  p: l# ]& u
public String[][] getMatrix() {5 I0 G% M. G2 N6 {4 W9 g* i
  return matrix;
9 ]  ?5 ?; U/ X2 x }9 V5 u" a* n( I. w( q) N, J6 W
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-25 13:58 , Processed in 0.016167 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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