设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9840|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
- Z9 s  l' E7 ?  T5 Aimport java.io.BufferedReader;
9 H$ r7 m9 H( }( r% W+ Rimport java.io.FileInputStream;
: N; i- E# ?' }  p$ ^import java.io.FileNotFoundException;0 ^$ y  c% b$ U8 `: n+ Q6 f- e
import java.io.IOException;0 c2 R7 U5 l3 Z6 ^$ e  j8 z6 K
import java.io.InputStreamReader;
3 t$ x/ @9 `4 {2 G+ @import java.io.UnsupportedEncodingException;
' m- v0 P2 ]. y: dimport java.util.StringTokenizer;
/ G( X3 A8 T; v3 |& xpublic class TXTReader {
+ k- U0 u# ^0 O( `( v protected String matrix[][];  S( D- m# I% c2 H" O
protected int xSize;+ A" n- |8 g0 N
protected int ySize;0 ?7 J/ y5 k( Q5 P2 z9 @2 L
public TXTReader(String sugarFile) {- R: d$ ?% y5 {7 |4 b
  java.io.InputStream stream = null;
9 V! f, x) ^+ p2 t& G  J  try {  Z  z) p3 d: i
   stream = new FileInputStream(sugarFile);
5 n7 M, U% B$ \  Z1 I  } catch (FileNotFoundException e) {% e7 c& V' K4 v1 T7 W: a" s/ g
   e.printStackTrace();) g3 }1 I+ e  ~
  }
8 h$ j  y4 Z( K- F. u  BufferedReader in = new BufferedReader(new InputStreamReader(stream));! M8 y3 P* B% ~9 W
  init(in);6 e# F$ k- I4 c
}: Z& R. T! V% P9 }2 Z4 F) x; v
private void init(BufferedReader in) {; F2 j/ L1 @, p  [+ g
  try {
/ G& Y* i; Y: Y" R  q$ g9 X7 c! s/ I   String str = in.readLine();
: D0 H) P6 L: ^3 v   if (!str.equals("b2")) {
; T, ?. Q0 R* |( \) Q' I0 [# o5 F    throw new UnsupportedEncodingException(
0 _. {$ I+ [  U! u5 k! N1 b      "File is not in TXT ascii format");1 h# v+ u& R* ?! |9 y+ ^2 T
   }4 J( q# U/ z7 V
   str = in.readLine();
6 i& s& d' x' j$ u   String tem[] = str.split("[\\t\\s]+");
; V$ \* p* q. A! M% N   xSize = Integer.valueOf(tem[0]).intValue();
  P2 r; g: K) A1 o. R$ t1 I   ySize = Integer.valueOf(tem[1]).intValue();
( {& p* J# q; F( G+ w6 m9 V   matrix = new String[xSize][ySize];/ w/ v1 s' ^8 n9 Y
   int i = 0;
( V; E7 n: k; V9 I! I. l7 [. E   str = "";
% \) B' u0 Q: B6 m   String line = in.readLine();$ c; X. D7 [& }% u* C2 ~  a4 g
   while (line != null) {' D! S; e0 s3 r, r4 C9 K* e
    String temp[] = line.split("[\\t\\s]+");4 O2 N2 v4 d; O! S4 P2 y
    line = in.readLine();
  r- G+ k% @" z  H! h8 K) A    for (int j = 0; j < ySize; j++) {; A+ V4 W& [' `- k& G" c
     matrix[i][j] = temp[j];. C3 |  T( n) g
    }6 j  F* r0 g3 F8 D  I
    i++;( b3 A& H; K: h( I/ v
   }7 S1 T, B) [9 N7 V# F
   in.close();& Y- s, ^5 ]% ?/ E8 U# z
  } catch (IOException ex) {! Y1 j+ q0 l4 H4 b$ x
   System.out.println("Error Reading file");
/ Y+ R* [. `! ~   ex.printStackTrace();
& {+ x/ Q5 A: n# W   System.exit(0);
- z4 {+ l& f0 r' U% \" y0 M: Y  }) \" t6 V, |; A' H
}
6 v9 h$ R. i2 J( F3 E6 Z# W public String[][] getMatrix() {
6 p' G  ^/ r. p  i, l4 `" p  return matrix;  h. C% h; M2 \  p3 f1 W
}
5 u5 w7 _, _# R) r6 o}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-6 16:01 , Processed in 0.015102 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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