设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7195|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;0 }' p: W" V2 m3 A; {4 U
import java.io.BufferedReader;) l9 b+ H) ^+ h5 I6 f
import java.io.FileInputStream;
  O" K8 c- M- `. E" v) qimport java.io.FileNotFoundException;
) J1 M4 c8 r. S3 M% m2 l4 b, N9 Zimport java.io.IOException;  ?5 D; T: J" O* m( ^% a
import java.io.InputStreamReader;
- K! ~* }2 p0 s8 l7 g$ Z+ `import java.io.UnsupportedEncodingException;
+ @1 Z1 [# O5 _import java.util.StringTokenizer;# c' _" b6 T1 Z; R1 K. r
public class TXTReader {
( u5 Q( Y1 t* [) U% ^4 K3 S. R0 Z! T protected String matrix[][];
( n/ N. A& ^( ]* E5 d( s7 _4 ~/ ] protected int xSize;
9 {+ [# [/ ]# X# o% Y& l* P. O protected int ySize;7 p0 s! a9 u* Z. t
public TXTReader(String sugarFile) {5 u4 {) t/ Z* ~; I  T+ W5 T* i
  java.io.InputStream stream = null;
" u" m1 M; B* V8 o0 k7 y  try {* W: ~1 C) ^% W  U1 D$ z( f
   stream = new FileInputStream(sugarFile);
: W! H/ l& G/ A0 k( Y  } catch (FileNotFoundException e) {6 y" M. o/ F3 w  _7 @/ y
   e.printStackTrace();' o; T* B0 [# [' o% F
  }1 t4 K: g5 I2 o& M7 z) ^9 X$ w
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));3 `8 D' O; c4 A3 @7 c8 X# L
  init(in);. L; B- C2 o6 [8 Z+ Y  u. I
}: D0 r% O3 X4 `4 D& z5 d; r
private void init(BufferedReader in) {
: L* A; ]& [, r" S. ?/ N  try {
% U4 C* }! u( W" D  {/ T- R& K$ S   String str = in.readLine();) x7 ^- R: j2 K: Z
   if (!str.equals("b2")) {
. U5 i& _) J: R5 H- U3 J; V4 `    throw new UnsupportedEncodingException(
+ m2 f6 }: h  `1 J+ c/ P& o  b- l      "File is not in TXT ascii format");
  g  `3 _0 a' l5 o* _4 F   }
9 C$ f- T$ ^! Z' w6 x. @   str = in.readLine();' V( w5 Z3 Z5 w* _! Y6 N: H8 O8 a
   String tem[] = str.split("[\\t\\s]+");; O, c" ?- {, O: S) ~* H
   xSize = Integer.valueOf(tem[0]).intValue();
" y- o1 w/ e7 v8 K0 G# _9 q   ySize = Integer.valueOf(tem[1]).intValue();. `: J) A6 j2 r4 b) y9 ^6 t5 X+ e
   matrix = new String[xSize][ySize];
4 |  G- @1 n; b2 I* C   int i = 0;
) ?# n& Y$ u- M) D6 |3 {# m   str = "";" G' H( a/ r0 @5 ?
   String line = in.readLine();
6 c9 a" E5 r/ m, Q0 E4 n' u   while (line != null) {. V2 ~4 N: }+ D+ u5 E& O
    String temp[] = line.split("[\\t\\s]+");
4 ^" g7 f- ?2 `% L5 L1 w. A# S    line = in.readLine();
7 H, v6 o$ m1 n    for (int j = 0; j < ySize; j++) {
- ?/ g6 T( d" e' b& }     matrix[i][j] = temp[j];
7 x8 [; `6 r# }0 \    }
. Z% z# Y4 p# A  M' ]& T- [    i++;
4 ?$ Q! s* q3 M1 S9 |! r/ x! ?8 ?   }
- Q5 F  o4 E3 _3 _" `   in.close();* k3 o) S& S! p' C& W. D
  } catch (IOException ex) {" Q& d( _+ K1 F  {
   System.out.println("Error Reading file");: j# ^- z4 n5 W  V8 F% t
   ex.printStackTrace();7 v( {& Z4 c3 k6 D8 ~2 d
   System.exit(0);
" K$ p) Z8 f# a- H4 t  }9 N+ E5 E! C% I1 q3 a9 E( F- @
}
, @" ?0 m) {( S0 H% {2 a, Z public String[][] getMatrix() {
9 Q9 W( e7 t. g& C  return matrix;
: _- U$ I: v. T1 q }
$ ]4 v# p4 a7 p2 G# c}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-27 07:11 , Processed in 0.020835 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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