设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10383|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;5 \. J( a% Z" A# h+ S
import java.io.BufferedReader;
% W1 `! ~) r/ N) V! V6 I8 K: j, ~import java.io.FileInputStream;
4 \% [" q' U& Himport java.io.FileNotFoundException;
& i# o- h; F2 B( fimport java.io.IOException;
' ~5 n5 I' j# n6 f, |0 v; l% m, i0 Timport java.io.InputStreamReader;! V; P& A  Z6 e* k
import java.io.UnsupportedEncodingException;+ v! }( x) f2 y: }% n1 W! T) H2 h
import java.util.StringTokenizer;) @1 N8 x: j6 c% k& I8 K  Q
public class TXTReader {
/ I* z! S7 T# G. u/ Q protected String matrix[][];
9 L/ b; o) g; z! Y. p protected int xSize;) t- F  A" l7 y/ _# i  H7 u: j; x
protected int ySize;
9 h9 N4 Q! g' g0 ^+ E3 X. c. H0 f public TXTReader(String sugarFile) {& S4 Y1 g% w+ f& _1 b8 H' B
  java.io.InputStream stream = null;& Z6 w' F$ J2 T! v# W, C
  try {- B& M8 E8 r5 J
   stream = new FileInputStream(sugarFile);
/ L( u# \2 Y. x- S. ~  } catch (FileNotFoundException e) {
5 t4 M0 \9 Q; D! q4 q' B* _5 Z4 ~) m   e.printStackTrace();+ m, a" Q7 A. I! N
  }
  p7 D% |( h# X0 j  BufferedReader in = new BufferedReader(new InputStreamReader(stream));( r8 i8 O' h$ ]$ w4 z' ^2 o
  init(in);
( R" T; g5 y" ^3 m  c }
1 Y7 r( w* z' H) z! j, [2 z$ u% Y5 t private void init(BufferedReader in) {& C& j: N( V4 {( ~
  try {$ [5 n  W9 }9 H% z
   String str = in.readLine();" K+ K$ H( T8 U
   if (!str.equals("b2")) {
) y0 V- f+ v/ X. f    throw new UnsupportedEncodingException(, G) S9 z6 ^4 g+ V8 J$ r
      "File is not in TXT ascii format");( H  P( z4 ~; r( I/ q. q) a
   }
7 G, Z& I( C+ z0 Z. u   str = in.readLine();" s1 A: }2 J) e* Z* v% ~$ o
   String tem[] = str.split("[\\t\\s]+");8 s  h) i& `% M( |% `
   xSize = Integer.valueOf(tem[0]).intValue();1 U+ m& N) ^/ d& U# E" f' x2 H
   ySize = Integer.valueOf(tem[1]).intValue();
# V' s6 f, |1 ]/ `* G( ]   matrix = new String[xSize][ySize];# u+ B+ x2 `9 n/ p9 `
   int i = 0;
7 w2 g3 R0 [7 D   str = "";- c. Z; V. u" }9 R
   String line = in.readLine();5 r& `: d# G  E8 C+ F$ y
   while (line != null) {
/ t# ]- s1 L. [; c* s; U: _2 h    String temp[] = line.split("[\\t\\s]+");
$ }( C  o' _0 u/ M9 `/ l    line = in.readLine();
* [/ d- a' P' u" H- H    for (int j = 0; j < ySize; j++) {
* F- |% O* s: d& H- C7 }/ w     matrix[i][j] = temp[j];/ V# W( j' w* |7 H- Q
    }
$ Z6 H7 S! T) n: r    i++;
: F5 O8 Z. c) T0 d  _   }
6 O9 \! n9 y1 H: ?+ P8 V. n1 o   in.close();
# N! H+ U- g& d! E; \  } catch (IOException ex) {4 i: C( G, B* Z3 f
   System.out.println("Error Reading file");, v& U) ~' Y0 T* Q8 V6 C- }
   ex.printStackTrace();  B/ ~3 |, h7 Y
   System.exit(0);
7 V, `7 O- p0 X+ y8 w( J  Y  }6 z  d& \$ k/ ^1 V2 z/ U/ a
}
1 h% o- d% M  `+ k% n  D public String[][] getMatrix() {9 ?: z  x; L. ~5 p! y
  return matrix;2 W0 @. s$ z# U4 K
}
) N3 Y5 v" w: m' ~, m}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-7 13:46 , Processed in 0.016441 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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