设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7460|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
) P1 k: v! {) c1 X2 _* d' jimport java.io.BufferedReader;
& @! d" h; n8 g& N; _import java.io.FileInputStream;9 R6 h. i! T1 a+ Z& k: x
import java.io.FileNotFoundException;- M1 `" ^0 Q" k& r; B( ]
import java.io.IOException;
/ t- F! D9 B. s( f# C4 I' cimport java.io.InputStreamReader;
' l; J7 x3 p: F& Q6 n+ j) Ximport java.io.UnsupportedEncodingException;
* G. R3 |4 H0 k1 e) B+ `import java.util.StringTokenizer;
( t% E9 ?9 Z+ upublic class TXTReader {
( f- `7 v6 X* g: g0 L3 j8 e protected String matrix[][];& N% I, J7 n0 i# l# w) b
protected int xSize;
: n$ u$ Y" I# f9 }  Z1 p5 Y) f2 j protected int ySize;5 C' A% x+ _4 _+ Y3 Q
public TXTReader(String sugarFile) {9 k- F& \) V' Y4 m& ^$ B0 h2 K
  java.io.InputStream stream = null;* V& G7 @4 |5 P' ]1 U8 y2 K
  try {/ _- ~# c$ a6 n2 _9 L& l1 q
   stream = new FileInputStream(sugarFile);
3 G5 H2 @% }5 }+ D6 V  } catch (FileNotFoundException e) {
" N& S* ?! G# O# ?: f: n   e.printStackTrace();
) d# C1 X5 ?( E. u4 k: F: F  }$ C3 W! h5 i. a, [3 Q. O) N* H; H* N9 _
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));+ R+ }3 M8 b" Z6 c/ ~
  init(in);
4 r( o" [- }( [" z- W' J2 z% ~9 \6 \ }0 h3 z2 T; ?" p2 z5 M2 h
private void init(BufferedReader in) {
7 I/ M* c, ^# J# s* D  try {$ a0 C9 E( ~0 P0 s6 D- V
   String str = in.readLine();
- n% V! c" i1 H  I  k   if (!str.equals("b2")) {
2 f5 B6 ^. R9 P2 F- u    throw new UnsupportedEncodingException(: w9 N8 v/ |' L5 _
      "File is not in TXT ascii format");
- o8 C9 m$ U. V: u7 k/ n! t; K9 U( k   }3 e1 k" s2 s. c0 ?& z- R: U& @
   str = in.readLine();9 `1 g- s8 S* P, V
   String tem[] = str.split("[\\t\\s]+");5 A9 H6 i$ p  {. l% H, U
   xSize = Integer.valueOf(tem[0]).intValue();2 x9 V1 j& _9 L$ w5 b) T7 `7 j
   ySize = Integer.valueOf(tem[1]).intValue();
$ b1 e. p  \* \6 O6 Q   matrix = new String[xSize][ySize];/ X- s) R. a$ D+ i6 `$ T8 n
   int i = 0;
3 L4 x; C) x; k9 h' l) b, Z   str = "";
- {" E2 A/ D% N1 x0 X" j   String line = in.readLine();
8 m% l5 T2 K! ~( J, q  o# }   while (line != null) {
  L( V1 n8 z0 a    String temp[] = line.split("[\\t\\s]+");
7 [7 m6 v) Z' j8 c4 E8 c" D, ?# f% n    line = in.readLine();6 @0 O; k$ O4 O6 J. V
    for (int j = 0; j < ySize; j++) {
( o* ]# E0 {  y: V" L0 l5 q2 \, U     matrix[i][j] = temp[j];
- N9 N" ~# S1 q  r& x( R; X    }& c$ O2 M1 g0 ], t) z
    i++;
+ x- G: f& a( Z: T# J7 v   }
* o) n, P* X) z( u9 n9 V+ U   in.close();# C$ @4 P9 j* ?
  } catch (IOException ex) {
0 d  [- h) u: c$ u   System.out.println("Error Reading file");( d- z# @* |% L$ H3 u: R4 {
   ex.printStackTrace();
9 l# G  n) @$ w   System.exit(0);2 l1 v3 w9 n( g
  }
' i2 U' Y6 m6 X; _: V: [( }$ G }+ A! K9 T1 D+ i* O0 u
public String[][] getMatrix() {+ g( J4 D# n1 y9 ?& M
  return matrix;
% _* \! ~1 Y: X( ]: E" y }6 j8 O; f9 t; V' Y! P) E; @
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-18 02:30 , Processed in 0.014594 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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