设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6439|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
( r& \- ^6 N2 }import java.io.BufferedReader;$ j) T4 J3 _) i" B
import java.io.FileInputStream;
$ B! }: ^4 o) L$ J; L; Bimport java.io.FileNotFoundException;
+ R+ u- v  Q! m# K  J' q3 Ximport java.io.IOException;  U& h7 C& D' y. f6 v
import java.io.InputStreamReader;
7 \; \5 s2 ^9 W4 q) a1 Jimport java.io.UnsupportedEncodingException;0 y3 n, H& L: B
import java.util.StringTokenizer;
' B$ J: x! J' ?, h7 P2 s" e: tpublic class TXTReader {
  w/ [" o2 i4 m( m; Z protected String matrix[][];+ H9 }4 V* X# F
protected int xSize;7 F+ y# w$ F1 ^
protected int ySize;2 m. L" w/ \/ ~- W/ g! [6 \  q' R
public TXTReader(String sugarFile) {
' S% j0 N7 V; s8 Y, G8 n, U4 l  java.io.InputStream stream = null;
. A( b6 K/ W7 y- h! H  try {
/ p. R. U5 h) U   stream = new FileInputStream(sugarFile);3 G) `: u$ C4 L. P0 T& \
  } catch (FileNotFoundException e) {
$ M$ W9 E7 F* a* S5 I( Q; s   e.printStackTrace();6 o9 {2 _# M) N0 ~2 F
  }" Q& N/ J+ h* C5 g& H+ J. a
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
8 T' t! E3 V4 _3 j7 t  init(in);  Y1 w7 m, W( [, }& j3 G! x& v
}
. I5 E' B1 m) b  ~ private void init(BufferedReader in) {
$ V  I# C, Y1 ^4 v7 D* |  try {
0 P/ u9 @% f; z   String str = in.readLine();
. x+ L3 h) w9 K4 D) Q* n   if (!str.equals("b2")) {/ U* Q7 o. B3 Y
    throw new UnsupportedEncodingException(& S0 I5 h2 K! V: F" n
      "File is not in TXT ascii format");
% s2 J& k# M: p4 D$ R0 {! A   }
6 U1 {* o) Z( E6 l3 Z  O$ G+ y   str = in.readLine();
2 O) P2 `0 k* [; ]: f# E6 F   String tem[] = str.split("[\\t\\s]+");4 y7 P3 {. o- n" Z
   xSize = Integer.valueOf(tem[0]).intValue();
7 ~$ D, E2 I3 N1 `# ~% a   ySize = Integer.valueOf(tem[1]).intValue();" j: v) a6 B$ F1 C- R
   matrix = new String[xSize][ySize];
( e3 H# k) F7 p; s6 ?+ r2 M   int i = 0;+ ^7 A- \' v- o: Q% P- f5 Z
   str = "";
" B. ]8 T/ |. l! o$ {6 |   String line = in.readLine();
/ l$ H# g5 G- m- y, D   while (line != null) {$ C* i6 b4 U2 Y0 u9 O
    String temp[] = line.split("[\\t\\s]+");
7 Q" s7 l9 o/ ~    line = in.readLine();
  G- A4 j1 ]3 C" F: f    for (int j = 0; j < ySize; j++) {
! `+ {/ f( ?2 G( d/ P, c     matrix[i][j] = temp[j];+ K. j3 l4 n" S# e
    }
7 x" p. E7 D- p    i++;( F! O& w  O, P, C' P' A5 g) ]
   }
$ @7 q- {0 t4 m- w* l: e7 r   in.close();
3 p; M4 \  N- y& j1 Z* |6 T  } catch (IOException ex) {
: Z3 @- [# V6 a7 s/ F   System.out.println("Error Reading file");# h; `1 e7 X: ?1 V8 o
   ex.printStackTrace();* G. v3 Q! P! f' c/ p5 J
   System.exit(0);
* G( A" Z0 M& u- }9 R% G  }
& o; {7 k5 B. Q0 f2 ] }! @5 n! {5 C2 _6 m9 d# h% @
public String[][] getMatrix() {
& V$ ~$ {* a: H! N6 W! q; |$ P/ c( h  return matrix;/ ^4 M8 k9 {0 q1 T2 V/ f
}" f+ g8 h3 o9 J2 o: c
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-11 13:39 , Processed in 0.022030 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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