设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9279|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;, n* P( R; z7 H7 c) {
import java.io.BufferedReader;6 l& a. M& E) J( K4 s6 h& |/ a
import java.io.FileInputStream;
* Y% ]& }7 L3 n/ Bimport java.io.FileNotFoundException;
+ Z1 J0 K% ~' U# G. G1 {5 q- ]$ }5 Eimport java.io.IOException;
) U* [3 ?8 n9 D: V" c5 O# r* Iimport java.io.InputStreamReader;
1 ^9 `$ V- g4 X' l( dimport java.io.UnsupportedEncodingException;
3 ^' ~. k% {7 f+ @& }3 z& O) K! `. rimport java.util.StringTokenizer;
+ @5 e1 u! x9 H' U  L) Zpublic class TXTReader {2 {$ Y2 z6 u( E6 q0 a$ |1 [
protected String matrix[][];
7 n5 v) p% P( s1 S' f( N protected int xSize;
) I! g: S" W$ M6 \ protected int ySize;4 b& m0 V; k# p0 }, r5 i: N' p) f
public TXTReader(String sugarFile) {
9 f6 n, y7 P/ W) p( e8 B  java.io.InputStream stream = null;
5 \) f! l3 |5 |. X* T  try {
) E0 k. }$ x5 T& O9 n! @   stream = new FileInputStream(sugarFile);- w0 {4 n0 @' `1 l, [) X
  } catch (FileNotFoundException e) {
  G, f4 M) y7 J: T2 [   e.printStackTrace();
5 X" w+ _3 O8 N- m9 R) X  }
$ X0 ~) s7 W9 ^3 p4 e# J, Q7 c  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
* ?/ [; o9 |; Z# L1 v) H# s  W  init(in);  A* o5 C2 d; {8 q$ X( L
}( E8 _! m% O+ E7 Q2 ^8 a0 E. a
private void init(BufferedReader in) {: e8 a$ ]& R+ {
  try {
7 Z1 l! X% N/ b& y" |& G7 Q   String str = in.readLine();& [4 L6 ?& z& b6 o1 h
   if (!str.equals("b2")) {$ \. c9 X* a, N% M* Q. S
    throw new UnsupportedEncodingException(
2 Y5 X9 Y& I. J+ f; w# P$ Y      "File is not in TXT ascii format");
7 P. ?5 K. x9 G) y2 w8 ^8 q   }
. H: K1 f: E. h' [; m   str = in.readLine();
. d% F' p9 |: ]' r* e; `; n" D   String tem[] = str.split("[\\t\\s]+");
. @# X7 \1 d( u, V$ @   xSize = Integer.valueOf(tem[0]).intValue();
( g6 s7 E" L/ S+ y5 p" S8 O% C+ E4 S   ySize = Integer.valueOf(tem[1]).intValue();1 l5 t5 W7 T( j  U" s2 R
   matrix = new String[xSize][ySize];% P9 X. f9 Y; H* h& f0 d
   int i = 0;
1 D) M9 v( l7 X5 u) F% S. s" U7 x   str = "";7 Y; ?- F" [- N0 T8 ^" V# Q* {& f  q! ~
   String line = in.readLine();2 f! j" q  K9 o2 k9 M7 U! ~
   while (line != null) {: w, ^; p- G! E& F" }; u1 H
    String temp[] = line.split("[\\t\\s]+");
5 M& c- @4 J* Z    line = in.readLine();2 O3 o, Y: V9 ]  S' S, `2 z5 m& C
    for (int j = 0; j < ySize; j++) {8 @1 V% ^/ C9 d# B, v# _, W
     matrix[i][j] = temp[j];
" W$ e! l' h1 m' T    }3 G% Z/ j% d" T  h  w
    i++;
' P* U& v" i4 J: @: d   }
. Z( m0 M3 v: B0 R; u8 X: B   in.close();" Q' B4 S& D) M" W
  } catch (IOException ex) {
0 W+ R; N- r7 p% P   System.out.println("Error Reading file");
6 F: u; |& \  u' F9 b8 L0 z6 i   ex.printStackTrace();
4 m! z' J# K$ K1 G* n" X5 U( L   System.exit(0);. C! t$ {7 S8 R# K8 a$ g
  }
( N( j/ S, n, o/ R }, W* n; X8 T% p) S+ c- e
public String[][] getMatrix() {
7 i! D$ v7 J; ]; m- p7 V0 d  return matrix;3 X: _3 e' `( N/ L. x2 o
}
% ~0 m& }, d6 i7 G- C}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-12 18:25 , Processed in 0.014465 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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