设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9564|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;2 |( H. F" j) V  s  T' U8 S9 ?
import java.io.BufferedReader;* r# `& I) [% Y+ H
import java.io.FileInputStream;
& f# i! d: b! A& q& Uimport java.io.FileNotFoundException;$ i  w9 a+ {8 K8 z% f% f
import java.io.IOException;
) H  _  ]: n3 r5 M* n& R$ Mimport java.io.InputStreamReader;1 N4 C: H$ {% ~/ }1 k" C1 R
import java.io.UnsupportedEncodingException;# ^' O5 ]- J. v+ R& X2 I: a# D/ L* i
import java.util.StringTokenizer;
* p  M3 K) J4 E& ypublic class TXTReader {
5 N+ m- n& t5 u" [ protected String matrix[][];* R0 g2 U1 B* [9 O: D
protected int xSize;3 |& W0 B# @+ T) C
protected int ySize;& S0 x  n9 u) ~
public TXTReader(String sugarFile) {3 S( ]7 o' u& H. l% p3 J
  java.io.InputStream stream = null;! T* I* g  r# D: F! z$ x$ J
  try {7 u5 i: C- Y" l% n5 }( i
   stream = new FileInputStream(sugarFile);4 q- h$ Q0 t6 w8 n
  } catch (FileNotFoundException e) {
7 T: O# ]! `' t- f   e.printStackTrace();3 M4 ^# T% t" _0 T: F
  }
/ z: h3 _* B/ _1 d( e  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
$ H9 s' H; y; ^8 B5 X  init(in);$ B  D: I- y* Z. r) \
}! T& N! W; a0 j) h
private void init(BufferedReader in) {3 ~' b" d8 S7 U% P- K
  try {
: \/ u$ ?9 H8 @* j$ x, H   String str = in.readLine();
% [; v( q+ v# V   if (!str.equals("b2")) {8 b1 k2 B0 w) j3 g" K5 K
    throw new UnsupportedEncodingException(
" B' x. q3 H% T* o, a# ~8 G1 j* R      "File is not in TXT ascii format");
7 w, R& i0 }: F! I6 f% D# G   }
+ S: G1 Y3 B6 a8 Z: e$ A% y" C/ h   str = in.readLine();4 l8 a0 `! q$ {; b' L8 p
   String tem[] = str.split("[\\t\\s]+");  _4 \3 G: v6 [0 A5 L! h- [
   xSize = Integer.valueOf(tem[0]).intValue();9 d" B$ C1 E; ~% o  A
   ySize = Integer.valueOf(tem[1]).intValue();; F) L4 V9 g1 j) u* v
   matrix = new String[xSize][ySize];
3 \; L* _9 f# ?$ s0 v/ ~" B   int i = 0;( M& R: b+ s+ K+ p, G. V" X( a1 H* I
   str = "";& _6 T' M" e3 X8 w! o* |0 I5 Z6 B" Z
   String line = in.readLine();" @" r) ]% N9 {, @" y, P  k/ W! J
   while (line != null) {+ ?9 I& N! r: w$ u8 q8 K( G
    String temp[] = line.split("[\\t\\s]+");
$ N* e( a: P) r: k3 F    line = in.readLine();, [# p" g9 e2 t: w! I; I
    for (int j = 0; j < ySize; j++) {
* s- z* E3 ?9 N6 A, y     matrix[i][j] = temp[j];) V( b! o+ w, T
    }- m( Y! ?7 N4 x
    i++;
; W! X" |  N) ~4 R/ P$ ?/ J% V1 B   }( O6 M+ U7 j( c* K3 r
   in.close();
) j0 \6 z8 W9 w4 k+ G  } catch (IOException ex) {
$ M& I- x, K3 e- P( Z   System.out.println("Error Reading file");7 n+ C; i, m5 j: K  `
   ex.printStackTrace();+ |9 ]: U: d$ j/ h% I' |
   System.exit(0);
  a; ~1 s2 W! _& o  }
' {: d" t# u/ b) g) _ }
( E$ a* S7 L% c: ]2 t9 I/ n public String[][] getMatrix() {
6 A, Z/ h8 |3 d! v8 J1 B  return matrix;
' Z8 Z1 i( E) Y" C/ @% [ }. c* f+ y- r: W. q
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-25 00:21 , Processed in 0.012492 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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