设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9088|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;; M, t- K, S" [5 J: S9 u
import java.io.BufferedReader;( o8 I- T; Q; V0 i% g$ Q. [  p
import java.io.FileInputStream;) r- C1 d; w( s
import java.io.FileNotFoundException;$ ?% P) D- S+ j: s8 p( p* ^
import java.io.IOException;
5 ?  E( R; ?8 o* c% C" Jimport java.io.InputStreamReader;- V- }  N' j( x8 H1 R9 u
import java.io.UnsupportedEncodingException;
0 I8 [0 E$ O( {# ~; }& g% kimport java.util.StringTokenizer;
8 ?) \6 d2 l; _& Npublic class TXTReader {9 V( S4 R( l3 a0 Q( V
protected String matrix[][];
' h7 e/ e: s' C' s* ~ protected int xSize;# _+ g* b4 w1 C
protected int ySize;
  A: h. a; v" o2 k4 [4 O' ~ public TXTReader(String sugarFile) {
+ W. A9 J4 e/ i  B; ]  java.io.InputStream stream = null;4 \' z$ S$ i4 z- k1 ?  a
  try {
" [0 V3 ^0 K0 H3 P" t/ D0 z& Z: `   stream = new FileInputStream(sugarFile);
* u! l7 m) i. V- e  } catch (FileNotFoundException e) {3 T3 j2 B, O" B4 K8 d5 ?
   e.printStackTrace();
% k0 c# W5 v: t  }2 ^5 P4 T; V  E7 N9 z
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
$ H- c* C' M: e- M9 V- T% C7 e  init(in);  @$ M/ x* t- i* ~1 e+ W5 h
}( ^) S# o  I% ]! Z+ W
private void init(BufferedReader in) {/ i1 L. O! m. T! v5 N
  try {
/ A& U8 c- }) V5 x2 M* a( V   String str = in.readLine();
1 H7 b* P, v8 v$ E7 R   if (!str.equals("b2")) {
6 P! {) b4 f' e    throw new UnsupportedEncodingException(
0 v4 r( Z9 H, n      "File is not in TXT ascii format");
# X; q  G6 M% }! R   }/ w- d1 x$ u8 d, ]
   str = in.readLine();) e& Y# B0 q6 J6 X$ P; \
   String tem[] = str.split("[\\t\\s]+");* \9 p" s6 O: A! I
   xSize = Integer.valueOf(tem[0]).intValue();
6 W7 F% y5 e) e+ J) d- r7 A  n" S% k7 O) j   ySize = Integer.valueOf(tem[1]).intValue();
1 B) V/ I: ^) r- t   matrix = new String[xSize][ySize];
3 a& v& q' ~1 i) r- s   int i = 0;
; g" e3 C5 Y: u" S7 O6 |. O   str = "";" x* S* a8 }- i5 B
   String line = in.readLine();2 j+ ]- \) c# n: b' ]: d! O
   while (line != null) {; f$ ], m7 X0 N& Y& t
    String temp[] = line.split("[\\t\\s]+");
; ?* x0 P. T3 x/ d8 h    line = in.readLine();& E" g8 V6 l5 j% m! o# v
    for (int j = 0; j < ySize; j++) {
& X4 k# z) ?8 s% v     matrix[i][j] = temp[j];+ W, M0 Q. v4 S( a" o
    }
/ L2 q$ }$ N) M& a" E/ o0 @    i++;
0 q: ~9 R& e# ^1 `' q2 f5 M3 D. g   }  |. l( z+ I9 k, G
   in.close();. _6 c- T: G+ z/ s' b
  } catch (IOException ex) {
+ R; X7 y  v9 P& O5 k7 B   System.out.println("Error Reading file");
! Q; c! y: z' S2 q   ex.printStackTrace();2 ]( A2 W% [. Q1 W% I: d* X8 ?
   System.exit(0);
  _5 x* L8 _# z! i; B  P  p! n  }
) M6 E& u, d. H' i- ~; Z; v  p }1 Z/ I# R6 P+ @; y5 q7 K
public String[][] getMatrix() {$ a; ]2 n' ]2 A9 z
  return matrix;
& j4 m( H/ k8 K! G# V( z% [& t4 D }  j! @# B, h. p$ o% A+ L# G
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-1 05:06 , Processed in 0.014582 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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