设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6626|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;; z& r  \0 {+ J/ u0 }
import java.io.BufferedReader;
( r4 d$ C8 [2 _" i0 @3 x) \import java.io.FileInputStream;
7 A0 n9 B& j( |& Y. K6 H% L: Uimport java.io.FileNotFoundException;
6 I, Q( t6 x) F0 s! ^import java.io.IOException;1 [" D; D; S& {
import java.io.InputStreamReader;' P: O( H8 c6 v/ D) X
import java.io.UnsupportedEncodingException;
3 W/ P/ k+ r& `1 @1 B1 w' Simport java.util.StringTokenizer;
8 F/ z# ?6 C! e. |& Zpublic class TXTReader {, }. I. ]: a3 j8 [$ d
protected String matrix[][];% S3 ^- O$ U; v' i/ m# e
protected int xSize;4 X  H8 t6 {: o1 [% m' x9 K
protected int ySize;
: O( G; W, t4 |+ N# g! c8 y public TXTReader(String sugarFile) {
/ V9 h* h8 H. n  java.io.InputStream stream = null;# Z% x9 x, R2 a6 c" n1 N! h
  try {% J! T3 s& b6 z
   stream = new FileInputStream(sugarFile);
6 i9 l$ Y1 Q; v  } catch (FileNotFoundException e) {0 ^2 p. L  h; I) R  ~
   e.printStackTrace();
& d# |2 y% c: B9 y+ I% G  }
& @7 ^# ]. y4 O2 r! D3 B. k  BufferedReader in = new BufferedReader(new InputStreamReader(stream));& x3 P- ?  o7 J5 f/ s
  init(in);2 W" w" H4 s. L  p3 z, g
}9 M) c7 U! g; T0 v" e
private void init(BufferedReader in) {2 ~: N# d% L0 l4 K- q
  try {
+ p2 H9 t4 k2 l! ~! A% e7 d& `2 C   String str = in.readLine();
. N( h. d' k0 f3 q# @5 B   if (!str.equals("b2")) {8 B; A; b9 H+ x2 e
    throw new UnsupportedEncodingException(
7 N4 z# J: H$ s" P; f      "File is not in TXT ascii format");& }4 b; H7 f; T/ P3 R' ?
   }6 |% _; Q* ?3 |" J  m
   str = in.readLine();
# `- I- `, S7 E2 Y   String tem[] = str.split("[\\t\\s]+");
% P( Q: i3 Q. [# G, {1 b  T   xSize = Integer.valueOf(tem[0]).intValue();
* s9 k. d1 r' \( Q4 o   ySize = Integer.valueOf(tem[1]).intValue();- K6 n" C$ M- X+ H! c
   matrix = new String[xSize][ySize];& Y. s/ B) ?3 w2 V- ?/ ~! J
   int i = 0;* T! N; D5 n1 e  M. M4 v0 k8 w$ @2 S
   str = "";
! }9 A% @7 H  X5 p- L, S( L   String line = in.readLine();  {' X+ Q8 @" s- w' b$ D
   while (line != null) {
% o: y  K1 [- B- R4 z/ c0 U! ]4 S    String temp[] = line.split("[\\t\\s]+");
; `2 d7 T* c# F# D5 t. W. x* x9 K    line = in.readLine();# U8 ]+ f* A: f9 i) b% `
    for (int j = 0; j < ySize; j++) {
$ d0 f) j5 d( P' {     matrix[i][j] = temp[j];
4 d* q7 e2 f& \4 p1 ~    }
% p2 [- z/ A' i( L* b    i++;
+ b0 v; v* R8 b% z: r- u   }
$ R, T/ k6 g* G: z6 e3 |- W- Y   in.close();
6 ]. h7 E$ R8 K8 P  X4 L  } catch (IOException ex) {
* d; p% v& P2 ?& j   System.out.println("Error Reading file");: t/ H( Z; v# c* T2 @
   ex.printStackTrace();8 ~9 Q5 l0 i1 n# P1 v
   System.exit(0);
) L2 \. y9 O. d) V  }
* H# l( Y$ Q0 P6 K' n" K }
2 Y7 z) O5 t+ }. U, J public String[][] getMatrix() {
% R- w( k. ^+ c  return matrix;
, j0 d1 ]* R0 D1 k/ L }
# S. O2 Z- a7 H8 d6 o* T& d8 h5 X9 m}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-23 07:15 , Processed in 0.024571 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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