设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5416|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
  g$ m! _7 R, |7 Simport java.io.BufferedReader;
; b: h# e% a7 K, Kimport java.io.FileInputStream;
, I& ~; I, c& }9 a; W. Cimport java.io.FileNotFoundException;  R0 I& A5 K7 b2 t
import java.io.IOException;
) T$ S8 y( I: P( j- I* c" k  D. _import java.io.InputStreamReader;
0 M! q' I. y/ uimport java.io.UnsupportedEncodingException;
1 F9 P) c1 p: m  q+ Ximport java.util.StringTokenizer;
; M, t  t4 y/ F# T& }+ A. M5 Apublic class TXTReader {4 e& K# ]  h4 L  s6 h% T- p( ?
protected String matrix[][];# o4 I1 b& B3 J  `8 Z8 h& K: |
protected int xSize;
4 @9 ^; B7 O2 j% t protected int ySize;
, N, A+ e) \, } public TXTReader(String sugarFile) {8 S# M- g+ U7 }& n& [* B+ `
  java.io.InputStream stream = null;# c/ a4 _& d7 U. b0 f( Q4 j% a
  try {" d& y; j& P+ N& n# m" ]
   stream = new FileInputStream(sugarFile);: ~9 r8 t# T: n6 ]$ I
  } catch (FileNotFoundException e) {/ Q% R7 G4 ~# d3 F: N
   e.printStackTrace();
$ I0 C& o$ |( G& G  }/ I/ M' L1 W4 m. v) j9 W1 @! z; N. |
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
, i0 ~6 w( ?5 V; k  k  init(in);, D. f2 ]) Z+ x, R
}: ?$ g( R; C6 H6 J# G( o
private void init(BufferedReader in) {
7 x9 x/ P0 N; r7 X9 ^" l  k% [  try {
; M/ O  r1 G3 p1 q. }% U   String str = in.readLine();% f- ?8 H; a3 [5 h1 z) N3 H: P
   if (!str.equals("b2")) {, c9 ~+ }$ O0 k+ y1 T
    throw new UnsupportedEncodingException(: c, M- C. a% ^
      "File is not in TXT ascii format");
! ~9 E5 C9 X5 ]# V- [6 z& u   }& ~) B  C  }* I) Y+ M" s' {
   str = in.readLine();" a1 T" ?, w- Y- c0 [9 x6 w. r
   String tem[] = str.split("[\\t\\s]+");8 V+ j; G: I& y4 ]
   xSize = Integer.valueOf(tem[0]).intValue();$ A% ^, c% x" d, d" j: g- p  q9 y
   ySize = Integer.valueOf(tem[1]).intValue();0 u& Z. ^& ?! X8 L; R1 t
   matrix = new String[xSize][ySize];* }* p7 ?$ W! b) U# D
   int i = 0;
& N" o8 z/ |' l: j9 y$ u: d* e2 G   str = "";
/ ?- E, ?8 X& I0 b% ?$ D4 K  m! d   String line = in.readLine();
  n. X, q  D+ M% c- q   while (line != null) {
6 Y  @! {( U% I; r2 A' X# E, h    String temp[] = line.split("[\\t\\s]+");
2 b2 w. V5 N. Y% O    line = in.readLine();
+ B/ C/ \- Y0 ~! H% B    for (int j = 0; j < ySize; j++) {
: o4 c+ o# f  u) M% _     matrix[i][j] = temp[j];
& _1 A  R5 `7 y. j) r8 c. ]6 r" f$ c    }
& v3 i. m3 Y# ?* f! \! J, Q    i++;: f; f7 F/ |% k/ ^
   }. g0 K+ q+ m0 Q) c
   in.close();0 L, n. A( q; {8 ~' u1 ~( b& _
  } catch (IOException ex) {
3 }5 ?+ g/ x7 `2 j% z   System.out.println("Error Reading file");
) K6 f  z1 ?1 V: V4 D   ex.printStackTrace();  [+ w4 a, R- G+ D+ l0 S( t
   System.exit(0);" l% u9 V$ s4 k$ [
  }
% `( ?2 Z1 B2 C& W3 P }# Y9 m; o" ~& k( K5 A5 Y  `
public String[][] getMatrix() {
8 t7 ]4 E7 K4 o2 f4 G  return matrix;
& u% e4 `5 n. r' q }
5 k* A- C2 R* c, Q}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-10-21 11:46 , Processed in 0.019415 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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