设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7225|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
6 e* U0 l+ K3 d# dimport java.io.BufferedReader;" u& b9 J1 G9 x: L- u
import java.io.FileInputStream;
" Q# L( K) _! dimport java.io.FileNotFoundException;9 O3 c$ t4 j  t) T; V
import java.io.IOException;! d( V' t, _9 T5 i
import java.io.InputStreamReader;) _: _* v9 N/ R& d3 h( b* T
import java.io.UnsupportedEncodingException;  e4 N" |/ L1 p5 e1 m
import java.util.StringTokenizer;
9 O9 y3 ~. C8 D) F& w7 opublic class TXTReader {: I* _+ G. g3 O
protected String matrix[][];
4 O1 L! L1 C  a0 Y0 Z. I. l0 n protected int xSize;
4 N# ?, a- u( t( u  e# k protected int ySize;5 G" o- r" R1 s5 {/ T8 l* p
public TXTReader(String sugarFile) {
9 o: w) @/ K. ^9 n9 M! ?( y  java.io.InputStream stream = null;9 D7 V) x8 K$ g0 x7 H
  try {6 N' \/ c$ z* e. ]
   stream = new FileInputStream(sugarFile);
$ E+ e; y8 U, p; P! G  } catch (FileNotFoundException e) {
- s5 G- X  L; U7 h- ?' H; _   e.printStackTrace();
% v0 P7 S) }- }+ b  }2 _, r6 M8 f3 O; p8 o# V
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
# q- }$ L9 r+ z% P: c/ d  init(in);
6 j" q" Q/ _1 P" t' g6 ` }
: ^) K/ W+ z1 ~) a9 x# {6 H( D private void init(BufferedReader in) {
  l6 Z# ^* v& x% e  try {
/ d& W" b5 c- M: n$ B: t   String str = in.readLine();, ~$ P6 N$ k: U" ~
   if (!str.equals("b2")) {2 A' |; e) }* s3 y- A
    throw new UnsupportedEncodingException(* s5 [" `# U6 _/ _% N
      "File is not in TXT ascii format");
/ ~/ X, R3 l! ]8 m: q6 S   }
# G: ^- S  b" i; Y" g   str = in.readLine();
" E' [& P7 J# T& G0 K1 i0 }8 x+ c   String tem[] = str.split("[\\t\\s]+");8 s8 u( f" c6 R- [; T* S$ r: N
   xSize = Integer.valueOf(tem[0]).intValue();
# j' w' Y, ]. k   ySize = Integer.valueOf(tem[1]).intValue();& w$ I6 D2 K0 L& v/ b! s
   matrix = new String[xSize][ySize];8 P6 o0 Y" _2 V( A* c; V5 A0 C
   int i = 0;
6 O7 h& @7 o4 p0 B   str = "";
+ q( R/ U+ Y/ j4 F   String line = in.readLine();
& ^/ N$ ^/ E9 o   while (line != null) {- f, z9 L, q! s$ A' j4 n" j
    String temp[] = line.split("[\\t\\s]+");
9 n$ O# N9 j* ^  w7 _0 G/ b% t  _    line = in.readLine();
' L. D  z1 W: ?3 B    for (int j = 0; j < ySize; j++) {
0 k6 U* l* Y0 |     matrix[i][j] = temp[j];6 S2 L1 y1 \' q- w$ l, E
    }
* C5 a* S% h  Y$ v6 `1 g    i++;
, h; o0 ?7 a$ K- m: J2 |2 R8 w   }
5 H6 x0 S; W- [; S+ b   in.close();
5 m' ^% f. P3 t9 A: k* b+ D1 i  } catch (IOException ex) {
6 L& g0 o$ m( Q& d' h3 f, c5 o   System.out.println("Error Reading file");
+ N" q# T/ t5 T& v+ k) t* k   ex.printStackTrace();
# b& Q. [7 w( ]1 Q   System.exit(0);
" |5 b5 F7 e! m  l! T  }) O6 `& {3 x- H( z; u+ ^
}
/ V) V+ S. K3 z' i' w$ `4 \ public String[][] getMatrix() {
+ I, Z+ c3 j* ^/ Q) j! m( [  return matrix;
5 d  u) D& K' \ }
% _2 [, B; j6 f+ N1 F+ F* o}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-1 05:41 , Processed in 0.018199 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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