设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9473|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;% V' o# g, n8 H7 A: v& m
import java.io.BufferedReader;
& d/ r5 e8 {/ W& @import java.io.FileInputStream;- }* o: a4 S$ }* Y* u1 D7 L: ^7 i
import java.io.FileNotFoundException;9 v3 O6 r3 U& l3 O9 f; X
import java.io.IOException;
' ]2 f# U6 h8 m) m- dimport java.io.InputStreamReader;
+ f6 Y# M+ `5 i- i5 R0 c4 z1 Simport java.io.UnsupportedEncodingException;
3 ]# T$ y3 v  ]0 limport java.util.StringTokenizer;5 B* x1 l) w' t/ h6 M$ G4 n$ M
public class TXTReader {
: S' z( f' P0 G! H& q; p protected String matrix[][];
/ B* i" \, s& d7 P+ A, { protected int xSize;) Z/ ^) [, ~3 u" N
protected int ySize;3 s9 ?2 r; ~8 B- \4 M
public TXTReader(String sugarFile) {
: ]5 w$ ^! z  H# w: r4 A  java.io.InputStream stream = null;
6 M: q" i, t7 ?" s0 L  try {' t8 e  ^! s+ ~  a! ~
   stream = new FileInputStream(sugarFile);
3 \' Q8 l5 [6 a3 x7 |5 C  } catch (FileNotFoundException e) {
$ M7 }& V* s4 J; Y  v   e.printStackTrace();
2 s# x6 E# N. |  }
5 K5 ~3 t, n9 x  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
: f) R  Z7 ^: D, H  init(in);: E7 Z5 _/ k7 \) a
}7 {% v& ^' T  v  c
private void init(BufferedReader in) {
$ b' ~# k4 P) H# t  try {
; R6 V) p4 W, r8 b9 A: G   String str = in.readLine();
: Z' [& Q5 s; S* g* ?7 E' Y   if (!str.equals("b2")) {
: O/ H" M# N; O1 x3 A1 t    throw new UnsupportedEncodingException(7 I; J% o' d  G0 E
      "File is not in TXT ascii format");
9 ]8 w; d/ h: }   }$ q8 N: \/ U% v/ ~
   str = in.readLine();
. P# N, T# v; p5 g1 V   String tem[] = str.split("[\\t\\s]+");( W5 V. X) ?9 V" O9 [, r3 h
   xSize = Integer.valueOf(tem[0]).intValue();
0 u8 ]$ v- E" o7 X% {0 ^   ySize = Integer.valueOf(tem[1]).intValue();$ ]& l; l# N5 z  ?8 S  O
   matrix = new String[xSize][ySize];
  p- b# i& h( u& Z) u' E   int i = 0;
, f* c+ P7 ?8 O" s   str = "";3 A+ {( V5 {$ [. V# T/ _- b
   String line = in.readLine();7 z; p5 ~4 O# C8 d& I) ]
   while (line != null) {
$ e1 ?- i' j9 a  t% H# [# N9 a    String temp[] = line.split("[\\t\\s]+");
, D& N/ z3 a8 k$ W$ b    line = in.readLine();
6 l# F: y; O- O0 J0 {4 N+ T7 S    for (int j = 0; j < ySize; j++) {) z9 ^' `# ~7 L; X7 K  u, e  E7 ^3 @
     matrix[i][j] = temp[j];: H3 s9 b1 G$ r( _8 R
    }2 j4 y5 o0 ~% {9 b
    i++;3 J; h) Q, n: c$ p, e
   }1 }: x) p2 |3 ?, C4 g  q+ k6 \
   in.close();
1 O, T9 U6 K( y/ l( X5 C  } catch (IOException ex) {
. \7 Q& c# o3 m! B$ h   System.out.println("Error Reading file");5 A, l" E7 A9 k6 R' ]
   ex.printStackTrace();
* X; v9 y$ n4 y# f4 Y0 I( R   System.exit(0);
7 J# X& W2 ]5 s3 o5 u9 N+ L  P' U* y  }& W! t4 A5 I* W9 n2 h% s
}
0 X) w! l7 n4 F1 p% B. ^  I public String[][] getMatrix() {. n. j2 }5 T6 u
  return matrix;6 m/ M7 t- X: ~8 N$ S( K# W
}
4 Y! \. x" y1 T7 h% ?2 z) K# N" T}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-21 10:34 , Processed in 0.015425 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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