设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8156|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;2 a: F6 r- E0 c% l( A
import java.io.BufferedReader;) A% |. D8 F4 I( Z1 k' S* D
import java.io.FileInputStream;
1 ~; X6 `5 Z" u. @2 S8 V( V9 q; Oimport java.io.FileNotFoundException;' R' Y, \" q* ]- X  S9 `; k
import java.io.IOException;3 l+ E$ w) E) @
import java.io.InputStreamReader;' t8 s1 z: M; ~8 W( K% r6 x
import java.io.UnsupportedEncodingException;
1 a& n% }: J: Y5 o, v0 G# O" n; A* a( fimport java.util.StringTokenizer;
) j6 J* C" p- g& S% spublic class TXTReader {8 s& A. @# c$ ?# E, r- I
protected String matrix[][];( d6 y  m5 o0 j: M! @5 ]' v
protected int xSize;
- m) e5 h- J$ d protected int ySize;
( q; J0 @6 ~2 ^ public TXTReader(String sugarFile) {  H: z& b/ O5 e( R+ _" r/ j
  java.io.InputStream stream = null;
9 v2 T/ Y& z  B- Y+ L" \2 X1 Q- q7 k  try {( x+ o( `8 ]( ]5 }
   stream = new FileInputStream(sugarFile);
& V1 `7 [2 I+ A$ l, T5 c  } catch (FileNotFoundException e) {
0 l# A9 t9 z' Q% x% `' u   e.printStackTrace();
! T3 u2 k4 C) F4 Z2 g7 W9 X  }' @5 [: J: K* O) Y
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));% B7 v$ @+ s/ z$ G- u7 r
  init(in);2 E- B% P; l& U- V$ o
}
' a; N! c9 Z8 m! r6 x( X( g4 { private void init(BufferedReader in) {
; X- n& n7 Y$ m8 `6 c: H# A  try {6 |/ b) V4 J' U4 @
   String str = in.readLine();0 z( n! B- a' ]
   if (!str.equals("b2")) {
/ s1 |, V( K! Y6 H/ h2 M4 m    throw new UnsupportedEncodingException(& X* S) _  T+ w
      "File is not in TXT ascii format");
# e* a9 B0 b5 N( v+ q/ L   }4 P& |1 P0 I8 p1 a; q
   str = in.readLine();
. ]/ s" u. z6 q. x   String tem[] = str.split("[\\t\\s]+");! b, H( Q5 M" U* n8 D1 \. D
   xSize = Integer.valueOf(tem[0]).intValue();3 X2 \% \' n" e5 B5 l& ^7 q& n: t
   ySize = Integer.valueOf(tem[1]).intValue();
) P$ @2 z( V$ S! @   matrix = new String[xSize][ySize];
+ {' I; R6 _- }8 b5 Q   int i = 0;/ b7 c; w$ X% s- R
   str = "";
" G1 }* H. f: }0 m( T$ s$ `   String line = in.readLine();
6 u1 A) ~4 `5 s   while (line != null) {
" Z3 O1 @3 B. h1 ]  w: k9 N    String temp[] = line.split("[\\t\\s]+");( o2 P' i: n  Q6 R: q( F
    line = in.readLine();
! \; V* o) ~4 @  v9 w    for (int j = 0; j < ySize; j++) {
' L# @7 Q* f( K     matrix[i][j] = temp[j];) [  B4 h# q# H  r! d' j
    }1 d# H* y2 C4 M/ H5 M. K! O
    i++;4 r9 X# P8 M, a- Z
   }# l* |: z9 }1 B/ I5 x
   in.close();
# b. m9 l. M  R9 E7 L* ?" K  } catch (IOException ex) {
. [* J* a: J- G   System.out.println("Error Reading file");- m  K' B! H; |5 S
   ex.printStackTrace();
. u( Z, j5 H) C8 p   System.exit(0);
, A+ I# A4 N3 D4 c! Z, p6 Y  }
) r+ T5 \# m9 T! ]" n6 @ }
! O3 u& S6 q/ m7 w: h$ l% f3 R public String[][] getMatrix() {
& l8 N# y9 s' x# E+ L& u' ]  return matrix;% u& I# F! ]' a
}
, e  l7 @7 r8 I/ B9 M}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-5 20:31 , Processed in 0.016192 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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