设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5712|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
, M! @9 Q, [2 x/ Himport java.io.BufferedReader;% p# c+ ^$ j: @5 N! V6 J  d9 ^
import java.io.FileInputStream;
, z* q5 y$ E/ [import java.io.FileNotFoundException;
8 o; T1 f$ t& O* K) Nimport java.io.IOException;$ t2 z, d7 G2 }8 }& q8 s$ |6 x
import java.io.InputStreamReader;
! F& q4 [* N# m- _& bimport java.io.UnsupportedEncodingException;
  v& _# O+ m9 dimport java.util.StringTokenizer;
( C( c! Y+ o, E) C' c5 Y3 `public class TXTReader {5 w% t* D: _+ n. A6 @9 l
protected String matrix[][];
7 `1 m! ^. {. G" m( C protected int xSize;
% b- r' Y, ~& h protected int ySize;# ]. y/ V4 q( i7 x( a: F7 v  R
public TXTReader(String sugarFile) {' |5 P: J- e( \, r( V
  java.io.InputStream stream = null;. X/ P2 M5 p7 i
  try {1 N6 W4 F7 R7 M/ I) ^, W$ D
   stream = new FileInputStream(sugarFile);
) ~# Q( F2 @/ p% Y1 i" Q! r  } catch (FileNotFoundException e) {
2 `# T; S$ j0 x9 G3 f. {, x: `2 J   e.printStackTrace();: G. M! G) I& N7 r8 m
  }
8 B+ `$ Q7 n* V% F% ?2 k. P  BufferedReader in = new BufferedReader(new InputStreamReader(stream));$ o+ ]: H% n* S" U1 R3 j0 g, l
  init(in);
6 R% J4 Y- L1 L4 l. @& P }
: j: _* z9 n" Z, F, e private void init(BufferedReader in) {7 y) o+ g. w- ?) p
  try {9 M% v9 o* V1 N. y, ~* ~+ ^
   String str = in.readLine();
' H' ?% E5 z  r   if (!str.equals("b2")) {
. i2 i7 O; L: T# R+ Q4 V    throw new UnsupportedEncodingException(- s! {3 x# R) A( u: M
      "File is not in TXT ascii format");9 a# j. @& I% y+ q1 f8 b
   }' M+ D2 f! f* M  S
   str = in.readLine();6 U: x- C6 n  {4 f# w
   String tem[] = str.split("[\\t\\s]+");" D) }$ e8 M- _5 N1 [4 i
   xSize = Integer.valueOf(tem[0]).intValue();
, A7 {' n2 z) O' d. g5 `5 z   ySize = Integer.valueOf(tem[1]).intValue();! M, B: T  l* y- @- R$ Y& W
   matrix = new String[xSize][ySize];
" S% L8 [" X& _: ~' N! U   int i = 0;
, r" J7 Q$ ?% m6 c7 q, W   str = "";3 k8 z6 Y  Q1 p( @
   String line = in.readLine();
3 R; e. U& v2 Q! X) B* I   while (line != null) {
6 c* s+ K/ r6 q9 I& A: t$ I    String temp[] = line.split("[\\t\\s]+");/ r. a2 H( E( A' }7 ?
    line = in.readLine();
1 k9 l# u5 N% n/ K# X! X    for (int j = 0; j < ySize; j++) {: K  Q5 ]* P! _. ^/ B' l
     matrix[i][j] = temp[j];+ k" c* Y$ j4 P. M
    }; c8 V: B+ W; x2 K* p! _
    i++;
% j$ N# L( }' `6 e% O6 p   }
, z4 T. ?/ F5 n- z+ `: O   in.close();
9 u9 Z$ q3 Q' \. F( m# X# ~  } catch (IOException ex) {9 [5 i6 M/ P3 U8 U( a) [
   System.out.println("Error Reading file");
" u* N. W5 z! o   ex.printStackTrace();  b$ M# G/ u: A- R, }
   System.exit(0);9 o7 ]' y2 }9 y
  }5 W+ P( y( g7 f7 v
}
, b- m2 h+ G- P public String[][] getMatrix() {) J; u+ q8 D8 }. Y3 l
  return matrix;" n2 F8 R& o3 l8 c, K
}
6 ]% l! F, c9 v( k5 {% R}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-20 21:22 , Processed in 0.017547 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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