设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6571|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;5 C4 c7 b3 ^6 `% {; `+ m
import java.io.BufferedReader;* w- e8 [- x; K0 j
import java.io.FileInputStream;4 x- X8 b- j7 J- c; ]/ s+ r
import java.io.FileNotFoundException;
$ K3 a+ b; c- K) q$ Z, i' himport java.io.IOException;; T) d( Y- }! g6 F$ C' a8 L0 M
import java.io.InputStreamReader;
" F, S- Q$ Y: Iimport java.io.UnsupportedEncodingException;/ s4 g6 k7 z4 e8 m! A: J  d
import java.util.StringTokenizer;
* M5 L' g  T& k% b$ z) Ypublic class TXTReader {5 @! a! I+ a, i
protected String matrix[][];
4 |- E, G5 j) M5 ^5 U protected int xSize;
: f$ D* J: _5 f# H) U) E9 s) H protected int ySize;2 r, u- ?; w3 e( S% j
public TXTReader(String sugarFile) {# e2 d; H& |$ j0 }1 O. p: u
  java.io.InputStream stream = null;- A4 X: {* ~, p0 E# j4 n
  try {
, D- N: c% H. Y9 Z$ D1 S' E3 A   stream = new FileInputStream(sugarFile);1 q! N$ _$ j3 S5 j# K/ y
  } catch (FileNotFoundException e) {4 A% i* o1 C2 f: {* m9 G
   e.printStackTrace();. h( y1 B# J# r/ ]# ?0 X$ z- G
  }; P+ _0 G' Q# L; l# V! ?
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));5 H! e& [! a4 l$ B! X! t; y
  init(in);
# u  |7 s& t7 Z+ d8 f$ t/ a0 L) Z }: `3 c5 `& U2 {" `3 [) k
private void init(BufferedReader in) {3 ^9 z) }4 r3 d8 F* c
  try {/ H$ `3 g5 a' l
   String str = in.readLine();
# G# r# a9 T0 G   if (!str.equals("b2")) {
6 P) q4 J6 l( l2 f/ h: d    throw new UnsupportedEncodingException(
3 C/ e. U" K) W9 b$ B      "File is not in TXT ascii format");7 a# O% I! k  Z% n) Z
   }
4 R; t' `4 P& ^& C   str = in.readLine();
) `0 O- ^; c! c/ m' A. h0 S   String tem[] = str.split("[\\t\\s]+");1 h7 F9 q& T$ G. W& \& Z
   xSize = Integer.valueOf(tem[0]).intValue();7 X" }$ ~8 r3 d
   ySize = Integer.valueOf(tem[1]).intValue();# e" u+ n% |/ g. t5 _$ G
   matrix = new String[xSize][ySize];  I* z& W8 P5 z  X( c+ ]6 j
   int i = 0;
& F: s# b# t2 S) L" W1 P" o   str = "";- H; N) {8 l+ C2 e8 W4 }4 W8 ]  u
   String line = in.readLine();
" C) {5 V5 G4 Y" ^1 y$ _   while (line != null) {
3 k. B7 ?6 Y$ v; t  H    String temp[] = line.split("[\\t\\s]+");
- S$ D2 v6 b: k  s6 J( d* X9 }" w    line = in.readLine();) U: h8 E7 V+ b: C7 ?
    for (int j = 0; j < ySize; j++) {
' X& c: P8 C$ l9 G/ U9 U; y. g, L     matrix[i][j] = temp[j];6 s! A( @+ K7 I5 a1 C1 @6 _
    }2 E4 b$ o1 L8 _# D3 [2 R- M
    i++;
# u: j/ a' B9 O   }
$ C+ ~1 }" L& m  P$ Z+ e   in.close();
. m9 n; n$ p3 z+ t7 r5 k1 ?  } catch (IOException ex) {1 X! s: F1 a5 V+ R( r2 n
   System.out.println("Error Reading file");
0 ^& n+ @' j( C) j% v% W   ex.printStackTrace();5 X9 U: ]' j" D3 @" `$ n& ]  o2 _
   System.exit(0);
* E6 K. |+ @! W  }
" e2 x1 _, u& k: S5 E0 C5 c }  I7 u6 b* P7 t: A
public String[][] getMatrix() {; t; G! a$ g, f
  return matrix;$ v0 A) x! d# `8 _
}
* F* D% T1 d: Y; b$ u" a}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-19 09:33 , Processed in 0.021363 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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