设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8035|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;) R) G& _0 N+ p! s  Y' q
import java.io.BufferedReader;6 S  n; V2 @# J; t$ J" H" Q
import java.io.FileInputStream;" U% V1 |1 l0 }# x: z
import java.io.FileNotFoundException;( h- `! L3 y, {" ^
import java.io.IOException;! S7 b8 e3 w4 z
import java.io.InputStreamReader;1 z7 \& u) ~- }( r6 c
import java.io.UnsupportedEncodingException;. }# a4 V. D! P8 C/ u" Y% \; X
import java.util.StringTokenizer;
# }/ y: k' t0 Q% Cpublic class TXTReader {' ]" [; @/ y) K) B6 s
protected String matrix[][];
" @1 @3 ]5 s# p. ?' L" M protected int xSize;
9 f( U) u0 f+ J! Q- l protected int ySize;# h1 Z( \1 h+ U! {% R9 m
public TXTReader(String sugarFile) {
$ P+ f/ I' F9 Q! q/ n: g  java.io.InputStream stream = null;7 A3 x7 R( R- N) H6 A+ N2 S
  try {
/ @! |* N) v3 w$ ?' X+ Z   stream = new FileInputStream(sugarFile);
/ b/ ^* ^- l$ D" z' P  } catch (FileNotFoundException e) {
! I$ @: W/ f3 D3 @" S   e.printStackTrace();
% b( \  q( v/ F  _9 t4 s% N  }
6 ^. @& w# |# ?  BufferedReader in = new BufferedReader(new InputStreamReader(stream));3 @  f& j5 x  e
  init(in);$ ~: f3 x- {9 t7 o0 M
}* Z# I' h9 Z* ]& O$ g5 K
private void init(BufferedReader in) {. x! U- b. V& P; \( y6 }& m, d  m
  try {3 C) _5 x% x- E
   String str = in.readLine();* W3 [% w# {; d6 l
   if (!str.equals("b2")) {
5 {* C5 t8 p5 W% B    throw new UnsupportedEncodingException(3 ^8 c& c$ j$ B, S, U- l5 ^
      "File is not in TXT ascii format");+ e  d2 e: O6 _' |  K
   }9 A; j) h6 `4 d2 u  h
   str = in.readLine();
3 n4 U4 s0 h6 k& j   String tem[] = str.split("[\\t\\s]+");( s& w: M8 S5 X; U2 }: \; J- z
   xSize = Integer.valueOf(tem[0]).intValue();( P9 P  P. z( R2 F6 w
   ySize = Integer.valueOf(tem[1]).intValue();9 W5 O2 d1 E6 O7 z2 h- T+ |
   matrix = new String[xSize][ySize];
3 M" g5 c. n6 B$ A; l   int i = 0;4 V/ W' J6 k4 F0 G  Z
   str = "";
' C2 Q7 Z5 H( C, k# Y7 _8 i   String line = in.readLine();/ L2 j, \2 w: a/ u. S
   while (line != null) {# Y# U+ ^: G, J7 Q; z, R
    String temp[] = line.split("[\\t\\s]+");
$ g* X6 Q/ ~4 I! I    line = in.readLine();
9 @; ]/ S( ?' l* W; y    for (int j = 0; j < ySize; j++) {
' c& E5 k$ Y& s     matrix[i][j] = temp[j];
) V/ ~# w( g1 U8 R    }
7 L7 O' b2 d" V9 Q, c    i++;
3 A5 f; w9 W2 \0 Z3 Q   }
" `! k5 \% l! t1 b) e/ z! ^   in.close();
2 y# t) W) C2 H# \9 c+ ?  } catch (IOException ex) {
9 `( R4 e" K( P2 m   System.out.println("Error Reading file");0 a, C/ `4 L6 o% R
   ex.printStackTrace();
! J* i9 r. B1 M0 A7 E   System.exit(0);
% h% M4 R  |- o/ d- L  }; `) W0 Y# E1 H' E" K* T
}
* M' z: p" L& ?& e- M' |& P public String[][] getMatrix() {
+ V  l7 U8 u! e* E) e  return matrix;4 X) M  g) J* e1 t% k
}
5 ]2 ^. q% w4 ?+ [5 }8 H4 b) L}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-4-28 19:01 , Processed in 0.025336 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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