设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6633|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
; Z# f3 i: e: C, d+ W% S" p( }import java.io.BufferedReader;
. b9 b& t0 _6 \# E$ b+ Vimport java.io.FileInputStream;
5 ?6 l$ b3 {  ]# ~/ L! Bimport java.io.FileNotFoundException;
7 x: a* z4 D4 o3 j8 {, Eimport java.io.IOException;
) v! j- r2 I; a! N3 oimport java.io.InputStreamReader;
, B' K2 e  j. b! \0 |import java.io.UnsupportedEncodingException;) W! y( E) B: p1 [0 g8 ]
import java.util.StringTokenizer;* }0 E7 H7 E$ B- j& C
public class TXTReader {4 a5 b- B  |. Q5 K- z: ~2 G
protected String matrix[][];
0 u' r$ R5 P% n3 z protected int xSize;
, l+ v% Z7 u0 U2 {" s$ ^8 m protected int ySize;
+ x. V" G0 d! @5 ^ public TXTReader(String sugarFile) {7 O4 o) g' {. h0 n
  java.io.InputStream stream = null;
5 G/ f' M$ \" ?: ]9 R' I  try {
& T% F, G4 h; X& x   stream = new FileInputStream(sugarFile);- _/ f" j. B- M* f7 z
  } catch (FileNotFoundException e) {
7 T7 n0 m" [( y! C% M   e.printStackTrace();
( E' V- o5 Y. V4 g  }
4 P% U( g. g* X. g2 u/ B! x  BufferedReader in = new BufferedReader(new InputStreamReader(stream));, n2 H3 O) X7 w- f  p: ^2 f( X5 _
  init(in);
0 o& `1 a# \6 f* Y6 i9 P }" L0 i* p  s) [. Z
private void init(BufferedReader in) {
. y6 }1 Y5 R7 m0 g- y9 r  try {9 t" A, d' u' B9 o
   String str = in.readLine();
  c/ \/ Q4 b4 ~1 z# P   if (!str.equals("b2")) {
8 l5 o+ a9 a* J  v    throw new UnsupportedEncodingException(* T) J# s6 V5 ?4 `9 V8 e
      "File is not in TXT ascii format");
$ n; R% W  y% I   }' ^; y1 a0 e% Z' P/ V
   str = in.readLine();, k/ \6 w5 P& i  m- O6 a
   String tem[] = str.split("[\\t\\s]+");
2 Y% y) g. m& Y8 G' Y   xSize = Integer.valueOf(tem[0]).intValue();
3 ]& ^7 X$ e; _$ Y! F   ySize = Integer.valueOf(tem[1]).intValue();
% p3 D; A+ p: j% u   matrix = new String[xSize][ySize];
- M: A0 h- [: f+ _/ a9 {   int i = 0;
8 E% V6 P( L9 S# M  ^( m   str = "";
3 [% U2 N7 M+ c2 I* V& h   String line = in.readLine();; B# V( J  G. f1 G5 z! `
   while (line != null) {
, J# }; h: A0 z- |, }% q) ?4 K    String temp[] = line.split("[\\t\\s]+");
, _! E+ b6 d+ z3 s    line = in.readLine();  k; P# L' c% X8 Z9 z
    for (int j = 0; j < ySize; j++) {! M: J: Z" ^3 D5 f+ ]6 I
     matrix[i][j] = temp[j];$ `. I* ^  S' Z" L
    }
6 U, A- Z) n9 @* X" f1 {0 p    i++;
! `! Y) N( w! q5 Q) T   }* H& \9 ]) {  I4 v  n% Z$ ]9 k
   in.close();- K0 T" r# Z8 |, a# c' g; I
  } catch (IOException ex) {6 Z6 _, m6 d7 s8 v
   System.out.println("Error Reading file");
9 x9 }) n4 _1 L   ex.printStackTrace();
% l4 j# W+ V5 O9 a; N0 x$ x   System.exit(0);9 I$ F# s- ~% x0 g! o% m3 Z
  }$ p' j/ d5 n3 J! A& {+ u; R
}4 S! a2 x7 M. m/ [0 T
public String[][] getMatrix() {
% g. [8 u( {% z" T$ g  return matrix;- U$ O( w8 S# i/ r( g8 x4 u( h
}' i) g& G/ D+ h) L8 l
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-23 22:50 , Processed in 0.018125 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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