设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10040|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;; P- ?# f' \5 L: Q4 v
import java.io.BufferedReader;5 |" k% `) C1 f/ e# \
import java.io.FileInputStream;# \8 N0 S6 P$ `
import java.io.FileNotFoundException;, v  W+ p' a3 f8 T9 n
import java.io.IOException;' Z! H$ j  `/ s6 K" U) Q; @
import java.io.InputStreamReader;+ i, f. g) w. c. ]! j
import java.io.UnsupportedEncodingException;
# Y2 V2 \( I" c: J( q* k7 t3 oimport java.util.StringTokenizer;
* v0 Q* @; y3 G4 I7 Z* V5 \( G0 Npublic class TXTReader {
5 l+ N! j9 k3 C/ t( T& ?( X protected String matrix[][];, X# b+ G3 g5 S$ L* H7 e1 d
protected int xSize;" o$ e3 \$ V1 {
protected int ySize;
* E& B! M; @0 M, P, w public TXTReader(String sugarFile) {. v$ q# t1 N. {: q  ]3 O, a
  java.io.InputStream stream = null;7 f- X8 T2 b) T. l# X3 I7 Q
  try {
) ~; L) x. I& d  h$ x  R! q   stream = new FileInputStream(sugarFile);# q0 L. b4 c9 a
  } catch (FileNotFoundException e) {4 Y8 @' ^/ o  t. _, x
   e.printStackTrace();' A. w8 m- w& m$ q9 r+ M/ Q1 z
  }
& c! u! K$ U! A, j  BufferedReader in = new BufferedReader(new InputStreamReader(stream));% }; p9 O: H1 F( ^
  init(in);) c4 M$ ~' R% G4 X# L6 H
}
$ m; G, n& ~* u  r private void init(BufferedReader in) {
+ w; P% W( ~6 {  try {
" [/ L7 c! h* n2 m6 S  \! T   String str = in.readLine();
" e" H7 ]: g4 d' @   if (!str.equals("b2")) {
, S% a* H1 z0 D) p: W5 C    throw new UnsupportedEncodingException(; R- \& b" c6 T, i# K% H
      "File is not in TXT ascii format");
+ w( S1 t1 f- V   }' F0 r" o3 |  `  p4 l' f
   str = in.readLine();
: J) Y' x# E* S  S   String tem[] = str.split("[\\t\\s]+");% x& X! y; [3 k9 P- ?& h
   xSize = Integer.valueOf(tem[0]).intValue();/ C4 r$ q9 A3 I0 ^8 h4 w, s# _: g) g
   ySize = Integer.valueOf(tem[1]).intValue();* A& y7 e3 n2 d# T; T
   matrix = new String[xSize][ySize];1 K) \6 ]6 K4 n# g7 x0 M' d0 R0 m' X+ r
   int i = 0;1 V% l; P) a/ L
   str = "";' o$ n8 U2 }1 i  n! F5 x7 h2 ~/ B
   String line = in.readLine();
1 t8 ?- v. |  }7 Q   while (line != null) {9 ?- R9 L9 M$ @: w+ x" L$ _
    String temp[] = line.split("[\\t\\s]+");
4 L% k' ]8 P4 ~2 x; p    line = in.readLine();! D9 `" w  {3 v* t
    for (int j = 0; j < ySize; j++) {7 T$ W$ Y! v1 {( q7 o
     matrix[i][j] = temp[j];' w1 J! Y7 m+ n6 D
    }- h/ h4 ]" H  L' I( h- Z
    i++;
: t6 x& W+ m5 q* C. b, a   }7 W" |9 R, A# E
   in.close();' F" ~" J  K) _6 h
  } catch (IOException ex) {
+ f1 z( y) C  r$ r   System.out.println("Error Reading file");+ d2 E! o- a& C$ ~& Q
   ex.printStackTrace();( ^  i  J1 n7 @8 ^' W; L$ R+ h
   System.exit(0);
0 l9 G& Z# @% F5 J3 R1 ^  }
+ b; |' K0 H6 \! I8 Q1 ^0 w }: b0 A( x& _- v5 r' {0 R" G/ e& A
public String[][] getMatrix() {
/ T3 u3 _, F0 X0 Q& G3 L; ^  return matrix;
; x" Y6 F( h3 R4 z0 p6 J  { }
, A  H7 G- o- K/ I( g- q& U0 L}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-19 04:13 , Processed in 0.016439 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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