设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5803|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
) w& V, d& @* Rimport java.io.BufferedReader;
6 s8 {7 R% l3 D, G, C9 `: L' Oimport java.io.FileInputStream;
( l& j' B0 E3 zimport java.io.FileNotFoundException;: g. W% V' ?0 |8 s3 ^8 U
import java.io.IOException;1 t# o4 [4 j0 E( z$ ?
import java.io.InputStreamReader;
% ]3 W7 ~2 f2 limport java.io.UnsupportedEncodingException;
* w5 E5 v$ _, E" Yimport java.util.StringTokenizer;
/ e! ]/ x0 T/ N' \+ Y! j2 `) p8 }public class TXTReader {: i8 N% {! M  R" L" l
protected String matrix[][];
8 E2 F+ p7 u6 E- y, W! S protected int xSize;- \3 c  x) n& a4 U4 j) p2 J* x
protected int ySize;9 D) o6 I9 y; x
public TXTReader(String sugarFile) {! B( i- o5 ^5 T0 ~$ c7 i
  java.io.InputStream stream = null;
: ?/ r& o3 r! A, F2 |9 J' w$ [  try {3 T0 l9 ]4 k# j7 {
   stream = new FileInputStream(sugarFile);
8 S/ l% o" J2 ?$ F8 k  } catch (FileNotFoundException e) {4 @' r0 c+ N- _0 w! [! L+ U0 }
   e.printStackTrace();" ~" x2 d( O) o- i# j: D3 ?
  }+ p: H+ y# i+ D2 B0 k7 R: k
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
: C6 w8 D- L9 C- |# E9 l3 Q9 X  init(in);  l% V7 r8 ?* t0 e& M* Q
}% h% k! V; _% I% u" Z- B  }  W
private void init(BufferedReader in) {4 z- I' c) [$ B. |: }. j! ~: x) Y
  try {4 q& U* m' _) k) r0 [# x; _
   String str = in.readLine();
. N3 k( ~4 f) C) b& [   if (!str.equals("b2")) {
7 @& u& p: _, C  k8 k/ B, X) D& q    throw new UnsupportedEncodingException(
6 @; D# j0 O/ m" g      "File is not in TXT ascii format");0 D/ Y4 ^) L$ W4 D3 ]
   }
# X, w* Q8 l+ p2 \) b# c2 D1 f; N   str = in.readLine();/ U. K( T" y% @& d7 i
   String tem[] = str.split("[\\t\\s]+");) e# T3 A5 G" @: `, J3 B
   xSize = Integer.valueOf(tem[0]).intValue();
& p. ~8 D$ R6 \   ySize = Integer.valueOf(tem[1]).intValue();& H0 W7 Z) Z: B
   matrix = new String[xSize][ySize];
8 n: ?' C8 h" h% d& C8 K   int i = 0;
$ `% w! E* y3 C8 L4 I3 R; h: s. B  m   str = "";
( G/ V$ J! V% a9 F2 J1 W4 S: V   String line = in.readLine();1 h/ V' y9 b! u6 @2 _
   while (line != null) {
" d# W6 y( K1 v    String temp[] = line.split("[\\t\\s]+");2 e+ B; {1 c0 _0 U  K
    line = in.readLine();
; U$ E3 a. l& j7 T/ z    for (int j = 0; j < ySize; j++) {' f3 j4 x+ a, x1 h! s+ g' e
     matrix[i][j] = temp[j];
# O: J7 y- n6 e6 @7 x    }7 t; Q1 L0 p9 \4 P1 U3 @
    i++;* N! x: r" a5 \# J
   }# B8 F5 m! a* c7 S( u
   in.close();
2 i8 M9 G& n' E, H  } catch (IOException ex) {
3 j0 J; u4 I/ L. T# E2 L5 t. U   System.out.println("Error Reading file");
7 _  J( k4 Y! A' v% c4 {8 ^   ex.printStackTrace();1 J! j8 C, c& n8 A
   System.exit(0);
0 o" C- e; Y$ k- Q+ q0 l  }
2 Z1 R, k* A7 P+ M& k }. f% T; ?0 c8 y5 u
public String[][] getMatrix() {" [1 ^9 T; H$ i: W* n& d9 |
  return matrix;, }9 I+ c6 O$ j7 q: ^
}% l) m0 \. F. p1 V# U$ i7 L/ M/ a  A) ?
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-24 08:43 , Processed in 0.020576 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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