设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9071|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
- _" k* ]. c1 wimport java.io.BufferedReader;9 z# I/ t  W3 F
import java.io.FileInputStream;5 N0 U3 X0 \+ V6 l* V
import java.io.FileNotFoundException;
5 b" ?& O+ S% W# gimport java.io.IOException;$ ~3 U2 K  e& X& ~" e# P
import java.io.InputStreamReader;
. k8 S8 A5 S7 k8 ?- |) l4 Uimport java.io.UnsupportedEncodingException;+ t+ f, n% w! _1 A/ q
import java.util.StringTokenizer;
8 N( X$ {: n0 P, a. i6 k+ p* v( Ypublic class TXTReader {
  \! _1 {$ T" }0 k protected String matrix[][];
$ R' y( o8 C- i( E protected int xSize;6 |! k- ]* d* h! E
protected int ySize;6 M, f/ M& |7 U9 x+ _. p& u: m/ h5 N
public TXTReader(String sugarFile) {/ ^7 @$ c4 d7 \: S$ Y
  java.io.InputStream stream = null;
5 ~: k- C8 i) J' I) P0 L! \  try {) u1 R4 C& L8 G& n7 T! n5 ^
   stream = new FileInputStream(sugarFile);
8 k# Z" G' d6 w0 H' h. D  } catch (FileNotFoundException e) {
4 G5 w9 x! W" ?% V   e.printStackTrace();
3 G3 p4 q4 q* n  }& M* y) n: A: P0 V, J2 D( r' s- ]
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));/ r+ g# C' `" T5 H! F
  init(in);. _: B$ j' [/ |  U. I
}* [+ R1 a7 ^( F+ B: t
private void init(BufferedReader in) {2 t* _+ @+ \& u% [
  try {. L/ p6 |2 ]5 U* M
   String str = in.readLine();9 h+ {: X& ^! {
   if (!str.equals("b2")) {# D8 a& y5 r* q5 Z4 x2 g, W
    throw new UnsupportedEncodingException(
/ F* G) U% Y& j7 x7 B( P; ~      "File is not in TXT ascii format");8 E, f2 B1 x1 i; f/ _* }
   }
7 @* ?) {# p0 r% A# Y   str = in.readLine();3 v+ |1 f$ u5 K
   String tem[] = str.split("[\\t\\s]+");
! A0 t4 e/ |' S' H: {  [8 z/ ]4 j   xSize = Integer.valueOf(tem[0]).intValue();
5 a, ?( W: x1 z7 b   ySize = Integer.valueOf(tem[1]).intValue();9 Y1 u3 l5 w+ o, @3 _8 m4 p# g/ ~
   matrix = new String[xSize][ySize];" j) V: D. E7 q: j5 t9 `2 X; S
   int i = 0;: n" w% m7 p- i8 w6 P
   str = "";9 h/ Q/ N. N! E8 s
   String line = in.readLine();
/ f* b) [. p  I( E( R7 h   while (line != null) {
3 L1 l9 r: E. U0 L8 m. ^    String temp[] = line.split("[\\t\\s]+");
3 K7 ~. i: _+ V# c  c, g: E    line = in.readLine();1 E8 |9 h2 Y# A
    for (int j = 0; j < ySize; j++) {9 I2 K% c# \: o8 w
     matrix[i][j] = temp[j];
% ~) r$ ~) |% f    }6 D4 [! f. l% G
    i++;
! z; R7 `0 M$ F   }
* u, ~- ]" ?8 R5 ^4 u4 c* }   in.close();1 x- i" O* `' h4 ~- I% Z! o2 c
  } catch (IOException ex) {& d( {$ @/ Q- @; C
   System.out.println("Error Reading file");
3 g6 w& X$ `4 @. P2 [1 {. j2 x   ex.printStackTrace();
# N; ]6 x& R2 |( Y   System.exit(0);
+ ~% o/ {& r1 o$ f- i- G  }  J( X% j6 W$ Y5 _+ j! V
}
7 d$ f/ C4 }2 h9 K6 o public String[][] getMatrix() {8 Z5 c: f! L" i3 q
  return matrix;; w* P2 l5 w- [( h" e
}! j7 n, H7 P6 }, [' A6 U: N& z
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-29 23:54 , Processed in 0.013413 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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