设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7010|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
8 r0 L+ Y. v$ cimport java.io.BufferedReader;& e$ a+ C4 C) L( b% O' Z9 b$ v
import java.io.FileInputStream;
) u# V7 s) O$ c+ I( ximport java.io.FileNotFoundException;
" S7 C* B* B  ?7 fimport java.io.IOException;
  u2 `+ N! I9 l8 w, H7 timport java.io.InputStreamReader;" h+ u3 `$ C( e
import java.io.UnsupportedEncodingException;
/ P8 {9 z: x! |import java.util.StringTokenizer;3 a1 p4 o/ y' F+ v
public class TXTReader {
, M1 U! e! F: c0 o5 i protected String matrix[][];
$ j) U/ p" k- z! ?8 _* _ protected int xSize;
! D) R, C7 H: n, L3 q, [& W protected int ySize;
# |( A7 t7 Y8 U  n) @+ N4 z public TXTReader(String sugarFile) {1 v. G, I5 h7 ?: [
  java.io.InputStream stream = null;
, R! ?, H  a! z4 f/ u9 x  q  try {( T4 r7 Y, R3 [8 A5 K! ~
   stream = new FileInputStream(sugarFile);; m& Q1 W  E1 }! {8 W) x) l1 ]
  } catch (FileNotFoundException e) {7 y$ \  p  d5 }# Q$ P+ A( q
   e.printStackTrace();) c2 ^6 V4 F6 `" d0 ]
  }
! v2 Q9 U* _& K" v8 Y" D* {. |  BufferedReader in = new BufferedReader(new InputStreamReader(stream));, o6 y0 g& f! v5 a" H8 x& W1 ?+ q
  init(in);# R6 F+ t( m% {
}; z8 G8 x% f4 z2 D
private void init(BufferedReader in) {% ?9 B# q6 o$ i1 ]6 i4 U
  try {
- M# G6 P$ Z) K5 `   String str = in.readLine();
: ]9 K8 ?& r3 h7 M   if (!str.equals("b2")) {
8 F+ j& C  r) u% H- S3 D9 F! p    throw new UnsupportedEncodingException(
/ U/ ^$ o$ g8 S  l6 m      "File is not in TXT ascii format");
8 z4 s7 f7 V: M& C   }
4 J$ i# O& O. z' x8 V   str = in.readLine();
7 k  w; S/ v4 j3 h; m+ l   String tem[] = str.split("[\\t\\s]+");
( r0 L  ^/ L# G# ^: G" S5 o   xSize = Integer.valueOf(tem[0]).intValue();  Q2 R8 T; s  x+ c$ O6 x
   ySize = Integer.valueOf(tem[1]).intValue();
# g  s' B. x( T2 F' b   matrix = new String[xSize][ySize];: \7 L) Q& J# Q+ I/ W3 i, |3 Z
   int i = 0;; W) _  X" z. T0 b" s; `7 `3 F
   str = "";2 z: k( \& H8 e
   String line = in.readLine();% J6 i$ `4 o4 L0 ~
   while (line != null) {% Q; ?% h6 S8 X, G- g$ ~8 b
    String temp[] = line.split("[\\t\\s]+");3 J4 `: z; l& \* }2 j* m
    line = in.readLine();
- Y# A' m1 n) m& V$ }    for (int j = 0; j < ySize; j++) {% |6 c% l$ ?6 _5 l& v( P2 x/ I: h3 B
     matrix[i][j] = temp[j];
5 K$ C) _9 J' Z- ?/ E    }" X$ x* \) n: |# B: ]
    i++;
, l4 s8 h4 e* k2 M! M- ~   }
8 G0 \' d6 X7 U5 t8 J   in.close();
4 S% N1 m& x0 \1 f  } catch (IOException ex) {1 g1 A1 ^1 B3 ]/ R/ E8 ~( V
   System.out.println("Error Reading file");0 H0 W6 A8 Z9 ?$ K
   ex.printStackTrace();7 F1 O2 W7 v7 K" D8 T, x
   System.exit(0);
8 n9 d9 m+ n$ m# w  }
2 ^0 b+ U  q% V1 [ }+ Q. f( x6 `( \3 m% S& Q
public String[][] getMatrix() {& i; x! K4 F& [6 C+ i
  return matrix;
7 s* ]: b% x& w- C }
9 `4 Y, `0 j( Y' _& o8 j}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-15 15:39 , Processed in 0.016015 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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