设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7300|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;1 O2 k' @7 n8 b9 n: C
import java.io.BufferedReader;
+ L: ^2 R2 M1 C. @. ^4 eimport java.io.FileInputStream;
1 I' x- ~% z" l7 K/ A2 l+ g# Mimport java.io.FileNotFoundException;% |' _) B+ u$ r* }' z+ J' `
import java.io.IOException;! u! }7 V+ c/ c8 J4 e/ Q% P& t; \9 D
import java.io.InputStreamReader;
+ k5 E8 a& }) b. v4 N2 Z3 Ximport java.io.UnsupportedEncodingException;
$ K, m# z( {6 i2 iimport java.util.StringTokenizer;
" |) [2 Y% n# @9 qpublic class TXTReader {
# B4 v0 N% C. ]1 N5 p* [- v protected String matrix[][];2 a3 {2 E9 G; v6 j# ]6 k8 X' P
protected int xSize;* S8 _& @8 @9 f
protected int ySize;6 |2 T( b& ~2 \$ [; I
public TXTReader(String sugarFile) {
) c' w) D  p: b8 \  java.io.InputStream stream = null;' ~$ u# w- F/ z& K, u
  try {
, E6 a' C+ A* A) f& e   stream = new FileInputStream(sugarFile);
* x& ^- F9 b1 K4 b  `7 W/ x3 g( v3 E, s  } catch (FileNotFoundException e) {
9 G7 Q$ R* @* l$ C0 N   e.printStackTrace();
" {- V# L. V4 D) V! N  b) [3 f  }, p2 J/ L8 D, [9 {
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));# E5 j& R6 H5 v' @' w
  init(in);
$ P$ s: X$ O! ~ }
7 p& C* g6 W3 q" V0 s private void init(BufferedReader in) {
: d/ y2 a; R& k- _1 [  try {
3 J- K, c  u9 `: ~, W7 v) y   String str = in.readLine();
( A7 S) p0 s) A& S6 W- T1 q: l. a   if (!str.equals("b2")) {
0 B  @5 H- Y$ l& m    throw new UnsupportedEncodingException(
; o+ f4 s& D- k      "File is not in TXT ascii format");1 J) v" w, _; e3 p. C+ E
   }* v. `5 ^: g( `+ d6 X5 C, G
   str = in.readLine();9 A/ x( J! j( \  L. O( `
   String tem[] = str.split("[\\t\\s]+");! |9 q3 L  {. {1 {
   xSize = Integer.valueOf(tem[0]).intValue();
& u. T# ]4 |' ]" ~3 {   ySize = Integer.valueOf(tem[1]).intValue();
) P; O; O( j, w" W   matrix = new String[xSize][ySize];2 V) k% d' r8 L$ x. o! }; l
   int i = 0;4 }  D/ B7 @$ |& [: _8 O- f
   str = "";
' x0 Z6 e5 C  E# M' f9 J- l/ \   String line = in.readLine();* T) _5 a! @1 M
   while (line != null) {8 Y% Y$ M$ S; s, |
    String temp[] = line.split("[\\t\\s]+");, R- `: |; W! {& [# G7 s
    line = in.readLine();
% g. Z( U7 r5 N  F: l. }: ]+ f    for (int j = 0; j < ySize; j++) {2 K. [1 G2 Q  ^0 ~. J
     matrix[i][j] = temp[j];
2 y7 t$ z8 B/ r& ?- T" D4 `    }, h) @) ~2 ~! x
    i++;9 v$ C/ \% }6 C! N
   }5 u; J' {5 |1 H+ ]( _
   in.close();$ @8 u. \- r" d2 w2 r
  } catch (IOException ex) {
( n* j) S5 m: C* a6 u! w) A   System.out.println("Error Reading file");8 y1 J8 @; u% [4 F' O/ {
   ex.printStackTrace();; A! }* F; q$ R1 X7 \
   System.exit(0);, @. S" V5 }3 F+ i
  }5 o7 s! _# c% y5 m
}
$ V/ a: c! m* M public String[][] getMatrix() {) c! n$ \' V* P: \8 c! R+ C; N
  return matrix;
: Q$ `& p) f+ S- d: [ }( {. B" n7 P% a- q0 k
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-5 14:14 , Processed in 0.020523 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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