设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8624|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
7 v8 {$ K6 `( d& q/ h) b$ G% Rimport java.io.BufferedReader;
2 F1 \; f7 S! r1 H/ }import java.io.FileInputStream;
& [. }7 P- w- ^import java.io.FileNotFoundException;
4 d' Y. g) x/ \( p" P# I# Pimport java.io.IOException;
$ X" S! W& m; p) B4 Z& Dimport java.io.InputStreamReader;
# b) H2 A% f- aimport java.io.UnsupportedEncodingException;/ Z0 ~8 T+ s! S" _" ^) C
import java.util.StringTokenizer;+ P' `- P2 K: \$ v2 [
public class TXTReader {5 L' k% k! i, Z8 X! {' v. T
protected String matrix[][];
1 I7 Z- c" `" P( t protected int xSize;0 A9 h1 H# e. u9 A, R, `
protected int ySize;' j6 a  a- b, X* \( j  P
public TXTReader(String sugarFile) {
, P' \* u$ O$ P" O% e1 g: M9 s6 U  java.io.InputStream stream = null;. \* b0 D8 U8 p
  try {/ M: ~  n4 I6 H
   stream = new FileInputStream(sugarFile);
# _- m1 Q" f; T" }9 U  } catch (FileNotFoundException e) {
* U) ?( u6 G+ A* ^* M   e.printStackTrace();) \) Q* b# e) V0 `
  }
) b) d* g+ e& C4 K4 ]* ?  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
% w0 u9 K. p+ t  init(in);
- p' e( L7 ?3 Q4 g) W) B  m }
! W/ o) Z! E4 L, K private void init(BufferedReader in) {
5 n# H! Z; c1 T0 M7 _  try {
& f. z7 m  t# F# G( q" D   String str = in.readLine();
3 Z& w5 s# U1 X   if (!str.equals("b2")) {
7 ^- H' [0 ?: b    throw new UnsupportedEncodingException(* l: y5 R- R# Y, T: a4 ]5 I
      "File is not in TXT ascii format");: A: s; m  B8 o5 }% b7 C  l' K! K
   }
2 B* f0 x# R2 ]   str = in.readLine();( ~. n/ S5 L1 i3 D
   String tem[] = str.split("[\\t\\s]+");
* L: \% y. i. L; i8 E9 }   xSize = Integer.valueOf(tem[0]).intValue();8 c/ o! Z, `- o9 }+ l% b. M" F
   ySize = Integer.valueOf(tem[1]).intValue();! h3 \- r0 z* i+ N& s0 Y
   matrix = new String[xSize][ySize];
' k! |, R; x4 W4 ^   int i = 0;
4 Q# G, n. O: ?* Q$ m   str = "";  `1 r1 ~, O2 I5 i. T8 y+ q
   String line = in.readLine();
4 O8 K8 R" ^% |! C' z, I+ n   while (line != null) {
% d% u0 x! C) f    String temp[] = line.split("[\\t\\s]+");
- y! o  c# K6 D" P& Q0 i    line = in.readLine();
! Y  p) C6 Y5 ?; f5 D    for (int j = 0; j < ySize; j++) {
8 F, \# K% M& j0 y2 N* C     matrix[i][j] = temp[j];
+ i# C: P4 E( K$ X4 {# w- F$ n    }
: _: ?, e) m! \/ [' }    i++;
6 z' p5 S# g9 O9 O  m) E( K* }   }
" b) [* Q& W" n& j2 ^   in.close();
5 q, k( W; {" |% c$ S2 H. z  } catch (IOException ex) {
: L0 h" K8 {5 A2 l   System.out.println("Error Reading file");
; M; I) M5 b/ t% }' s4 N6 W   ex.printStackTrace();7 e( ?2 N- g8 }% ^+ g# l1 R* {. d
   System.exit(0);% U3 }9 k' t/ ~2 n3 W/ M
  }
. W- M# W* c6 n: D7 q }
+ a- a) F. a, }1 Z& d7 n2 p$ G/ I public String[][] getMatrix() {4 {2 e# V% o: @1 H5 J
  return matrix;
# z4 s/ f% }3 v0 L }
; a  K  ^! r: Q# d4 n# d$ D5 V}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-4 06:29 , Processed in 0.015018 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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