设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8635|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;$ t. g% Y) M3 G3 c& q
import java.io.BufferedReader;7 d2 z' n. w5 B7 F/ ^
import java.io.FileInputStream;
) q& e1 f8 W: `! Fimport java.io.FileNotFoundException;. g3 [* T, M- s/ a5 s; k
import java.io.IOException;: [/ A1 v( `' l, d# I% r2 u' \6 \
import java.io.InputStreamReader;
$ ?! e* e8 j5 g# l0 p; w' [; Ximport java.io.UnsupportedEncodingException;
& Q8 j& F) w/ w1 ~( X) h' Aimport java.util.StringTokenizer;/ d) g' L- ]. Q4 \5 @
public class TXTReader {3 E* q6 X5 b% m3 K
protected String matrix[][];
( t2 I! o! s2 n. P. S( l protected int xSize;
1 E7 _: E+ M( L protected int ySize;
$ r6 L4 Y2 Y3 y/ ^8 j( G% z public TXTReader(String sugarFile) {( k5 D5 i) n! d4 j/ J% r* a" d
  java.io.InputStream stream = null;
5 F) c' ]& U0 @. z9 O6 l  try {8 J( c8 l0 e4 B
   stream = new FileInputStream(sugarFile);
0 m% h- H2 [: j7 e1 a/ B  } catch (FileNotFoundException e) {
' b6 v) m" m  d1 l& s   e.printStackTrace();$ z: B4 p7 J4 F& m( v/ S9 |
  }8 W7 S, s$ u+ [
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));0 p* s" w9 S( q" Q
  init(in);  ?$ |7 \( [  U
}# V4 `) W7 B9 |' }. o  O5 G, C
private void init(BufferedReader in) {2 U8 r2 _3 W4 s* k( ?' w( a: b2 }
  try {7 G# N+ s; @) s& n' X
   String str = in.readLine();5 W% z: W  S$ s$ w
   if (!str.equals("b2")) {! h3 H( \/ @* ~5 A' _, g9 [
    throw new UnsupportedEncodingException(# O* M- I6 {1 B, c, [
      "File is not in TXT ascii format");
; R& _. I4 d! \- T/ l5 \4 u0 A0 \   }* h3 ]# I& a  @  [, y. W
   str = in.readLine();
: {# F4 b- l" n4 `   String tem[] = str.split("[\\t\\s]+");
4 U$ u6 A, l6 U0 F! v9 y   xSize = Integer.valueOf(tem[0]).intValue();: f& S1 Q, `. }
   ySize = Integer.valueOf(tem[1]).intValue();! M) h" Z/ r  R; w% E4 Z
   matrix = new String[xSize][ySize];
# Y0 f3 D- D1 l) I! @# F% c   int i = 0;! U# d+ g5 {. e
   str = "";; V& W) q2 j0 _. L+ m
   String line = in.readLine();
( D" {5 r& Q9 r# z0 @   while (line != null) {$ O, s4 x; ^+ S
    String temp[] = line.split("[\\t\\s]+");1 B* `) Y  K, u( n9 _( Z4 C/ A
    line = in.readLine();; A4 T2 y$ h: _* E  N% w8 d% |# u
    for (int j = 0; j < ySize; j++) {' U0 D" j9 K# g0 E: h& T
     matrix[i][j] = temp[j];
: J( D& E5 x4 i    }1 }  W2 I7 F# s( R
    i++;6 b8 t* e' J$ C% b5 |4 j0 o
   }: E% V$ e" S6 }  `8 n- ^( k
   in.close();
" H& ?& D/ m  l' v$ f7 g  } catch (IOException ex) {" }8 D& x: Q  u& j* y$ g
   System.out.println("Error Reading file");
) K! ~* q9 D8 T4 D6 [' t9 ]   ex.printStackTrace();
1 s- K5 ]) s# X5 j1 s  @   System.exit(0);
8 Y, u( N' `3 T  S. f  }
; [! l. n6 R1 o }! e. K$ p1 Y: ]1 Z5 o3 g) {, D; U" y
public String[][] getMatrix() {
" s: R! t* U+ W; x6 Z  return matrix;" k5 e: k3 y1 ~+ o
}
4 R2 k. V& X% f2 k5 M}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-4 16:04 , Processed in 0.019465 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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