设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6091|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
( A, g6 F3 y1 j; \; _import java.io.BufferedReader;9 S6 ]7 ~) o& b; c6 p2 P
import java.io.FileInputStream;( W, u  b* H6 E
import java.io.FileNotFoundException;2 R2 i0 L& [0 `* l
import java.io.IOException;
: p) j7 w; ^. _import java.io.InputStreamReader;
4 A1 n5 n! N4 h2 J0 z: ~1 U: O! bimport java.io.UnsupportedEncodingException;
& C. I% T: b' q- \7 `) Kimport java.util.StringTokenizer;% |) j% U! t+ [: U* K
public class TXTReader {
( o0 C' x: Z( I/ ^+ U3 {* L protected String matrix[][];
9 y6 V" L$ U( P% |" P) z; Y& b8 S# u protected int xSize;$ t0 k- Z7 l7 M% E& C; h' M; v( I3 L7 v
protected int ySize;5 O- H2 a) ]1 K. d  K5 n5 j
public TXTReader(String sugarFile) {
' P" B6 W) K+ J( G" A  java.io.InputStream stream = null;
% G( Q' f& \- T  try {  H4 x' q0 Z) o0 |  u$ K! \
   stream = new FileInputStream(sugarFile);7 N! O& O0 E2 e" I/ j& }7 ~$ h
  } catch (FileNotFoundException e) {
$ j# M* A8 `, Q5 e   e.printStackTrace();
2 S4 }! C) p; S7 `  }
- \3 o7 N: z5 F  BufferedReader in = new BufferedReader(new InputStreamReader(stream));; `! M3 h, v" S) W7 w
  init(in);, _( k& T% W! M3 V8 p" h
}
1 C5 k+ O1 o3 D$ S4 F( b private void init(BufferedReader in) {) K) W6 E9 {& W+ d' a
  try {+ `  n# c1 T2 u( d0 G8 Y: u* g* o
   String str = in.readLine();
, C) e+ I6 [9 m' Y' l: d   if (!str.equals("b2")) {
3 `( S8 v+ `; ^+ N/ @' U. g    throw new UnsupportedEncodingException(
4 ]5 J0 M/ p5 U0 C: u      "File is not in TXT ascii format");
2 m2 C; ]- n8 z1 `* j* \  |# i7 ]   }' \$ g7 D3 K  N
   str = in.readLine();9 r# X% j5 Z$ k+ a! k
   String tem[] = str.split("[\\t\\s]+");
7 n0 }. w- x" H! A( x   xSize = Integer.valueOf(tem[0]).intValue();
6 [$ Z, O  Z& a! L+ F- z   ySize = Integer.valueOf(tem[1]).intValue();
6 G1 H$ C# L' z! R6 X/ P   matrix = new String[xSize][ySize];" o  c7 u8 U: _0 ~( M; j" I% E
   int i = 0;: ^9 w+ y. j  x, h% j' O! a0 R
   str = "";
5 q& k3 A7 o0 B- r, s- |   String line = in.readLine();) U; h- X, d8 }0 `# B" E
   while (line != null) {; x1 X6 q2 m  x; E  B
    String temp[] = line.split("[\\t\\s]+");
, V. m# V1 `/ {: T$ R3 L    line = in.readLine();4 e7 Q9 I7 ~! G/ q7 A) S, j
    for (int j = 0; j < ySize; j++) {# Q4 R0 o, a+ |1 V, t
     matrix[i][j] = temp[j];- b" v4 |$ U1 v# `1 V7 _
    }9 Y( Q( @+ m' T5 V
    i++;6 G. q: `5 X" ?9 |
   }! l  [: B) ]- h& G
   in.close();
2 x0 G# [3 k- F  } catch (IOException ex) {
4 C. s% T) y' b- c5 ^   System.out.println("Error Reading file");
( \: e) t/ @5 k* W, ~; B   ex.printStackTrace();. F8 \9 k. p( b" q! N3 a' `% r
   System.exit(0);& P# V* S( \) m
  }
/ P, b2 z! I$ J+ t& r; [ }$ p( y. |% @; c7 b/ i6 o) g3 {
public String[][] getMatrix() {
2 s5 `5 b: k5 p* {  return matrix;
9 B2 L) B" o4 A: E8 j3 |! O: }/ s }
, w2 n% @4 q4 ^% _" ]$ X1 f" M$ A3 Z}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-10 22:48 , Processed in 0.013963 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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