设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10371|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;- r  u. |. V5 F0 f
import java.io.BufferedReader;2 O& h7 g. m) ~5 v  `& G1 r2 n
import java.io.FileInputStream;% E5 Q2 g5 q, Y
import java.io.FileNotFoundException;
$ y  i( u- g6 C- yimport java.io.IOException;0 t# A" u6 w4 A
import java.io.InputStreamReader;- d' A/ M% P4 X; w* l* W
import java.io.UnsupportedEncodingException;; O5 A: t$ `5 \0 z: f0 C1 }
import java.util.StringTokenizer;: z+ i) M5 ~$ i& Y9 S2 S# G+ b) p
public class TXTReader {
7 T+ [7 [# r& q# f% @  o4 }( ] protected String matrix[][];
' ]+ p. f$ Z2 I; Z% ~ protected int xSize;. |3 R: V3 K4 i& ]
protected int ySize;
2 C: J$ k. b- J( {# L; H, ^9 B" m public TXTReader(String sugarFile) {0 G- Y2 Z6 L1 D& k0 s6 }
  java.io.InputStream stream = null;) Y- p$ a' z% G) B2 R$ X) w
  try {* g/ k  m& w2 {
   stream = new FileInputStream(sugarFile);
) A# H$ G# [/ ?! @: S  } catch (FileNotFoundException e) {
, P# ~, c3 s: S& W% @8 v: e* E   e.printStackTrace();
- M, h! l# o" f9 Q* l  }
% k4 q* c( ^/ o/ @& K9 ^1 r7 E4 y  BufferedReader in = new BufferedReader(new InputStreamReader(stream));# l3 l% ~( k# e9 m* v1 l% B
  init(in);
/ ~; x; b- B, M6 f% y }
, j! Q7 l) ]/ n: W9 l1 n- _. n private void init(BufferedReader in) {
, o( J; ~2 v$ j& P! A* R# Y  try {
& n& l8 N( l3 O   String str = in.readLine();; e3 B5 O/ Z9 V! m; B2 U
   if (!str.equals("b2")) {
' P9 o$ F( R5 B7 y% w$ t3 z    throw new UnsupportedEncodingException() d5 a2 H9 j# ]" I. B$ v& t
      "File is not in TXT ascii format");% Y9 A  C; r! s. z
   }+ p2 \/ N8 {* u4 ]
   str = in.readLine();
; L, T2 c2 m' P   String tem[] = str.split("[\\t\\s]+");
$ E# [  n5 |* d4 S: }$ P; h7 I   xSize = Integer.valueOf(tem[0]).intValue();' ]# @  D0 p# w4 v& u: B0 e) _
   ySize = Integer.valueOf(tem[1]).intValue();
* f" w. g0 Q+ T' K   matrix = new String[xSize][ySize];
% f  ^) S8 s5 T1 w   int i = 0;/ o8 i- R% L3 ~6 s( g
   str = "";
' D+ x" @. @+ O   String line = in.readLine();
. p9 M3 b( i2 F) _- D# o6 h" R5 n   while (line != null) {
9 _3 v6 n6 n! D8 E+ n" D    String temp[] = line.split("[\\t\\s]+");& }& \* _- z4 G: u5 [
    line = in.readLine();7 A; P9 f3 T- m  J% M9 U0 |
    for (int j = 0; j < ySize; j++) {3 N6 L' _5 E3 f, F
     matrix[i][j] = temp[j];
% p  G7 t8 X; }* G! v, K6 m    }
; t" f- ?: K! u& h: v: u4 n. g' w% r    i++;
! E2 v: |/ z6 q/ @" O6 T( E   }
; q3 v/ E6 |; Q2 I8 o" V( F0 s9 q   in.close();& X4 v  [, O; U- B+ u+ i
  } catch (IOException ex) {
" z# b  {# x$ a& M   System.out.println("Error Reading file");/ x% a/ s  ^/ A3 o* [2 }
   ex.printStackTrace();
9 J. Q& y; N8 M5 N% S   System.exit(0);: y2 @' r1 s/ X+ K- e
  }
7 b9 d+ V5 W" I6 k! d }# j" }3 n. z- o) J) h- x
public String[][] getMatrix() {
8 @, y% V% b5 E  return matrix;- F- A( U  G4 O1 k
}
# S( v( l$ |" u% g}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-6 16:03 , Processed in 0.016068 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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