设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10275|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;' Y! _) N' [( h, m. e( A  H6 k
import java.io.BufferedReader;
2 M" j3 L0 D1 |7 ximport java.io.FileInputStream;
. a% g* `: {& P9 r0 u4 B; |! ?import java.io.FileNotFoundException;
1 s! ?1 b+ A8 R; o) c& z2 Rimport java.io.IOException;7 c- d2 j, W& L* z( t: `
import java.io.InputStreamReader;
( {8 N. f9 f' ximport java.io.UnsupportedEncodingException;
6 D5 F2 O4 w! B. {6 ^import java.util.StringTokenizer;
  g1 B5 W% w4 Y5 G& spublic class TXTReader {
% L; t& b2 n/ O" Q3 X" b protected String matrix[][];
) X" H% {: K, t0 U protected int xSize;. q. c" z/ }1 I& @2 C
protected int ySize;, @! A" B) U8 r2 q
public TXTReader(String sugarFile) {  x0 d0 k1 C7 d6 }  h8 I- S  }
  java.io.InputStream stream = null;
+ g, W# l5 v% {. v3 X# i9 n% P6 r( x  try {3 o3 _( H0 u% f' h- g; D
   stream = new FileInputStream(sugarFile);
4 l' T- h3 V6 y; K8 y4 P- K/ }& ^  } catch (FileNotFoundException e) {6 y' X) S/ R. o
   e.printStackTrace();2 T+ ^9 z1 S- d' J3 F4 r
  }
- c  Q: e/ M! Z$ T3 {" X+ ^  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
4 E& T0 q) h* \  j  ]! }3 t3 w  init(in);) w0 m# \4 ^+ @
}( \* I' V/ a; p3 H  z
private void init(BufferedReader in) {
- h1 j: k2 X& u) c$ I  E  try {+ ?& J! a0 l/ I( |0 [. m; b# o
   String str = in.readLine();
- a; S! [2 \$ m$ w' F. h- d   if (!str.equals("b2")) {) i; R; I- j$ L
    throw new UnsupportedEncodingException(
+ l/ U. A; Z" M" I      "File is not in TXT ascii format");
& o* D! V( _8 L, ?% x   }( ~6 ?. e8 r+ C" r
   str = in.readLine();% d+ F' M5 B& {
   String tem[] = str.split("[\\t\\s]+");1 E3 Y( B( W6 i* l' i2 y2 f/ @% Q
   xSize = Integer.valueOf(tem[0]).intValue();1 B  h& X" C" q4 U
   ySize = Integer.valueOf(tem[1]).intValue();
2 K4 f1 e. y. r! [6 {   matrix = new String[xSize][ySize];: m, j) C) f) \) \$ u
   int i = 0;
4 |9 D. f" o8 J# [. g# |   str = "";- C) V! K% ~1 L- ^' E
   String line = in.readLine();6 B3 L" h- a+ {
   while (line != null) {
# Y7 I7 n9 l" U( x    String temp[] = line.split("[\\t\\s]+");
9 ?1 g/ e! m9 r, P3 b" J7 O    line = in.readLine();
4 n4 E$ x4 v# e1 ]    for (int j = 0; j < ySize; j++) {
( E) [+ a3 P% v' [& V$ @9 o     matrix[i][j] = temp[j];
& V$ T$ O. A. s6 d* k7 l    }
+ D5 y9 P- o) J7 u" g) j    i++;
0 n7 ~2 p. u0 I9 y   }
5 ?* d, ^4 d) K   in.close();4 d$ B' p  [0 c. M% ^
  } catch (IOException ex) {
/ w$ x7 |$ N2 {2 [: \+ n   System.out.println("Error Reading file");
7 `9 r& j' k0 k/ g) E   ex.printStackTrace();
( F; ?5 e7 M6 ]! Q3 G* l   System.exit(0);
9 _2 K! ]9 s: |$ P4 `* _  }$ e  f" n$ K* u) w
}
) u/ [$ P) |9 I- M0 k" Z public String[][] getMatrix() {
: c/ M* G3 G( c% h. u  return matrix;, }/ D# y! ^+ p& J" A3 x
}
  n7 }+ ^; r) s; E0 A}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-2 00:17 , Processed in 0.013969 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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