设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10046|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
. J% J" B6 y( w* i& S: `import java.io.BufferedReader;
  D- y, J* x. L* ]: Q; dimport java.io.FileInputStream;" n* R" r! m' H% L
import java.io.FileNotFoundException;
2 M# v) v3 ^. K9 U4 T# K  @import java.io.IOException;
) x4 B4 z& G2 B/ nimport java.io.InputStreamReader;
" H8 B( w" \: F2 ]) bimport java.io.UnsupportedEncodingException;7 R, ~0 M; i% ]$ E4 J( l/ O
import java.util.StringTokenizer;
4 Q8 P. s( C2 H4 @5 V9 G( Epublic class TXTReader {4 ?! F$ ^  P6 d6 j
protected String matrix[][];
  n; ^$ X0 o/ J4 J. l( } protected int xSize;: ^1 b3 K3 f4 @7 _3 ^- ^: J0 h
protected int ySize;
$ b) z7 L$ l' Z6 o- K public TXTReader(String sugarFile) {
* y, A# X- ?: \  java.io.InputStream stream = null;0 F, j8 \# o% C  N0 A
  try {
+ p+ W2 T# X6 n- u4 m4 s# h   stream = new FileInputStream(sugarFile);8 E+ R$ J, z/ q& z. B4 u2 g6 a  S
  } catch (FileNotFoundException e) {
8 |% M" M' l, c$ E% H/ T6 S   e.printStackTrace();
: x1 z, v" O, y8 O" `  }
/ ^7 P5 ]: o  W- w+ a  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
- D7 q+ o% f5 Y* S: X% Y  init(in);9 {8 k2 m" p* X- H& O
}
: b# g* @( M5 o" P private void init(BufferedReader in) {
% b% u! S' w- A# t  z0 L6 \4 j  try {
1 Q; y+ L: P; `( {/ E   String str = in.readLine();
( R/ B( S' s% J3 K   if (!str.equals("b2")) {; @, R$ d% P' K) S/ p
    throw new UnsupportedEncodingException(
) ^1 F" s, D/ g- G& |      "File is not in TXT ascii format");4 [, S+ ]5 w( E6 {* P
   }3 x8 k$ @2 g: i* ~: ?! L1 D
   str = in.readLine();) j' Q2 G, _6 Y, A: D
   String tem[] = str.split("[\\t\\s]+");
, i( w# g$ _9 P1 x+ o   xSize = Integer.valueOf(tem[0]).intValue();
$ v% j; @3 k3 e7 C5 i; m* v   ySize = Integer.valueOf(tem[1]).intValue();
, Y' o5 Z. `: F; K! z& d( n9 ^' D   matrix = new String[xSize][ySize];4 [# E7 M& F  E$ ^6 t+ M; _
   int i = 0;
2 `' R5 F. ~- H; l# C* _& a   str = "";. I( t% |, z7 a  a! M' R) L
   String line = in.readLine();0 _& Q$ {: h5 c5 O+ t
   while (line != null) {. v) u; x# L- x- \- y, [4 b
    String temp[] = line.split("[\\t\\s]+");
% n! J, q* ]; \% n& N7 h; k  F    line = in.readLine();
$ q0 U9 M& P! {: T, r5 F    for (int j = 0; j < ySize; j++) {) n& r" h2 V' f4 K
     matrix[i][j] = temp[j];
6 B4 H/ F3 U5 r3 m0 I    }
( N- w( k/ H) s4 f* [0 G6 o    i++;
, q9 q' q: P  R! |6 j   }5 v8 c. L- Y( a
   in.close();
" d9 X* {" z7 B9 g# m  i% i  a  } catch (IOException ex) {
2 ^% r9 W% I' D: m+ I( [+ a   System.out.println("Error Reading file");
" [1 z# @1 p7 A   ex.printStackTrace();! v7 m. V: I. e( x4 B5 @
   System.exit(0);
9 s. N( Y+ K) H  }
" q# |0 N( m4 C1 y- ] }
% ?3 E% n) P% g$ D3 L# H public String[][] getMatrix() {
. ], H, L1 k  y& }$ j$ Z  return matrix;. [1 Z. L- u3 @9 I
}# k( s. C' o. l2 `* Q- z
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-19 13:04 , Processed in 0.017982 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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