设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9691|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;2 S) V, m0 M4 _. }& Z6 L: |
import java.io.BufferedReader;  G8 b" y! q% ]
import java.io.FileInputStream;
' ~2 P, u( V, X4 nimport java.io.FileNotFoundException;
' r. o, p& R) E' ximport java.io.IOException;( r1 O) V8 |3 v6 U
import java.io.InputStreamReader;
! a5 v% W# R7 F& w6 oimport java.io.UnsupportedEncodingException;6 m, d6 g! P5 g7 }- }3 d
import java.util.StringTokenizer;
8 r  P7 v* O/ B& Y& Dpublic class TXTReader {) W; ^& ?* A; p, U2 K5 d
protected String matrix[][];8 b- W5 N  \* g& V  |
protected int xSize;
/ B7 F" ^  m5 E( a2 { protected int ySize;& h4 ]  k+ @/ j
public TXTReader(String sugarFile) {
' {) G. d9 w7 _/ G  java.io.InputStream stream = null;
$ s+ h/ T$ U3 V/ S2 Z) `  try {; P! d1 j$ z7 Z$ N+ z# a
   stream = new FileInputStream(sugarFile);
$ Z+ |" v& [9 x" ]+ N7 k  } catch (FileNotFoundException e) {
' H  M; s& c( w& S% C. k   e.printStackTrace();0 F) q5 D9 e7 [+ k. D: y% U
  }
% d+ c: ^+ A% f- J# M1 j' O  BufferedReader in = new BufferedReader(new InputStreamReader(stream));6 }8 F/ ]" I, ?. ^
  init(in);
& Y+ n1 q& F$ @9 {, y }
: h/ }' f2 E, T private void init(BufferedReader in) {
% N+ T0 _; c" Y' O  try {
; a) u% W) k/ z% |   String str = in.readLine();
# Z5 J" V  e6 ~( F3 L0 H   if (!str.equals("b2")) {0 d: _( W( f0 m% D& C7 k
    throw new UnsupportedEncodingException(& @$ p% Q) o" O4 u, Q
      "File is not in TXT ascii format");
) D( D; N- Q1 \: q, u   }
7 l9 D, Z5 i( [1 q5 [   str = in.readLine();/ ~& {, |( e. ^8 t, x; y7 @
   String tem[] = str.split("[\\t\\s]+");
2 O. N% S6 s5 Q9 H. l+ v; R& o   xSize = Integer.valueOf(tem[0]).intValue();
5 p: C3 y& T& i   ySize = Integer.valueOf(tem[1]).intValue();
% e/ _( {5 L( c5 F9 ~   matrix = new String[xSize][ySize];1 ]: \+ d$ k7 ^
   int i = 0;
3 y1 ]; |9 G7 i* O# u   str = "";$ Y; Y: J0 ^# S1 w7 a" }) P: ^
   String line = in.readLine();' n& A2 N9 W* i) V8 p
   while (line != null) {. X; P6 t  K+ k
    String temp[] = line.split("[\\t\\s]+");# V+ E$ L5 i5 P  k5 H
    line = in.readLine();/ |, l( K( |7 A( \8 K! t% r
    for (int j = 0; j < ySize; j++) {
8 T( A; ~, h0 M0 N6 |     matrix[i][j] = temp[j];
3 W9 F' q' j0 H$ g    }1 R  O- `: t8 d5 Q) W0 I* J4 f
    i++;
' x# G, H) @. D/ I   }
- I, p1 f; y& w  m' w% |, }7 t   in.close();) J; J- ^3 i% S! M
  } catch (IOException ex) {' c% g. i: @$ M; ]
   System.out.println("Error Reading file");. {0 [$ x) d. K3 n* V+ P
   ex.printStackTrace();
5 d. q. L) V  D* W9 F   System.exit(0);/ M. u2 M& ~$ |  V) B, r- P
  }
6 d1 `& l" d6 s: M7 e }
" ]4 R# A) W3 f  E' B& W public String[][] getMatrix() {
$ r4 v  o; |* {" u  return matrix;
$ i# A) b3 U! V& ^* E. t6 J }
4 z  H: q8 C% B) }# s9 F- `}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-31 09:34 , Processed in 0.017384 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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