设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6412|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;! Z# X- N% ~1 [0 u
import java.io.BufferedReader;
  T* L6 M  Z" [7 Dimport java.io.FileInputStream;3 n3 w9 l9 V1 n% f/ Z/ K2 d% z; \  r
import java.io.FileNotFoundException;
) a* T" Y. n, r" Limport java.io.IOException;& Z; A% g7 d/ z0 K
import java.io.InputStreamReader;
3 Q" Q- h, g/ r+ Q9 {9 Limport java.io.UnsupportedEncodingException;3 u9 Z- y8 E: }
import java.util.StringTokenizer;6 x3 d% ~0 T' J% T0 C% ]5 i
public class TXTReader {
8 M1 O5 Y+ X  k/ T) x0 c protected String matrix[][];$ v$ T; G( k  j8 L% j
protected int xSize;; |# y- J& t/ P( h5 w7 Z
protected int ySize;: g9 ^4 N# p! j8 B
public TXTReader(String sugarFile) {
) v. L& _1 F/ R$ ]  java.io.InputStream stream = null;/ l: c; q4 m$ X9 A! }
  try {3 k  D& N/ k5 D# D- v
   stream = new FileInputStream(sugarFile);
6 X5 G3 `& U8 X- S8 z  } catch (FileNotFoundException e) {
9 T4 o. r' C0 R0 y   e.printStackTrace();
& |9 ]5 s' F. P8 J/ F& m9 ]. X! h  }$ i3 c9 [: t, x' Y! B2 ^* j
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
' L& E; G* o) }0 [* k4 m  init(in);. c# O: r$ p2 t+ g
}! l# c- C) j9 f/ H0 Q6 G# P# h
private void init(BufferedReader in) {
$ K$ J. I* J5 T  try {
/ `7 d* D) U  w( y6 e   String str = in.readLine();
* {: e( t0 c5 N: U, H   if (!str.equals("b2")) {
) ^7 d# |" A  c( F% k, x    throw new UnsupportedEncodingException(9 n6 `5 w& I2 Z
      "File is not in TXT ascii format");' D" J, O, ], v
   }- ]3 a  C  {' v* v7 C2 P
   str = in.readLine();
5 f" U: ~7 `9 @# K   String tem[] = str.split("[\\t\\s]+");
2 t0 _2 A* U: t5 B3 b/ E   xSize = Integer.valueOf(tem[0]).intValue();
# S$ n3 x- f' y3 Z$ y8 U' e   ySize = Integer.valueOf(tem[1]).intValue();
* ?5 z" ~2 A1 F   matrix = new String[xSize][ySize];
) J* f) g& P9 e' A6 u   int i = 0;: l7 X& |2 C! k9 m( J2 ]
   str = "";
, ?( D2 y; T, D2 `% w9 R& F! i   String line = in.readLine();
% D8 @! W4 C6 F   while (line != null) {  Q: S. I5 L1 z
    String temp[] = line.split("[\\t\\s]+");7 j( D) U2 t4 u, B6 K
    line = in.readLine();$ U' l1 V8 i5 H  x, Y0 [" c) a
    for (int j = 0; j < ySize; j++) {
0 e% h& r5 G6 `" K7 t     matrix[i][j] = temp[j];$ e" Y8 I5 O4 k  u8 r0 X
    }% u' d$ {4 N$ Q
    i++;5 q: p* `; c3 I5 }2 i; Q
   }
3 _: I1 w- e, u. A   in.close();
% W9 m" d2 l9 [2 h1 A" A  } catch (IOException ex) {0 s% L. V) w! T# r
   System.out.println("Error Reading file");8 L  J+ H  \4 _. ^4 }9 I4 n9 J. S
   ex.printStackTrace();
* A" ]* U2 P  T- I4 }  h4 M+ W* h   System.exit(0);
, C- h7 H1 `6 }3 a  }
- Y+ H& g& E' Z/ a7 g, m( O }/ h  q- ?7 m2 l7 g) {
public String[][] getMatrix() {
2 G% m( O' }) s6 h+ g% X! d% |  return matrix;: ], G. e" b% y7 V0 o3 T
}2 S' |/ r$ \( |; L4 i
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-7 22:17 , Processed in 0.020722 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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