设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6650|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
7 y: }: H2 Y# B0 Q- S- T" qimport java.io.BufferedReader;
* C. T1 \- O. a& u8 Nimport java.io.FileInputStream;
8 f/ x' O9 [  L8 i! O1 L2 dimport java.io.FileNotFoundException;, K4 v2 ^8 {+ W- B* Y
import java.io.IOException;
6 K2 P% a, h2 }, h5 N, Z- limport java.io.InputStreamReader;) a; D7 Z! r- P/ U0 Z" N% e
import java.io.UnsupportedEncodingException;
$ A. P' R  S/ Mimport java.util.StringTokenizer;
1 |5 c% a/ Q" R; apublic class TXTReader {
4 {8 P" s% h5 M7 I$ F3 N protected String matrix[][];
% ]# B5 K! {6 J& I$ U protected int xSize;
1 U: ]7 e; P" J3 i, w, L: @ protected int ySize;! M) G2 g% E+ f3 n1 M, C. u
public TXTReader(String sugarFile) {9 ^! T" N) o& G9 |) N+ b, y: ^
  java.io.InputStream stream = null;4 Q2 p6 B1 P) j8 F0 u; d/ W
  try {. y. T9 O9 v8 }% ^, `* J$ i3 P
   stream = new FileInputStream(sugarFile);
2 e: u/ I- m  J; p1 I4 o/ X  } catch (FileNotFoundException e) {, y" W! {* K1 P; }2 W8 j
   e.printStackTrace();
# U- r: d* X# Q+ f: h- \  }( g  K5 q; y8 Z0 z! Q# N
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));5 D6 ]7 }. b8 ]- T
  init(in);# h. m8 }# l5 Z! b
}
* `$ Y' D6 B, r private void init(BufferedReader in) {
. {) r' H1 Z, @/ l, I0 k$ f  try {
. I8 x. |1 u3 q) @$ J, ]4 N   String str = in.readLine();/ w8 F) Y/ I" y+ n3 z
   if (!str.equals("b2")) {( I2 F9 G/ k8 Q1 y: a* @8 S5 ?
    throw new UnsupportedEncodingException(
, J; o# q! l/ e      "File is not in TXT ascii format");
! G* k6 u/ d$ f; k& a8 X8 v1 ?   }9 i2 H1 H! a" c1 {+ \7 m$ P$ S
   str = in.readLine();  ]% [( ~* K+ r( ^
   String tem[] = str.split("[\\t\\s]+");) X  O. S, r* l0 f! L
   xSize = Integer.valueOf(tem[0]).intValue();+ V6 }  D: Q# Q$ ]
   ySize = Integer.valueOf(tem[1]).intValue();2 M7 R* N  c" a
   matrix = new String[xSize][ySize];
9 r. |& a( p  X0 S  [   int i = 0;4 ^" e+ j2 H9 G7 o4 ^& \
   str = "";
: x- ^) `' a( j   String line = in.readLine();2 l8 g2 [8 \2 O, M( B
   while (line != null) {! c1 ?1 A2 w5 y- x* p; F
    String temp[] = line.split("[\\t\\s]+");3 \2 [0 S, I* I  a* P
    line = in.readLine();* u! s4 z2 o' L) P
    for (int j = 0; j < ySize; j++) {
! n; G1 F- G5 I! k" ]$ c     matrix[i][j] = temp[j];
( x1 K2 s# V* q% [    }- C+ H1 ^6 d6 @- W
    i++;- Y: Q& ]& M+ \! s* f, R" F  Y
   }
# q5 P$ c. t! Y7 M: m( s   in.close();
* ~. f8 B" b& j1 F& [" u# W  } catch (IOException ex) {
' ?& _9 T! J" n; \# R   System.out.println("Error Reading file");1 a% l& y' u. J5 s8 |8 k
   ex.printStackTrace();
7 u. c' p$ ?  W7 D( \+ w   System.exit(0);
2 m" G  R% }2 U* Z, c  }0 k6 v& h4 P. I0 g7 r
}
, k9 F& b3 X. Y& |& R public String[][] getMatrix() {
8 N( G5 J3 E; k4 l+ q) V0 z  return matrix;9 b8 |: o! M+ f$ P
}9 F! o) i( d; o
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-24 16:16 , Processed in 0.020094 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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