设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6462|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
8 P" R7 Q/ \. H% Q9 Limport java.io.BufferedReader;% \1 D5 r1 V; w4 E% J& a
import java.io.FileInputStream;; z# n$ g, R! B) g
import java.io.FileNotFoundException;
3 g& u9 z9 u% |import java.io.IOException;
! M1 M& m3 l$ x% ^# Aimport java.io.InputStreamReader;. q; h5 N# s' a  R
import java.io.UnsupportedEncodingException;$ ^2 K/ q' N* {  t% M4 D
import java.util.StringTokenizer;
! U) M( \. S# |. W! ?public class TXTReader {2 ?+ O. o1 A' T$ I6 [( L+ A. X
protected String matrix[][];
, p, U: _+ t5 v+ r protected int xSize;
+ n& H& |' C* y+ _) K& K protected int ySize;! S9 h$ Q8 g4 x4 |- v
public TXTReader(String sugarFile) {
6 R" N$ @% n+ C2 n& r' N/ B  java.io.InputStream stream = null;
+ x! ~8 D: [8 c- z- w- s  try {- c; M6 u1 `7 \5 F
   stream = new FileInputStream(sugarFile);: V) `* Y% t% p8 @
  } catch (FileNotFoundException e) {) A6 Z$ m" I: D
   e.printStackTrace();4 G2 Y# [# j& T1 z) k
  }
( Y/ x, O6 T# b. l  BufferedReader in = new BufferedReader(new InputStreamReader(stream));" D' e' {) G' R5 s. g3 T- s- W
  init(in);' [# |6 X& ?' e
}
7 Z1 F1 ?1 ?" |4 h- j private void init(BufferedReader in) {
6 _  H* e- g- L& T8 h  try {
3 q/ X% e- x3 h8 I! V4 n   String str = in.readLine();6 Q/ O& P) Z2 N/ b/ ~5 c
   if (!str.equals("b2")) {$ e: b" N# ^1 i' O
    throw new UnsupportedEncodingException(
# r( P: f  @+ L& ^      "File is not in TXT ascii format");
$ b- ]- W! P6 @! I4 [   }( q$ U# R) |- J( Y  N' p# H1 `
   str = in.readLine();% X- ?" X( k) ~, b0 v$ L
   String tem[] = str.split("[\\t\\s]+");
8 n- K5 J4 l8 v  _& V   xSize = Integer.valueOf(tem[0]).intValue();
* q0 ?& R$ x; g+ X0 A- s   ySize = Integer.valueOf(tem[1]).intValue();; X' X; A8 L& j+ g) o( S
   matrix = new String[xSize][ySize];
) d0 f. R6 ]' J2 M) w   int i = 0;
$ Y7 U# i. F( a3 \2 A, G; Y9 V   str = "";. D9 B, ^$ z" m( f* k7 ]8 ~- u
   String line = in.readLine();
5 K+ m( S0 n$ b5 D! I   while (line != null) {+ @- C! `# u7 ~: b3 g
    String temp[] = line.split("[\\t\\s]+");) C* f- r3 @* M# F2 i% d% o
    line = in.readLine();8 W0 A. n8 i8 r: Y
    for (int j = 0; j < ySize; j++) {
0 S6 Q3 g! g" X  o+ R+ [     matrix[i][j] = temp[j];% J3 M7 t4 l  ~- r* z" g
    }% f8 q; ~, q/ |, C% v4 P
    i++;
: U2 @$ c8 z! L6 U   }* C4 Y( X1 [) b4 K* l5 {2 q
   in.close();, P8 k8 V2 I& M. D1 K. }8 @
  } catch (IOException ex) {3 r& N) [9 [! X2 F& O5 E9 I& G. x
   System.out.println("Error Reading file");) |6 J6 E4 n6 ^" C: X. s8 Z
   ex.printStackTrace();
7 X! W7 I+ H; X( `: r   System.exit(0);, A' u: @% x. A* k
  }* P7 G: ?+ s3 g' v: j' S
}1 a: }! d: i2 x8 K5 A
public String[][] getMatrix() {) v4 h  T2 D9 F7 P# V
  return matrix;, g# ]( v! q" L8 n; F. U
}
5 h9 K- y2 M  f1 v$ \}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-12 16:16 , Processed in 0.019181 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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