设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7453|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;( d+ R- Q! A# g
import java.io.BufferedReader;
- w/ G' `; {0 n0 C; Z. Y0 W" himport java.io.FileInputStream;
- |6 v3 [/ m% I8 wimport java.io.FileNotFoundException;
. \1 E$ M5 B; Q( f+ Uimport java.io.IOException;
/ z8 [  _# c+ z/ i& {; t% N! k* q  Qimport java.io.InputStreamReader;2 L/ R) z/ r8 }
import java.io.UnsupportedEncodingException;
) q7 d2 a% n  `( [8 y2 Oimport java.util.StringTokenizer;
, G% y/ T  e9 R) g' R2 n, v, ipublic class TXTReader {
) k2 L! U0 V4 b# S' o" I protected String matrix[][];- x0 |  [( n, E) s2 e% a; ]
protected int xSize;
  ]9 @, p& B2 l protected int ySize;: I  k2 w: }7 {  g4 ^* k
public TXTReader(String sugarFile) {
1 [  K: t+ B2 g% f0 `( R  java.io.InputStream stream = null;; j& q& j9 N! ]
  try {$ E) P! \& I9 ^3 _1 A9 i6 C
   stream = new FileInputStream(sugarFile);
" o% \2 Y' E( n( {0 I5 y7 p3 b- S( R  } catch (FileNotFoundException e) {
6 N( g' R' B5 }   e.printStackTrace();- Q( J1 F9 e: m. u4 y- b
  }
% U2 i0 y1 _5 {5 @! P6 A/ C  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
$ j- o4 q6 _$ i) R2 B! S  init(in);# M. A1 O( ~' l; \& O
}6 E1 C$ @4 S4 A$ T. p+ J
private void init(BufferedReader in) {8 Z" \+ J7 ]: m8 p$ o* F8 a
  try {
. g* l: }% R' K' H+ f' r# E   String str = in.readLine();* X8 C; u3 ?' I3 j
   if (!str.equals("b2")) {
- z, b6 V; |; E    throw new UnsupportedEncodingException(
5 L- `1 z/ U7 O9 `+ ?      "File is not in TXT ascii format");
& h0 v8 g' K% C8 @$ o4 q   }* O' G9 Y' o9 l$ @& |
   str = in.readLine();
/ c0 y) b6 t& H: J. y1 J   String tem[] = str.split("[\\t\\s]+");) G$ O+ B2 K; C  c' k
   xSize = Integer.valueOf(tem[0]).intValue();
/ w) Q: z0 s2 z6 l   ySize = Integer.valueOf(tem[1]).intValue();
) c$ {+ q7 I2 G* B/ f0 {% b- h$ [   matrix = new String[xSize][ySize];6 k; [1 B" e: c8 x( _
   int i = 0;' K& x  k  g( I) K! V" Z7 G
   str = "";
$ {& ~" f% G0 A   String line = in.readLine();
# V1 M6 d; M8 ]" u   while (line != null) {
6 |8 F1 j7 s4 c3 `5 Y    String temp[] = line.split("[\\t\\s]+");
  l' s2 j7 }3 X2 q2 {+ _    line = in.readLine();+ B/ [, R6 R9 K/ A2 ?- T
    for (int j = 0; j < ySize; j++) {0 V. c% k' G4 e* d" W: t( Q
     matrix[i][j] = temp[j];; U4 `7 i$ k7 z& Y; E
    }( h0 p  [3 w# Q  s7 Y
    i++;; I& X! o) P- V. A
   }0 d5 {( l' z9 V9 q0 W( V, ]2 y! p
   in.close();
1 ?  M  n  Q/ U; O  } catch (IOException ex) {9 L! [& s  k- p& y4 u" K; n
   System.out.println("Error Reading file");
; u( T% N8 H- ]) X% {$ X$ E6 c   ex.printStackTrace();
3 B1 B% Y4 j: J$ b   System.exit(0);" d3 i# }  D! Y: h3 p' m
  }
1 X5 P5 c- e( U4 W }( S: w" X: N6 a0 D, ?! s1 ~- k
public String[][] getMatrix() {
4 u4 {; T5 ]& C1 `  return matrix;5 F) |  ^9 M- `* V
}& A5 @+ d% U3 @  O, v  r" b* ^/ r
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-17 03:30 , Processed in 0.015013 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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