设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8853|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
( Q- g- O( o8 h- `import java.io.BufferedReader;
0 R/ I3 }# n  v2 O9 |. \' Rimport java.io.FileInputStream;2 f( L1 n6 L. g3 y
import java.io.FileNotFoundException;' F: y  V5 ]4 _* F
import java.io.IOException;
% h: t2 i  Z/ u' L! ]: Bimport java.io.InputStreamReader;9 Q- a0 t! f) H$ t* i" h4 j% R# `. v
import java.io.UnsupportedEncodingException;
) M; |! I0 v" S; E2 Nimport java.util.StringTokenizer;
- K' a( d8 O) Y8 D# S+ @- |' Upublic class TXTReader {
; p. G7 W! l( q1 `* P protected String matrix[][];
# ?+ {/ [; U" Z- p protected int xSize;7 W3 }' z* R5 E& h
protected int ySize;$ ]% T/ I7 n- [: u0 z2 t
public TXTReader(String sugarFile) {/ B9 X  p$ K4 M9 \+ `
  java.io.InputStream stream = null;
  x( {+ P/ P' g( _2 n7 y  try {8 r  ^, b2 `$ t  r7 U! y% ]
   stream = new FileInputStream(sugarFile);7 [5 G- z& e0 O  M0 k' p
  } catch (FileNotFoundException e) {" _8 r7 c! m1 ?1 u+ I9 j0 K
   e.printStackTrace();" O1 @8 o; X8 e) g$ W9 W5 z4 `
  }
0 p6 V6 K8 T7 F: }' I  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
2 ~% P% O, r2 c  init(in);9 h5 ~2 l" }. R8 n9 m' _5 u
}
4 h- V) q+ ^+ @1 K* S( y/ X9 t7 s9 L private void init(BufferedReader in) {
0 D8 Y1 `# j& {2 j+ ?  try {
# u3 _/ {  J# Z5 R5 i   String str = in.readLine();
4 x, \9 v6 d/ ?, W, w   if (!str.equals("b2")) {
! O" t# h0 t6 G! }, O* ^    throw new UnsupportedEncodingException(9 d. Z- d/ a* j& b! [
      "File is not in TXT ascii format");: P3 {2 e9 a7 O/ p
   }# V% g2 t$ _! u; n, M, V
   str = in.readLine();
9 d/ _7 v5 ?: V   String tem[] = str.split("[\\t\\s]+");
  Q/ W; {" j7 P# s   xSize = Integer.valueOf(tem[0]).intValue();0 L1 s& g: g7 @) E
   ySize = Integer.valueOf(tem[1]).intValue();% M2 ~# ?2 q/ F1 Q
   matrix = new String[xSize][ySize];
/ d% `4 X6 Y/ N) X" A   int i = 0;$ s- s6 q0 d- k. a
   str = "";$ `8 ?! d% i5 D) o  \
   String line = in.readLine();4 D4 A) ]2 C: M9 w$ s! ^0 [. I' @* W
   while (line != null) {
" E# h# n" k: B2 Y( t' c" ?( r' ~    String temp[] = line.split("[\\t\\s]+");. m# p) @6 K5 N9 N
    line = in.readLine();
2 V: [5 a: S6 n    for (int j = 0; j < ySize; j++) {9 ?. C7 c( h- W1 T8 g
     matrix[i][j] = temp[j];3 E  E* d# |! `# m* u: e
    }
, k- L0 m! y6 N: r2 u- w    i++;
) R' k# b3 t, M   }" T0 S- U! m& h. z( y: O+ ~: Z
   in.close();
& I( a/ ?9 D+ r0 j  } catch (IOException ex) {
/ P# a5 d8 H( N$ i8 T   System.out.println("Error Reading file");/ J# J( _' d! o3 L4 [
   ex.printStackTrace();; Y2 \/ R7 H7 s% z$ L
   System.exit(0);& n# J# o" x2 _7 K  g* Y2 D2 M" r
  }! v# r+ v% a: z/ b( @
}. p6 Q$ U& s- p4 [
public String[][] getMatrix() {
' h' o$ w/ p4 f8 c  return matrix;
+ D1 @% a& r2 D  ^4 x6 k }+ J0 W, a( K+ j. x: B8 U
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-17 12:25 , Processed in 0.013109 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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