设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7243|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
  g# Y. I1 M0 M7 e! l- ?import java.io.BufferedReader;+ s, S5 V& _  ^* |! U# T" r, ^
import java.io.FileInputStream;
3 E0 K% L% b6 q4 Qimport java.io.FileNotFoundException;/ [7 [/ F/ m8 u# q+ f* r( \5 _
import java.io.IOException;4 Z! |, T" }+ z
import java.io.InputStreamReader;
( g( o! A' b: P, a4 Mimport java.io.UnsupportedEncodingException;
/ q4 G# C( ~# C: W) E% simport java.util.StringTokenizer;( ~+ o+ T% |& T% z0 E
public class TXTReader {1 `) K: Y/ @: S. I7 ]+ ^
protected String matrix[][];1 P) T5 q# Y' i! ^" Z3 T, L2 p
protected int xSize;7 C  @- r' N# o# J
protected int ySize;
5 ]3 d: G! i  t8 h6 q7 d2 I/ ] public TXTReader(String sugarFile) {0 r5 S; X; C# c4 w% ]- e
  java.io.InputStream stream = null;
# _5 k, Q4 D0 O2 t" s" Q3 a2 H  try {
0 F& Q9 [, ^, j4 G   stream = new FileInputStream(sugarFile);
" Z  k, h- {( e. d  } catch (FileNotFoundException e) {
7 n2 }& m4 F: E9 {, f   e.printStackTrace();( `6 S4 ]0 X6 {+ o. M7 W7 N4 k' j
  }! y' O! Y3 i% ?9 ^7 @& f) i0 g
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));6 u/ q& t$ f+ O; b6 @
  init(in);
5 D# V" t  N( o }
$ c0 ~9 c  D3 Z4 t: w private void init(BufferedReader in) {  n- }' s' M1 }/ M
  try {
8 T7 d' C0 t. A   String str = in.readLine();! k( B2 D& ^% B
   if (!str.equals("b2")) {
: m) }; P8 Y* u3 h/ j% t    throw new UnsupportedEncodingException(
& c8 ~0 O+ \9 U: c. ]      "File is not in TXT ascii format");
( Z, g$ o5 V' H! u   }2 ~5 b7 K; t" m: |
   str = in.readLine();
; w+ q4 Z2 p, v' @& @& z; x# \% r   String tem[] = str.split("[\\t\\s]+");+ @( _% K4 `- k5 I
   xSize = Integer.valueOf(tem[0]).intValue();
1 G, g) z) o, A" e8 ^1 k3 U   ySize = Integer.valueOf(tem[1]).intValue();* L/ {: N1 G, O4 h" H1 P8 t8 e" U
   matrix = new String[xSize][ySize];" @: V1 t& z: l1 Q' T; m
   int i = 0;: k0 @2 j; |5 w
   str = "";
% ^+ k, g  p! \1 A, {- S, {, B* G   String line = in.readLine();1 d  P  j$ R. q1 n" x# ^% C, u
   while (line != null) {
, x& W3 v! N" V# m' @9 U( N    String temp[] = line.split("[\\t\\s]+");
( T. M$ O4 b7 m$ J: I5 I. J    line = in.readLine();) }1 A: [% l3 r: J3 [9 b
    for (int j = 0; j < ySize; j++) {1 b$ d5 O1 }6 G  Z2 ]
     matrix[i][j] = temp[j];% ^  i4 g3 {* w- _! k# [
    }9 n* }9 |  ^& w7 G( [
    i++;
' M) _8 f1 T8 ^4 H; q   }2 Y8 O; N' [' S  a* [
   in.close();* Q9 u! |1 }" V8 k* l3 p
  } catch (IOException ex) {
6 v# s7 {: Y. Y7 G/ l) C9 d$ R   System.out.println("Error Reading file");
6 O. L5 D$ Y9 C* l; J4 ^, T. v   ex.printStackTrace();
( _& S% h( n, p2 K: P6 G& c   System.exit(0);% x% c/ H# v) ~& N1 @6 a1 d- ?
  }: _! Y2 R3 L1 d& P2 \5 H
}- t# r: X3 ?$ v" c; A
public String[][] getMatrix() {6 k2 W7 b9 k( s6 a# u$ K
  return matrix;
5 ?! Y1 h5 ?2 Z! X  _# \& d' y }  D' b* E8 M. B
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-2 07:38 , Processed in 0.017645 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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