设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10660|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
# e( k) s# \; N: h" M3 I* Simport java.io.BufferedReader;. y, x& t9 i3 {! @4 T
import java.io.FileInputStream;
# T, O1 d2 r" F5 c1 ?5 rimport java.io.FileNotFoundException;0 D# l9 {9 }2 Y. ^# f7 t# {
import java.io.IOException;
- _  \0 ^0 {2 i1 v/ z- eimport java.io.InputStreamReader;
& y7 S+ r, K) @' `import java.io.UnsupportedEncodingException;
  D. l$ _8 d* i9 k+ y3 w# pimport java.util.StringTokenizer;0 Y" T; ?" }2 {: V  m9 w- v+ ?
public class TXTReader {5 p  ]( D3 G! F
protected String matrix[][];
2 T4 w. g9 H. D) G2 Q7 X protected int xSize;
0 E* g8 a/ c0 o protected int ySize;6 M+ S% T+ j& c7 w* t) U+ j0 `4 M$ ?
public TXTReader(String sugarFile) {- c( V6 `/ W. ^" O; ]! g
  java.io.InputStream stream = null;
3 J6 B3 s9 Q- w/ H/ J  try {7 t! [) D# ]+ y! j9 N
   stream = new FileInputStream(sugarFile);( p3 A# ]: A* L, z( i3 X
  } catch (FileNotFoundException e) {( ~  B5 Q7 b! |- k1 u
   e.printStackTrace();7 ~. g9 n' u5 T" D+ P
  }
- r; @" c! R4 {' Q  BufferedReader in = new BufferedReader(new InputStreamReader(stream));! g) i6 X' P' r
  init(in);
$ N9 l" a, _: j) s1 t% `8 ] }
- ?! C7 H4 f+ U" V private void init(BufferedReader in) {
8 a# R7 b) |0 j; S( z0 ~4 H  try {
* d  }' Y- ?: E1 J2 x* Q* B5 t   String str = in.readLine();
- V5 u- q9 E+ V+ i. J. J" `& Q! a6 M   if (!str.equals("b2")) {
$ c6 C' a/ C* ~- f6 T7 F3 p    throw new UnsupportedEncodingException(
! a4 K; `# V7 c5 x2 F4 c6 [      "File is not in TXT ascii format");; q8 N$ G9 k( k  O
   }7 y& x  y; }1 q$ \1 d) {* N6 a8 s
   str = in.readLine();' y7 o) a% ?4 |+ N1 a! U+ M
   String tem[] = str.split("[\\t\\s]+");5 ]9 y( N( _6 |7 P/ U6 n! X: P' m
   xSize = Integer.valueOf(tem[0]).intValue();. S& s- x" `+ Z
   ySize = Integer.valueOf(tem[1]).intValue();
! Z0 }/ t$ v: o: z  [( _   matrix = new String[xSize][ySize];
4 `  Y# C# d8 W. Y, c3 z9 u# }/ {   int i = 0;) |9 p; M* ]# v7 A, @
   str = "";
7 C0 F6 ~6 f  K9 S( x   String line = in.readLine();
% \/ n" b7 i6 Y, u! U# L   while (line != null) {( g4 R+ ?5 l: P. B
    String temp[] = line.split("[\\t\\s]+");- Y* I6 _: j3 A( F/ P/ |
    line = in.readLine();/ B$ z2 o$ u& Q! V0 [* F! E
    for (int j = 0; j < ySize; j++) {
$ s, @3 f. z) d" g, A% L     matrix[i][j] = temp[j];7 {' `- \6 D; z# p: r
    }, a* P' B7 H0 ^
    i++;
: g3 d! q7 ^, }  f4 O* _: `   }
, Y/ F. T# X: v) N/ j6 d   in.close();0 Z2 ?! V; z; A' c; F  x& {! f0 g; @
  } catch (IOException ex) {/ b3 O2 i3 }& `
   System.out.println("Error Reading file");
' d+ b& p% r8 D" K# C9 _$ o   ex.printStackTrace();
8 v1 g, a+ K; e8 t$ M4 w6 B% h   System.exit(0);6 b/ w, \' c0 i* d
  }" p- L# r  \1 W, W0 v* b
}
0 p% @% y- m3 A7 S2 J public String[][] getMatrix() {9 _2 n- S" c4 F9 i4 d! e/ _
  return matrix;5 I5 s1 B4 j: R9 t# Q8 Z" @
}
: M/ b0 r  k# f+ Z}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-21 10:33 , Processed in 0.014788 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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