设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10593|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;! Y2 N  r& u  C: v6 g! U3 @( L
import java.io.BufferedReader;$ l+ t* g# H: I6 h: n
import java.io.FileInputStream;" U5 i' {2 B4 n  I1 X( u; s
import java.io.FileNotFoundException;, Z! m( U/ |2 o" _+ G2 f! q
import java.io.IOException;7 `5 C# F$ ^2 R
import java.io.InputStreamReader;
  M7 k2 F* Z" y4 Dimport java.io.UnsupportedEncodingException;/ ]7 A5 V( D% b! x4 ?
import java.util.StringTokenizer;
) p  A. c, ?3 p! @public class TXTReader {" ?8 [( J7 K( I/ m/ r, G
protected String matrix[][];! g' g* J1 ]: G, }5 y+ @
protected int xSize;  q7 s& N3 K' b* f. z5 L7 J( x# d
protected int ySize;# d: _8 w6 G* }* E7 [: e
public TXTReader(String sugarFile) {- k4 m( S% O' Z3 @
  java.io.InputStream stream = null;+ a# G, Z+ s% u: H
  try {* a# v4 y  ^$ J" w* F0 f
   stream = new FileInputStream(sugarFile);
) f7 h- G9 v9 V  I! ~1 v  } catch (FileNotFoundException e) {
2 q) n7 ]! a: N7 g6 V: D3 _/ p   e.printStackTrace();
  G% ^2 v7 I3 Y4 d( s  }/ Z3 g! r. t! k# K$ a
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));; c+ x" Y% n# h/ S4 z6 j' v1 u
  init(in);( U. ?/ c4 L( E# N2 ~4 w
}$ d) E6 X- b9 \1 z4 b1 G2 f
private void init(BufferedReader in) {
: G! k3 m- Z" ?# y( K/ b2 \  try {
' h8 B$ D, B6 l: T' ?3 f( X   String str = in.readLine();
3 p% d% s6 j8 J, ^" s2 x   if (!str.equals("b2")) {4 A+ ~, ]9 [; m* w8 k
    throw new UnsupportedEncodingException(
4 q3 s* X" C" V% Y) n6 `$ O9 z, y      "File is not in TXT ascii format");6 t' |- N8 {1 M  N$ R
   }$ M4 p" @2 Z* `. j2 b4 _
   str = in.readLine();
4 x' Q- J" q1 F% z% c   String tem[] = str.split("[\\t\\s]+");* q# c% t( j7 u8 C% w) O5 @
   xSize = Integer.valueOf(tem[0]).intValue();6 ]9 J6 _+ `' b2 H
   ySize = Integer.valueOf(tem[1]).intValue();+ U) I- P6 G* Y5 ]
   matrix = new String[xSize][ySize];+ \. ~9 `3 N9 ?/ R& N# W
   int i = 0;
7 |' Q* X3 N* ^& H0 M4 D   str = "";* r1 z& h  E- K# O
   String line = in.readLine();& s( G5 r) d! U5 ]5 H9 a, M
   while (line != null) {
6 ~! A9 [+ B& J1 S3 c/ q    String temp[] = line.split("[\\t\\s]+");
: Y- V9 a' a( r- b' c7 |3 r    line = in.readLine();
. T* v; ]6 o6 Q+ ^2 |$ Z( e    for (int j = 0; j < ySize; j++) {) B4 R+ r0 |' e( K& {2 D
     matrix[i][j] = temp[j];6 T# N  [2 e0 C% c! u* K8 u$ X
    }+ V% {0 k, b2 ]7 h$ U& i
    i++;/ p  l1 V  }9 ^) A/ \. l
   }* W2 ~+ Q& B, ^" U
   in.close();
9 K+ h& c) Z6 G, ?1 t" }4 i+ Z  } catch (IOException ex) {3 |; w$ L' n9 q8 p& o2 p
   System.out.println("Error Reading file");* s6 f& {% ]) W# e0 M, N
   ex.printStackTrace();7 j7 p* i" Q9 _6 y5 s3 g6 Q( L
   System.exit(0);: }  q& j/ \) C* I
  }
* X# t) [3 l! f/ Q/ N9 \) D }
/ b. K+ I% X" M: I8 B# {9 f6 t public String[][] getMatrix() {8 o4 o3 M; z0 R5 z3 e
  return matrix;
+ N& O0 i+ r& \$ D- V; U* L }( S( d  S, j4 i# _
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-18 09:15 , Processed in 0.015840 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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