设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9754|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
6 s0 F$ N# L# }( V' F+ Pimport java.io.BufferedReader;
( }) }* J5 b+ p) \3 Iimport java.io.FileInputStream;# c' ?$ I3 {  d! M0 G( W7 ?
import java.io.FileNotFoundException;  U) {, Q0 k( @1 a. n
import java.io.IOException;
5 d  L& w$ h/ M+ l$ k/ |: zimport java.io.InputStreamReader;, N& s/ R5 N5 z( G& J+ m8 E
import java.io.UnsupportedEncodingException;
  g' t) N: N* Timport java.util.StringTokenizer;4 T& @! {  x& `6 d9 w. l
public class TXTReader {
$ L& Y. q9 T5 f5 b( C, l( }5 Q protected String matrix[][];
+ e4 @9 ^% F! h) n( r: N9 @ protected int xSize;
" ~" t* B  g9 k protected int ySize;
9 R# S$ p. J' m' f; I+ g5 Y2 J public TXTReader(String sugarFile) {. `/ x; i( ]" M9 `* q
  java.io.InputStream stream = null;
6 N" ^! y7 y, x- l! Q" }  try {6 `+ t' v& o3 ~6 s3 d
   stream = new FileInputStream(sugarFile);
* g: Y6 O6 y0 y9 b9 V( F; B- r  } catch (FileNotFoundException e) {' p4 O1 ~' U3 H8 u  R
   e.printStackTrace();
& W6 o0 F. r) l* T0 d- \. Z  }5 K9 ~* f6 O9 y( O
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));+ h0 C6 b1 Y7 l
  init(in);8 E! b$ O; x' d& x; A
}  `' v; N" z% v2 E9 ]+ g
private void init(BufferedReader in) {8 Q4 ]8 e4 D9 C3 X' \; a' O
  try {: H6 g9 z* A+ L
   String str = in.readLine();: L: O1 ]5 Q8 X( t0 A% f
   if (!str.equals("b2")) {
5 X% L1 D8 Q1 S7 ^    throw new UnsupportedEncodingException(, q" l$ Z3 X$ F) f
      "File is not in TXT ascii format");8 @, \" H1 S. o3 V
   }5 x$ L4 w) m2 t4 D) r7 {* E' s
   str = in.readLine();9 l0 `4 G+ b. x
   String tem[] = str.split("[\\t\\s]+");5 o1 t  s- u% K1 n& w1 O! v4 t" w
   xSize = Integer.valueOf(tem[0]).intValue();
3 i6 A# G7 y$ Y" p( W- z! ?   ySize = Integer.valueOf(tem[1]).intValue();4 m6 e! \' c5 i* W7 ~
   matrix = new String[xSize][ySize];9 D' Z1 l8 V& @8 C) G
   int i = 0;
7 ?! D$ y* Z/ N& O5 N; g* ]* w   str = "";
( q0 y6 g/ ^* O$ K+ j: }   String line = in.readLine();
# o# o1 f9 E6 v* ~   while (line != null) {
7 \7 x- O3 {% `    String temp[] = line.split("[\\t\\s]+");
/ p  A0 a: g, H; i1 @3 v7 @. w; @: f& `' g    line = in.readLine();
) w+ e, V# t: m3 Q    for (int j = 0; j < ySize; j++) {# V. D) ]. r* F; ]! g4 f
     matrix[i][j] = temp[j];
: x7 ~# n. o. q6 ~, S  T7 ^# D    }
# a: q/ |6 h5 g" |( E% A  U6 k    i++;
" N6 T% Q0 V  c, t   }2 N# k" \' S4 g8 c3 K( D8 p) {
   in.close();
: t$ P2 e- j( p2 r# {9 J  } catch (IOException ex) {$ J# g1 q5 g; z$ z8 L* M
   System.out.println("Error Reading file");2 g  C! C- Q. T) V0 E7 \
   ex.printStackTrace();/ V2 S4 B; ^2 N5 R/ ]9 z( G/ o
   System.exit(0);1 {3 q/ H, ?' l, }7 p
  }& O$ |6 u" f9 v5 K) [
}
, Y* j, h4 x6 U public String[][] getMatrix() {
8 s4 Y) ]. N2 |/ k  return matrix;
( s! V8 {& I, E- T }1 a  j& C/ x9 u$ E4 [( e6 x
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-2 06:21 , Processed in 0.015213 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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