设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7245|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
0 @; J$ |( `: ~! b( bimport java.io.BufferedReader;
) p  Q, _( O' r) `& M* V* bimport java.io.FileInputStream;: d7 h5 {! d* z2 s8 k( P
import java.io.FileNotFoundException;' _1 r" T' a4 k+ \/ ?  x
import java.io.IOException;
! v1 Q) H, u- Z' jimport java.io.InputStreamReader;2 H; P+ M! s' z0 O5 V
import java.io.UnsupportedEncodingException;
6 K! u; j  c/ kimport java.util.StringTokenizer;& k0 F* p0 R7 X! S! F- [
public class TXTReader {
: C4 j) r3 n3 w3 A+ q9 R protected String matrix[][];
1 ]9 E5 e  Z( N( y protected int xSize;
, [5 j8 j7 L; A5 f9 M; l# W3 @ protected int ySize;" w: Z$ H! W* ^+ |$ E
public TXTReader(String sugarFile) {3 Y+ K/ ~, s' S) b* B6 v2 r2 `
  java.io.InputStream stream = null;
0 M( X1 n6 A% X: K; [  try {% ~# s8 \0 I! B) Q, {
   stream = new FileInputStream(sugarFile);
( k: p) ?, u% X5 H6 h# I( x( N  } catch (FileNotFoundException e) {% Z: P) N: L# n
   e.printStackTrace();
" H4 Q" I) q. C! V5 }5 e6 F' r  }
1 e* m# g: C! B7 ^4 Z" I  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
( k* G! T+ s, M+ L; d* o3 I  init(in);
7 W- K! G- ]" {8 y7 Y( c' r }
4 a; s6 h$ M( p8 T/ E private void init(BufferedReader in) {
/ ^, d9 K% U9 [  try {* Z3 [& o8 R8 }3 [, t: W* L8 F
   String str = in.readLine();  ]2 b" B6 A* l7 Y6 ]4 y/ U
   if (!str.equals("b2")) {- z8 |5 D5 X; K+ l9 [
    throw new UnsupportedEncodingException(
* I' q/ o; R+ k' K6 [" I      "File is not in TXT ascii format");& ~  {7 F, S3 v; C
   }9 d: ~) c7 T* m6 j; t
   str = in.readLine();0 B9 I. r: ^$ l) D+ v
   String tem[] = str.split("[\\t\\s]+");9 L$ J7 ?1 Q9 L3 I% j/ b0 o
   xSize = Integer.valueOf(tem[0]).intValue();
. |9 d( r' m% ^# g1 n8 C/ j   ySize = Integer.valueOf(tem[1]).intValue();6 @- b7 B2 ?8 }+ N6 {, m, d* W
   matrix = new String[xSize][ySize];6 a$ I# M$ A1 ], }4 i& P+ z0 w$ M
   int i = 0;$ o. k* T; s: O1 ?
   str = "";
- Z" R  C% B4 q; l! d   String line = in.readLine();; x% J$ p0 M/ O* B4 [  T7 p3 i
   while (line != null) {# g8 B4 @+ k$ }7 Y
    String temp[] = line.split("[\\t\\s]+");
* A$ }& [2 e0 W6 _( f5 A8 F/ Z7 s0 a    line = in.readLine();$ v2 {+ A* P7 C2 S* n2 d
    for (int j = 0; j < ySize; j++) {
' l( c# U- ~5 S5 |     matrix[i][j] = temp[j];9 F  g& k8 K+ p
    }9 Z( h/ B2 t7 x+ W" \
    i++;
$ y% U0 Y# ]% X& j; E   }: A8 C$ C$ t% c+ e1 [  T+ N; J1 W
   in.close();  t$ s2 X# ]7 y- p
  } catch (IOException ex) {) u4 G! s# S" q5 q; U! n4 E
   System.out.println("Error Reading file");
* |6 E  B/ |9 @8 p. N7 Q5 o% T   ex.printStackTrace();
$ {2 O. Q9 o' C2 f; W   System.exit(0);0 p. Y; ^* N  Z
  }0 H: }# J0 ?% n: S5 q  N) \2 o
}
* N$ p* S! \, ~) s4 U public String[][] getMatrix() {
) j" A" _8 }! }5 v- ?! j( u  return matrix;5 G7 n: Y9 n0 q, _
}+ o( v: Q8 V7 b+ J) c8 r5 A; {
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-2 08:03 , Processed in 0.018679 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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