设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6773|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
/ a/ j6 A- G6 v$ j4 z9 J! ^9 pimport java.io.BufferedReader;
2 F. c. a/ W: y6 h# k9 @/ pimport java.io.FileInputStream;
0 T+ ?$ G' q* `3 ?import java.io.FileNotFoundException;
# r/ o' E3 H# }, z5 gimport java.io.IOException;& ]; b3 z8 i+ D0 X6 ~# ~2 Y
import java.io.InputStreamReader;
. Z1 t3 T/ B" K6 {$ a( }+ qimport java.io.UnsupportedEncodingException;: r8 E5 m( |7 [2 A5 Y! Z/ E
import java.util.StringTokenizer;' a% O) q  a3 ^
public class TXTReader {
* a8 T+ \8 }' n6 n  t protected String matrix[][];
# x5 ]0 v; a6 A protected int xSize;/ T6 M" H8 k' T+ b+ z+ _
protected int ySize;+ a- B7 r" H/ U; w, x6 \( x. n
public TXTReader(String sugarFile) {
/ N( \  i& ^) p$ ?  java.io.InputStream stream = null;
- q$ \: i$ O- Q% }2 \  try {4 a+ D$ B* `4 h) q7 t* J
   stream = new FileInputStream(sugarFile);
1 y4 P" r  i- Y/ `( B) R% l9 ^4 t# O  } catch (FileNotFoundException e) {2 y; z& y& g3 |$ \
   e.printStackTrace();; N4 a1 }5 }: S/ v; D
  }
/ v1 w* D* ^6 A0 _$ k  BufferedReader in = new BufferedReader(new InputStreamReader(stream));' a$ s; @6 S' p' S
  init(in);
/ N7 O4 [# e8 V: ^ }7 f! n7 O  U$ C: I& g
private void init(BufferedReader in) {
1 j/ |" o2 q$ h  try {$ Q* k; e! G7 K0 E2 N, P
   String str = in.readLine();
& e1 ^" Y( t6 V& x4 N   if (!str.equals("b2")) {
( j7 C* s9 i$ U3 R* A& x    throw new UnsupportedEncodingException(, P/ v$ J  k! v1 i
      "File is not in TXT ascii format");1 i# X9 ]5 c% ?9 n, l# v! f+ U
   }
. R; A/ o5 c/ p; A. B; _   str = in.readLine();: g$ a3 C& }- F2 o8 b  s
   String tem[] = str.split("[\\t\\s]+");
! w: U& {3 Q  N* r9 n" S   xSize = Integer.valueOf(tem[0]).intValue();1 {; M# [* H1 O" q
   ySize = Integer.valueOf(tem[1]).intValue();
# K7 _0 A( A: O/ k   matrix = new String[xSize][ySize];2 r: t4 @! h9 [
   int i = 0;
* k1 m/ T5 r, v* k   str = "";
- }; p* J3 ?  e9 ~" E  X   String line = in.readLine();
" w; W, d7 J) ?) X   while (line != null) {: ~1 x" M4 o7 k3 x( T. Q- n0 j+ N
    String temp[] = line.split("[\\t\\s]+");
8 L( H! s1 U2 R- m1 S    line = in.readLine();- [0 t; U0 v" O# g
    for (int j = 0; j < ySize; j++) {
# T; ?5 i. s/ M; c: f% _) l4 _     matrix[i][j] = temp[j];
' F* S. G0 x$ t) ^) _+ ]9 o7 s    }
- @: L2 o7 n: y  v  L    i++;0 ]  m3 Y+ x  D
   }/ R. i: K2 [! F% g7 m- o
   in.close();: [" s3 E8 p( T
  } catch (IOException ex) {
+ A8 G6 |5 d' P( {: c! d% t   System.out.println("Error Reading file");9 L3 I* q: s* U- z8 L+ {
   ex.printStackTrace();
+ m& I7 _$ h3 D) I- t$ k* u   System.exit(0);5 ?8 H: ~; k. ?" h( y; x( Q) J
  }  i0 n# A/ Y8 Y* A; X# n) F
}1 \5 ^0 y7 d5 K/ k) }9 w
public String[][] getMatrix() {4 y& l8 i( s$ Y# E
  return matrix;
. @5 R; F' r% @9 d) w }
8 W, m+ j' k1 r. L' H. M' d}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-2 07:51 , Processed in 0.018513 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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