设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8868|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
, ~! h2 U6 a0 C; Nimport java.io.BufferedReader;) Y$ a# [+ y. z: \
import java.io.FileInputStream;! e. L* d8 Z1 V) B  E5 B; ^4 [
import java.io.FileNotFoundException;  |! k" [# L2 b
import java.io.IOException;
! ^) |$ a/ I* Z$ F  t1 N$ T1 Z5 mimport java.io.InputStreamReader;* f- q* d* T* f
import java.io.UnsupportedEncodingException;
' r/ B4 c$ g0 @import java.util.StringTokenizer;7 Z! D9 k8 r& W
public class TXTReader {
( f3 d0 X  V, F* F# R% q protected String matrix[][];8 T8 e3 R( |" R$ p" W: Q
protected int xSize;- L  y4 V- z8 m1 G  ]( v& C  @
protected int ySize;7 `4 ?  Y: p& z; U( u6 g1 R3 h
public TXTReader(String sugarFile) {
, w" G2 m' Z; T  java.io.InputStream stream = null;: o! y  e. c" M/ Y1 n
  try {; g* y; s$ u" ?* o5 P8 R
   stream = new FileInputStream(sugarFile);) y& d. U3 J% J3 m# S
  } catch (FileNotFoundException e) {
% @- I8 u' M0 ^9 w8 [' o   e.printStackTrace();
, Z# N$ E: _% O& g" a  }. [' S* V; k& J0 e# j
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));$ f) f0 H4 u+ Y! u) v# v
  init(in);
. V+ Q! {1 M* c' z) p6 E' }1 q# ~ }- x. J; X7 f" i2 y/ E9 Z
private void init(BufferedReader in) {" S9 ?% x6 H) s$ Y0 ]4 @. U+ t
  try {
2 d3 `5 d3 F2 j& K   String str = in.readLine();
& w" R1 g0 x7 ^8 U   if (!str.equals("b2")) {
; g" y. ~% M) M2 A- r    throw new UnsupportedEncodingException(6 d- f* o  j5 ~# d
      "File is not in TXT ascii format");
/ K1 Q) K7 g& B8 A2 D9 W: H. |& \* V   }: V6 \2 z. B, ^+ b3 {% ?, l
   str = in.readLine();2 P2 S/ O/ @" o9 S" T; p
   String tem[] = str.split("[\\t\\s]+");( D; C  O4 n5 S3 ?. R5 f3 L9 g
   xSize = Integer.valueOf(tem[0]).intValue();9 P2 }# o. E+ A  a1 x% a7 ~0 ?& w- h
   ySize = Integer.valueOf(tem[1]).intValue();! {! ?6 r. _2 e* v
   matrix = new String[xSize][ySize];: @) e5 o5 ?% S4 Q! q2 A1 D  ^& G2 E
   int i = 0;
! t- I. H: ?- H. y- Q) J2 U4 }, z   str = "";/ T4 G3 @; k4 K9 J4 q( H4 X6 j
   String line = in.readLine();; _& e; C* R& F  t, @! T  ?7 b
   while (line != null) {7 v6 t$ ]0 ]6 K* A6 w: v& M5 I" C. H
    String temp[] = line.split("[\\t\\s]+");
1 D; b! s, m4 j- D/ L# i    line = in.readLine();
; g9 i/ X- i* K8 c0 Y  U2 B    for (int j = 0; j < ySize; j++) {
4 b0 l  h1 V( ^$ z7 t( a  r) t7 H     matrix[i][j] = temp[j];
9 `: i2 T, _1 j    }. x* z5 |: Y0 g/ X' K( ^
    i++;( j4 b" v/ v/ D4 b9 D2 n
   }" G' P- P8 y9 t# P0 C* s5 j' ?2 G3 q' ?  o
   in.close();
0 m  ^* J# A8 M  h! X# c- h$ T  } catch (IOException ex) {2 [! @+ A3 ?' q" C( M3 c
   System.out.println("Error Reading file");
+ [+ f6 W% m$ b% @: |   ex.printStackTrace();
+ g) R* N2 X. F) w7 T% F* W   System.exit(0);
5 E9 g5 u9 j2 O  }
. C2 v" K1 Y* d }4 K2 l+ B% C% J9 @$ N
public String[][] getMatrix() {- a( Y& C! h. D
  return matrix;) s' `* U( U8 q( L$ ~8 y; [
}; a* d& S3 C( \& E# _* K- H
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-17 22:31 , Processed in 0.016527 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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