设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6983|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
8 j# z. J6 t, [$ i0 T: S* `( ximport java.io.BufferedReader;/ F  _" T! G. J+ F
import java.io.FileInputStream;
: p  m" F/ x" f' z% \" Mimport java.io.FileNotFoundException;* H7 {! @1 }/ Z1 N* b
import java.io.IOException;
' D4 g& S1 D! q4 O' \9 Yimport java.io.InputStreamReader;
3 C) Z+ a  ^  m, {import java.io.UnsupportedEncodingException;
% L6 M# [6 m7 \9 f& Nimport java.util.StringTokenizer;1 l( b% c. A6 _& f
public class TXTReader {) w' ?0 q+ Z' v8 {) P
protected String matrix[][];- Q% g% b. i% s; Y% s2 S
protected int xSize;2 q! [0 e0 ?- Y% A
protected int ySize;9 G. I0 [% ]! `' x) H0 C' f
public TXTReader(String sugarFile) {2 n( Y, ~+ o( S0 w8 F6 X0 S0 r+ y
  java.io.InputStream stream = null;1 \: O& ~" N. R" Q/ W
  try {
: ^3 c8 _) S' E+ s. S   stream = new FileInputStream(sugarFile);  ~  A) |0 p5 ?
  } catch (FileNotFoundException e) {3 d$ W" J! T& K# E0 o
   e.printStackTrace();  L, \- I8 Q) V
  }
& K7 B* \% H6 @# C' t  BufferedReader in = new BufferedReader(new InputStreamReader(stream));3 p3 E' a: {- c# s! u
  init(in);
; Z7 q( p8 f. ^( B( H }' I! g7 `2 d# F
private void init(BufferedReader in) {/ `7 F6 Z/ V2 M% g; N0 u* {
  try {0 o1 }4 f5 p$ K* K1 X/ u9 q
   String str = in.readLine();
3 C9 G: n0 Q; [% D, u   if (!str.equals("b2")) {
5 {+ h3 o; c2 P* b8 p0 n! a8 t    throw new UnsupportedEncodingException($ w- y$ y& Y4 d. b
      "File is not in TXT ascii format");/ q8 Q4 S1 W* t" |1 V- P2 J2 S
   }. t! G$ `+ Q0 C# T& d8 H
   str = in.readLine();& r# t6 v0 q4 \' E7 C  Y
   String tem[] = str.split("[\\t\\s]+");2 |& d) L7 t9 t6 N% h- n
   xSize = Integer.valueOf(tem[0]).intValue();
% H6 W$ x9 L7 C   ySize = Integer.valueOf(tem[1]).intValue();
  q+ m: i6 Q$ m. v   matrix = new String[xSize][ySize];, \  M1 H% _) J2 ]) M# p
   int i = 0;
2 l7 x/ s1 C. ~: A5 P+ o   str = "";
$ ]: Q1 n0 V# O! |   String line = in.readLine();" B. i% }0 A6 d
   while (line != null) {5 O) m! N5 v. X3 s# u
    String temp[] = line.split("[\\t\\s]+");. _& q0 C" V4 B
    line = in.readLine();5 p' Y) B: P( W
    for (int j = 0; j < ySize; j++) {
% n5 v4 M% n5 k2 `3 w     matrix[i][j] = temp[j];
" A' @: V* _0 k0 ]7 z' h; B0 T7 m    }$ _- w8 m& ?* S( r/ q
    i++;
$ G: ~: v! h0 O1 k+ \   }
1 @% T1 e7 d9 g  j" e1 W* \) B   in.close();5 J- M  S  e# }
  } catch (IOException ex) {4 p/ h+ J3 y4 X+ P, V
   System.out.println("Error Reading file");0 i( s1 A. ^* [
   ex.printStackTrace();& p: _( L7 d- e9 ]% s
   System.exit(0);
+ ~7 v& i) w- y6 H( o9 \. o/ ]  }! K3 r( D! R- q: s
}* n( c2 ~& [- N
public String[][] getMatrix() {
2 e, z/ z! j* N) I5 t  return matrix;
) H! w/ ?8 I% G4 |4 g }) k! }$ l: D5 @2 w* F- J
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-13 22:39 , Processed in 0.022794 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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