设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10392|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
; L  ?% A2 }$ B3 H: b; K. h" Wimport java.io.BufferedReader;* N( n0 _/ ~% i3 H
import java.io.FileInputStream;/ v6 I2 G' r3 _5 H  y3 V
import java.io.FileNotFoundException;
" p6 v1 }, Q" m4 I* Wimport java.io.IOException;
9 Q1 H/ Y: X7 y; Simport java.io.InputStreamReader;$ ~1 }6 ?% j6 B* Q# I) B6 r2 t; ?
import java.io.UnsupportedEncodingException;
5 l4 N! [4 D$ l1 u: dimport java.util.StringTokenizer;
& ?& D2 _$ d  H% g% npublic class TXTReader {
) I. ~, A* l0 H protected String matrix[][];
; K2 n0 G: [- f/ C/ I9 c+ B3 N protected int xSize;7 v4 X; K6 Q, \; g8 d
protected int ySize;0 M/ M. _" e! {
public TXTReader(String sugarFile) {/ ^) l8 u- u" L" M/ E& z" Z2 J
  java.io.InputStream stream = null;. b# T. J1 o7 E% |5 `. ^
  try {, S8 m0 m4 _% m2 B
   stream = new FileInputStream(sugarFile);
) [% M: N# s  `  } catch (FileNotFoundException e) {: |# I: j3 ?7 O4 D
   e.printStackTrace();0 I6 B" O3 D4 V) P- R/ e
  }4 m! G- p- W6 B& d9 q
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));: m& _; L* T  b. W# y( {: _
  init(in);  [! @/ a9 I5 y3 G+ \9 ~
}
0 X0 \. a5 A% T private void init(BufferedReader in) {2 N9 o% y* h' X1 `2 ]
  try {
8 m) _. Y5 H7 @: D# T   String str = in.readLine();5 k' G- X& O5 F2 [( G4 \! f
   if (!str.equals("b2")) {$ P: `' U  \- e8 R
    throw new UnsupportedEncodingException(" x4 Y# Y9 X/ t
      "File is not in TXT ascii format");# V+ r( b0 [# [9 V# n& G
   }
8 z/ E1 r7 w- W' k  r   str = in.readLine();1 C3 E# B1 V$ J$ x+ ?; f' W+ |* p
   String tem[] = str.split("[\\t\\s]+");( u: }! N% ^1 _/ d) u1 K3 Q
   xSize = Integer.valueOf(tem[0]).intValue();
$ c/ u" ?' y8 i. w$ Y7 W   ySize = Integer.valueOf(tem[1]).intValue();; {- f4 T. m. c
   matrix = new String[xSize][ySize];
2 W' M- u& L7 Y- n' ~$ D- T   int i = 0;( d0 i: p, X) m0 }
   str = "";
, I+ @9 e) V$ N& I( l7 {2 K2 l; H   String line = in.readLine();
' `) ^/ L; H& t5 d# A& P. l" G3 }   while (line != null) {
8 }0 q" ~+ x+ z7 G9 ~5 b' Q! r    String temp[] = line.split("[\\t\\s]+");
0 i$ ^4 F& {0 ]- D    line = in.readLine();
1 _5 E, Z. V; k! g    for (int j = 0; j < ySize; j++) {
( Q. J6 l5 E. E4 g+ R     matrix[i][j] = temp[j];
" b$ Q  @9 Z3 o, |9 }4 {    }9 L% c( s6 s' H
    i++;! u4 \7 g" ^- u5 F9 ]" x, Q* T
   }
" ~  K& T: Y$ q" r, ?/ r   in.close();
# f  I( V2 t! u* r9 P# Q  } catch (IOException ex) {- ^1 l" e5 v. V
   System.out.println("Error Reading file");. u/ M% ^  v' A8 @; k
   ex.printStackTrace();
$ a. ]$ E% a+ o- a+ v$ \   System.exit(0);
; n$ u5 A9 W( _3 i; ~; D  }
( W4 y! \- F. g; r! Z9 k( }) p }- A) V' k3 w) O; D
public String[][] getMatrix() {# T1 C- J+ Q( x) R) a/ B% Q
  return matrix;5 z% i6 {* F( G$ f
}6 `/ \% D  J9 c" M  H) y2 |
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-7 22:14 , Processed in 0.014419 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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