设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6775|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
. u2 p" i$ v# M2 l4 l: h* i$ dimport java.io.BufferedReader;+ ?3 j" l) N0 r+ I5 x
import java.io.FileInputStream;
8 J' d/ g, L) I' M4 Q/ c$ j" K# pimport java.io.FileNotFoundException;
, k  N! a( b, p5 nimport java.io.IOException;7 q9 q8 O: @* g/ C7 s' m+ ]& [6 s' R
import java.io.InputStreamReader;
& ~3 k& I: t+ R8 [9 S# x% v, \import java.io.UnsupportedEncodingException;
& z! @+ y# Y8 aimport java.util.StringTokenizer;/ V. y" t, x" ?& m1 K
public class TXTReader {
8 o% D$ F7 U# M protected String matrix[][];
2 w% ^. R' {3 ^2 }  x  H. r protected int xSize;
" H0 m4 a, B# }8 W4 J" \ protected int ySize;
, D7 X5 _) o! v# ^7 c; I) Y) D) J public TXTReader(String sugarFile) {; o* L4 r' K) Q! Y
  java.io.InputStream stream = null;1 S5 W% Q3 E) w* w' B, `& ?# b
  try {
' k! W$ s/ f1 u1 z+ F* `   stream = new FileInputStream(sugarFile);
3 o9 r. A0 X; u  } catch (FileNotFoundException e) {
6 U  r, N- G! Y  f9 l! [   e.printStackTrace();' [: h7 w. f- R8 d( N
  }
1 `0 V9 k' I* Z0 a7 \, d  BufferedReader in = new BufferedReader(new InputStreamReader(stream));4 E( c( i! O1 c6 f2 v( W
  init(in);
7 i9 b0 V0 Y9 e% U; a }1 y3 X. z$ ?1 s7 F% d7 z
private void init(BufferedReader in) {
8 [" s/ u  S# H' ~# d: Z: \$ y" y  try {
* {! P; ?+ G1 y" W& Y- z   String str = in.readLine();
8 B: ]# }' [+ M   if (!str.equals("b2")) {
$ ?0 h+ {* C- H1 F& g    throw new UnsupportedEncodingException(
1 C7 V2 m( |4 b) x      "File is not in TXT ascii format");/ B/ i+ g$ F! J2 x* J
   }
# G) x$ A$ t% Z  E# A3 ~   str = in.readLine();
- G3 t* S. e& f; R; |   String tem[] = str.split("[\\t\\s]+");/ V; J7 {! A4 h0 l& W" x% T, [0 t
   xSize = Integer.valueOf(tem[0]).intValue();
2 M5 [! m7 p' b6 a* \! |+ y1 _   ySize = Integer.valueOf(tem[1]).intValue();2 }# `1 c# g( F% `  D2 f
   matrix = new String[xSize][ySize];4 Y9 l, M# e3 Y2 H* |7 L  v' F4 M& A$ T
   int i = 0;
8 I# S. I/ K  p   str = "";3 d2 Z& f/ p3 R) K; ?
   String line = in.readLine();
- c# e) P) f3 \   while (line != null) {" ]5 a, R( V) S
    String temp[] = line.split("[\\t\\s]+");
9 V7 D+ W4 h) w( s    line = in.readLine();5 J" U' }- W- p: F$ r8 Q( E) W$ o
    for (int j = 0; j < ySize; j++) {& z( Y; X4 g& M+ f- y
     matrix[i][j] = temp[j];
. U  |% J$ o8 @4 ~& Y4 r) D% X! a    }
: f, q8 h, S. h' t    i++;
. c6 p+ J3 F: {# b3 Y   }; C( f/ T, s; b% u9 E. M$ }% q% T! ~" C4 a
   in.close();. _0 q' k# Q  N- W2 g" H: k$ g
  } catch (IOException ex) {1 M5 ]6 ?/ q) A: E* z
   System.out.println("Error Reading file");
. y9 K6 l, T: _* p! _   ex.printStackTrace();/ W2 D1 o/ d: n! |
   System.exit(0);
/ v( k9 C; I3 t" G  }
  z* R) }7 o( T6 ~: |% | }
6 T5 v& d* X9 h. M public String[][] getMatrix() {
0 H' T! Q9 |& O$ l$ A9 A6 b) R  return matrix;
# c) y6 h0 D3 R/ D }
' t) y6 O2 ~3 o% E2 p3 D. j3 u}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-2 11:42 , Processed in 0.014595 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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