设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10026|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;6 g2 H8 q4 Q" ~5 I5 |4 c
import java.io.BufferedReader;9 N: R. G3 {& k6 w
import java.io.FileInputStream;: w% p, C  C3 i7 k2 X( k  T, t4 o
import java.io.FileNotFoundException;* R; x) b9 R, n
import java.io.IOException;4 W) ^8 L) s9 [5 [- S. o. K, V! v
import java.io.InputStreamReader;
, j. R4 G- c0 O  ~% vimport java.io.UnsupportedEncodingException;" G2 s) m2 A* w" J5 v& ?7 J
import java.util.StringTokenizer;& m6 C+ ?& i5 d
public class TXTReader {; e1 @+ `( o1 q4 p7 I. e
protected String matrix[][];1 f, b& v! U* X
protected int xSize;
' l' o' p/ a6 c protected int ySize;; u; Z  |2 A) o$ @) W  \
public TXTReader(String sugarFile) {
; t9 F; C$ m: p$ T& d4 {( S  java.io.InputStream stream = null;
: D6 n" R. @! n  try {
: q3 P* N, e8 Z% J: V   stream = new FileInputStream(sugarFile);
4 C& z2 {# r1 H  } catch (FileNotFoundException e) {: _0 _$ q0 h( ]: c: ^
   e.printStackTrace();
. {7 D0 s% Q8 L! C* d  }
4 O, g: ^3 ~  t0 F% r1 e% W  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
" ?$ J9 F: c4 v- }& }% V" o  init(in);' e% c" g% i/ w. @
}
$ h6 R% e) z+ M0 K private void init(BufferedReader in) {  p  y7 E9 O; P  n$ A  z
  try {
/ q. a/ _' b* L$ [: @4 ^& A   String str = in.readLine();
2 N: s: i1 d: F+ \- [& H   if (!str.equals("b2")) {
- @0 y8 Z7 c$ A& G7 ^    throw new UnsupportedEncodingException(
: [$ P7 n' s6 i3 k0 `3 H. u2 E3 [      "File is not in TXT ascii format");  L$ @: q& ?* d3 @( C
   }
1 [" b* l$ a' r3 }8 n7 y   str = in.readLine();  F7 q4 s# U- _  a$ y( m' @8 K& N
   String tem[] = str.split("[\\t\\s]+");0 s% p2 z6 Q7 a& }6 P+ D
   xSize = Integer.valueOf(tem[0]).intValue();
. B, [9 b+ [3 L+ l   ySize = Integer.valueOf(tem[1]).intValue();
" b: k+ U& X2 \  p" J- O   matrix = new String[xSize][ySize];
1 Q1 Y& c2 l" S0 ~) L. l& L1 M   int i = 0;# X7 i3 q) h! I8 r" ^4 j3 z7 r
   str = "";2 [2 |; l  c' m5 L; |. I
   String line = in.readLine();9 O# y6 |& \7 g, @  b  b% t9 A
   while (line != null) {
7 h: [; e) O7 R' L8 k  b  R    String temp[] = line.split("[\\t\\s]+");. a4 c7 N) b% W  v$ U" J
    line = in.readLine();+ N/ i. k- G- u8 \% r
    for (int j = 0; j < ySize; j++) {. h: v2 v+ [7 M8 X3 ^
     matrix[i][j] = temp[j];3 i, g2 n2 @$ r: c# N# o* w6 S8 H
    }" ~! \; A& B/ e# h4 u. b
    i++;
; O) i3 t$ ~" k7 y   }
, v0 D: }1 [/ D& Q. N+ ~   in.close();
) r( h' o- ^: x1 d  } catch (IOException ex) {
$ R2 q% I: m0 Z: w   System.out.println("Error Reading file");7 @# Y* h2 c+ t9 f* o
   ex.printStackTrace();8 R, K6 l3 O! j1 j
   System.exit(0);
, R5 E. ?/ b# q  A" Q  }/ I( H- R! V7 K+ d" J3 g( E
}0 q" C( z% }4 D9 i
public String[][] getMatrix() {6 Y' e7 I. E: N- @, V+ S- d
  return matrix;
$ O: m0 M4 n9 A( G: ]* H& @ }: I4 N; P. Q8 l
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-17 11:24 , Processed in 0.022129 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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