设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9596|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
" i+ X% a) h" u' |0 z  ]0 vimport java.io.BufferedReader;
( x3 {8 n: J; B. ?2 aimport java.io.FileInputStream;
* p; r9 j/ s  @8 v$ G. s& p0 u/ l1 K- _import java.io.FileNotFoundException;
2 x$ t. r5 E+ S1 U8 f7 u4 I& A; eimport java.io.IOException;
9 z* H( \0 |' Z0 a. qimport java.io.InputStreamReader;7 G0 O$ A4 w1 G
import java.io.UnsupportedEncodingException;
. z4 P1 r" b; m. Ximport java.util.StringTokenizer;
' y7 \3 K+ }2 l$ ipublic class TXTReader {
; t8 S  x  y" ?; u0 k0 e) U1 v protected String matrix[][];; ?% d! ~8 c1 t$ A! I
protected int xSize;
' B, P; e  u$ c% H protected int ySize;
) v; m7 R8 E9 y0 U$ @ public TXTReader(String sugarFile) {' S% l" e# |( z4 F0 ?" w& f
  java.io.InputStream stream = null;! A& ?3 V4 h$ ?! W+ K
  try {2 A0 `) g3 [& P
   stream = new FileInputStream(sugarFile);* k9 ?, {% \9 c/ g. ~/ t
  } catch (FileNotFoundException e) {/ f  K- x; _+ R" I3 J: e: G
   e.printStackTrace();4 N# k5 W) q2 ?8 |2 k3 |
  }
/ ]+ `- B* ?- R# w6 N  BufferedReader in = new BufferedReader(new InputStreamReader(stream));' z& I1 h$ u0 k% n6 I4 w' U
  init(in);
( u! N: I4 v5 m# ^8 y }
& h1 d1 O9 W) k5 U private void init(BufferedReader in) {
; s: G+ ]# h, K  try {& V! ?; y2 t) h2 j' k( o9 t" h
   String str = in.readLine();/ ^- R4 U/ l; b! {( }: u# w# G4 I
   if (!str.equals("b2")) {+ B: U9 {2 x% H; T- o  X1 J
    throw new UnsupportedEncodingException(
. y# L- y, x$ u/ r7 w0 ^# b      "File is not in TXT ascii format");# m  l* B2 @6 Q
   }
+ T( C2 a2 B* U$ z3 D   str = in.readLine();7 ]9 z1 o9 M+ M6 X
   String tem[] = str.split("[\\t\\s]+");
2 A9 a) X+ k: \$ C+ o. h0 w; A   xSize = Integer.valueOf(tem[0]).intValue();% u+ s' H( e0 Y$ g0 _
   ySize = Integer.valueOf(tem[1]).intValue();4 M$ P$ m# Z2 @: ]# Z
   matrix = new String[xSize][ySize];
& m$ N3 P  t, s$ N1 f   int i = 0;/ H. z4 Y+ V1 X
   str = "";
" b2 T2 n2 e& t   String line = in.readLine();
& ^9 T% a. o! g0 L   while (line != null) {* v( x3 _0 h' ^( Y3 v
    String temp[] = line.split("[\\t\\s]+");( q5 y& X( K. m
    line = in.readLine();8 j5 j0 Y2 w4 c' k( T7 x
    for (int j = 0; j < ySize; j++) {
  H- U  y  T4 y     matrix[i][j] = temp[j];' x4 Z/ Z6 {. x+ X- n" r! J' u
    }5 H# s1 ^- O6 U& F0 g& v& O
    i++;
2 a8 k8 d( Z6 @9 n/ A" \7 b   }( o7 ^0 Y8 D. I+ v7 E
   in.close();
, S0 R& |2 H" b, o8 U  } catch (IOException ex) {
% O* d7 z$ ?0 V" I! Y   System.out.println("Error Reading file");
9 x" [( j" b6 s3 F   ex.printStackTrace();+ v7 @+ g, K- B) }1 f8 d3 M; _
   System.exit(0);0 M, `  x/ z: W) L6 ^4 D  B1 R
  }2 ~0 c9 H# R2 l. h# H4 v
}. w. W; Y4 T8 e$ j: l
public String[][] getMatrix() {
) \) J! L. r5 d2 X4 ?& L; Y  return matrix;. B- J  [( Z/ T% G8 U8 \$ I
}8 g; e! Y0 A3 j
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-26 17:13 , Processed in 0.015123 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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