设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10064|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;* ~% U9 y* F5 p2 q% M0 c! i9 E
import java.io.BufferedReader;" Q0 M: q8 X% l3 M
import java.io.FileInputStream;
- g( c6 u1 a% k* qimport java.io.FileNotFoundException;
; ~+ j. l2 |9 ^7 {) Eimport java.io.IOException;
* p1 A# ?6 \7 j2 `: H. B9 simport java.io.InputStreamReader;- p' Q- V0 c. l6 j0 c
import java.io.UnsupportedEncodingException;
) }4 K/ v) w) F1 I, E! r; W6 Cimport java.util.StringTokenizer;+ h: N& i9 d: u3 k! W- |) _5 ?
public class TXTReader {
3 j) J( w. @+ G# W3 H protected String matrix[][];
( X' f% \6 c4 y. j protected int xSize;
/ e% a. \- A: x8 v! q% S) c protected int ySize;
4 W' K! n- T2 b, x" S public TXTReader(String sugarFile) {
$ k& Y# f4 B3 R1 o6 ]  java.io.InputStream stream = null;
& f+ y0 t" H1 I6 r+ c% [  try {
3 c( X$ b; ~* ^. s0 V5 ]( C   stream = new FileInputStream(sugarFile);
' |) ?( m, G9 E6 y  } catch (FileNotFoundException e) {6 D  A; j6 _# r+ a- J
   e.printStackTrace();
  l9 A5 z8 Q7 ?2 b6 N( a  }
, \9 h7 n. Y/ e  z  BufferedReader in = new BufferedReader(new InputStreamReader(stream));& g+ y7 m3 P: o2 J" E0 _7 L7 c+ |
  init(in);
3 N" e2 u! i+ l& {; L4 m4 k0 a }, i: [1 _% N  v) q2 h2 _6 e
private void init(BufferedReader in) {
7 Y; p1 o: J. ~! e  W  P  try {1 z) `$ `! ~' \' N, H$ r4 C
   String str = in.readLine();; Y2 E9 g- J, V1 ^' D- \
   if (!str.equals("b2")) {
: z9 j- l: H. O+ k    throw new UnsupportedEncodingException(4 L8 a6 b6 o2 G
      "File is not in TXT ascii format");4 N# a+ G: k- a5 D6 t- c6 o
   }
# D6 |9 K8 w$ u+ ^* ]3 y. k( K   str = in.readLine();7 L" @0 _8 U! A' Z; D8 C5 d8 v
   String tem[] = str.split("[\\t\\s]+");
0 V: k4 x/ q/ g6 b' O   xSize = Integer.valueOf(tem[0]).intValue();8 n2 \+ p. T+ w4 {) v# g
   ySize = Integer.valueOf(tem[1]).intValue();) T4 R3 @' R/ n% Z
   matrix = new String[xSize][ySize];. \3 E6 M% n5 R" l* B% ?+ ~4 d
   int i = 0;+ ^5 V3 k9 r1 X3 a
   str = "";( g' X5 E8 u" \% ]
   String line = in.readLine();
+ E3 ?. S1 P" D3 _( n1 M   while (line != null) {# H2 F' `* j4 q# t7 f
    String temp[] = line.split("[\\t\\s]+");
9 f  l. E! ~% [    line = in.readLine();# z/ d: w- g2 ^$ S+ ]+ H( F
    for (int j = 0; j < ySize; j++) {
' D( K; x9 K8 J2 F' y; e  ^: |2 Z     matrix[i][j] = temp[j];+ B0 X  n8 Y5 f5 x
    }  {& |8 F: G% |% g9 D, [6 E5 G8 z4 M
    i++;
  }7 n- P% Q. h5 g2 O8 B* _+ t   }" m8 d9 N; Z: g8 N. {7 q8 {5 G
   in.close();
  F& @% U# T5 _  A2 I) M* L  } catch (IOException ex) {
5 S- [4 V! w6 Q7 ]7 P- j# b   System.out.println("Error Reading file");, U$ A" e2 x: E) L$ t4 \
   ex.printStackTrace();0 t8 z" i+ L# s0 I/ Q5 t3 t# E( t) Z- y
   System.exit(0);
0 U* k) n% [- W+ q+ n3 |  }
. l9 G: l  ?( G }
  z: }/ i5 b- V! G9 u public String[][] getMatrix() {
- c$ ]: L+ m5 K  return matrix;) O. m  n) Y# r  m- Z1 q$ f
}7 ]& b* a7 P, j! D
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-20 07:25 , Processed in 0.016244 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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