设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9328|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
; C  E5 d4 G* r# c$ {2 f, s$ g1 B% Cimport java.io.BufferedReader;
  A! j4 E: M. G% jimport java.io.FileInputStream;2 l0 q  c( A; W, g7 k% w
import java.io.FileNotFoundException;. d3 r! x3 J3 C5 h( }
import java.io.IOException;. B5 f1 h& Q, ]/ c* s9 @( T
import java.io.InputStreamReader;+ a0 `: j1 o% i6 E3 X
import java.io.UnsupportedEncodingException;
; m3 a4 s3 _. F5 \0 i" p$ Rimport java.util.StringTokenizer;1 F/ ~! G5 g% o. p& S3 ?# A% S
public class TXTReader {
1 M3 V0 J7 |/ X" P1 `& [) ^ protected String matrix[][];
( ~8 Z9 N; k' `- X0 A protected int xSize;; E: i7 K6 ~6 B# Y+ i# l
protected int ySize;
9 X/ p5 {! ?. W) W public TXTReader(String sugarFile) {
1 ~% ?# A' s7 ?1 F  java.io.InputStream stream = null;
, w# |8 ?; G. h1 e  try {
' Y  g9 a" R5 d0 j4 E2 a( E$ ]" q; J   stream = new FileInputStream(sugarFile);4 X' X) g0 Q' l$ R( I
  } catch (FileNotFoundException e) {' V( B1 E. y& X( Y2 T
   e.printStackTrace();3 G5 O: o! P) Z* y% k
  }, T$ d$ N# B. X
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
$ N" Y; {& y; W+ |8 o2 M  init(in);" ]1 S+ K1 F5 H' x
}6 X. |/ r8 I( t& s: I  `
private void init(BufferedReader in) {3 `+ b: ]( O0 r# x5 y
  try {
" `& ~3 C/ e7 ~! s* r. C   String str = in.readLine();: {+ v% k) Z9 G# C
   if (!str.equals("b2")) {$ q3 P9 |; ]4 y. j: X
    throw new UnsupportedEncodingException(
9 i$ n. K1 W4 o      "File is not in TXT ascii format");
) M7 t* k* s5 v   }/ b# w  t- A/ R9 Q7 l
   str = in.readLine();
6 o0 H3 K3 V* W   String tem[] = str.split("[\\t\\s]+");
6 z7 g- Y5 L% z3 n4 P+ f   xSize = Integer.valueOf(tem[0]).intValue();5 J7 l* y% K! g5 V5 O
   ySize = Integer.valueOf(tem[1]).intValue();
! K- F; W0 U% t# g' d   matrix = new String[xSize][ySize];
5 r; I' D' m/ @   int i = 0;
9 e* d( G* k  n: O2 ]. C   str = "";2 }- m0 n! G5 ]& C" W
   String line = in.readLine();6 R. I# e: {' ~& T) \# Y6 Q! j
   while (line != null) {% N8 ]! `4 {. g) `, \
    String temp[] = line.split("[\\t\\s]+");
6 e  z2 `  O: j    line = in.readLine();% l8 u3 {7 i/ Y% T
    for (int j = 0; j < ySize; j++) {7 o4 A7 y; N9 m2 \; R) r
     matrix[i][j] = temp[j];% n& M# S2 q8 `' N% \) K
    }
. b* }( p  T6 Q2 m7 k    i++;
& |3 D  M  u/ ?' a" Z7 t, W* k& X   }
2 c3 ?) o3 n# }   in.close();6 J' j& N5 m# y* U5 t" C% U, F
  } catch (IOException ex) {/ e0 b: X' x0 K! d
   System.out.println("Error Reading file");+ J' N7 x. [% o
   ex.printStackTrace();$ s& L- v! c- D1 A: `
   System.exit(0);
* z- s& ]$ v* P% z+ X9 @  }
. @8 O. ^& h" k6 O( p; x6 }( y }6 P" R% ?1 |; m0 f! e; c  G& ~7 Z
public String[][] getMatrix() {/ x7 g8 R: m: u8 v( N/ P& {
  return matrix;
9 s( k1 R6 e  H- l  H; | }$ g) l: c3 h3 n# Q+ v7 m' K4 y
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-15 06:03 , Processed in 0.017617 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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