设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9098|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
; p0 j3 ~! ]/ G7 q, Y" b6 Vimport java.io.BufferedReader;- T0 Z$ M  ^5 T; p
import java.io.FileInputStream;( Y: A4 L4 y' w3 ^2 ^
import java.io.FileNotFoundException;
, y9 L7 L9 s$ @$ Z) uimport java.io.IOException;
+ F4 C. c; c3 p0 qimport java.io.InputStreamReader;/ `/ X. u: G* `& I
import java.io.UnsupportedEncodingException;
  p$ [/ s. e7 W2 g4 Jimport java.util.StringTokenizer;
: Z! u' ?' J, ?; ?public class TXTReader {
. Q) r3 A$ Y$ V protected String matrix[][];0 H) K1 o9 P# K. W
protected int xSize;0 ~7 a9 Z, |( D+ w/ Q, K9 K
protected int ySize;
( a1 U3 ~+ H5 E% y) ]2 K8 H1 ` public TXTReader(String sugarFile) {5 @8 F& L: n3 C/ q; Z) K: f
  java.io.InputStream stream = null;, v+ }; p% T4 a. Y7 }
  try {0 C/ I1 t& u* D5 T+ e9 P& @- g
   stream = new FileInputStream(sugarFile);4 v  ?  k& u# q/ e. x# Y
  } catch (FileNotFoundException e) {7 K6 g# b1 h& ~$ @
   e.printStackTrace();
' U% |* O0 M8 x7 C+ l" o  }) s6 l( C9 k: N) v
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));6 f5 j1 {( r- ~% F, S) n+ ?
  init(in);
6 t3 H" H) d" q  W0 ^! ? }$ ?# U( q* ]( ]# h  ?
private void init(BufferedReader in) {' m5 f) R4 h7 @( @& ^1 x
  try {/ I  N# I8 g& @1 K3 Y
   String str = in.readLine();
# `& G0 R6 f- _% l% f   if (!str.equals("b2")) {
) i% i6 r6 R' x2 E    throw new UnsupportedEncodingException(( v( c2 Z+ `6 i5 W
      "File is not in TXT ascii format");
& D8 Y$ r2 }  H8 G   }
/ i' _8 I& B/ S* Q0 B   str = in.readLine();- |- I  ^. \) h" G" T! G2 Z
   String tem[] = str.split("[\\t\\s]+");5 ^6 N2 N' K/ V) `! j! T
   xSize = Integer.valueOf(tem[0]).intValue();1 I. v1 q7 R4 ]8 u  y0 e
   ySize = Integer.valueOf(tem[1]).intValue();
# p: P/ D$ Y1 U. a* G   matrix = new String[xSize][ySize];
, }) ]% U4 G/ l6 a* k0 t   int i = 0;
) X' q2 U% y6 j" `# F: u1 C2 Z   str = "";1 ^! J; c1 ?4 N$ q9 q: ?0 _
   String line = in.readLine();: v# M  R1 X( i# C8 d  o
   while (line != null) {
8 M0 \# \: \8 W    String temp[] = line.split("[\\t\\s]+");
3 Y% R' ~, Y: ?    line = in.readLine();. V( s* W" g* f
    for (int j = 0; j < ySize; j++) {
" v0 e9 M# J: u+ B     matrix[i][j] = temp[j];( O9 S7 p3 ~+ I7 B9 b
    }
) o) ~' k: J' o; q% V    i++;6 p$ U, ]* |+ `* M1 @% n& _
   }
! Y( U0 o9 k8 q   in.close();4 v5 ]7 M" Y: Z, \! g3 S
  } catch (IOException ex) {
. S2 ~" I0 T/ l   System.out.println("Error Reading file");
. s/ r" U" W4 E& k' ?, c   ex.printStackTrace();6 Y# I! L& x  X% }+ m
   System.exit(0);
# ?4 D8 V; v9 y# P, o( _0 V  }, M) `' Q% F  S7 k. d5 }" s$ m
}
& }" q) p( {. Q6 U2 z  o public String[][] getMatrix() {
1 G% q% N7 ^/ f4 |# z; V$ l8 `) g  e  return matrix;
; |! q( ^- Q5 {5 Q* ?  A9 s }  _/ ~( x. ?4 T& @/ x# }
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-1 21:22 , Processed in 0.017570 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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