设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8052|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;4 P- T& U. s* ]4 Z5 V
import java.io.BufferedReader;2 f; t3 f. f* M
import java.io.FileInputStream;+ {) y3 p0 ?2 c9 a( u: c7 h4 B7 @
import java.io.FileNotFoundException;" o/ G' X) J, z/ o5 H+ W
import java.io.IOException;6 I  {& }: [! c4 T; ?
import java.io.InputStreamReader;* U* _3 U+ \, T* E4 n' [6 L
import java.io.UnsupportedEncodingException;
7 D; ]% K/ }* i+ uimport java.util.StringTokenizer;
. e$ Z0 [4 R; e" Q" Apublic class TXTReader {3 \) T: R: U9 H2 m
protected String matrix[][];
( h9 y% s# y4 R2 B6 x2 F8 d protected int xSize;
5 |# H3 k5 {1 K/ G/ X protected int ySize;
8 x( x. w' d; A1 [0 t) w# F public TXTReader(String sugarFile) {( i* m% U0 f. H. c
  java.io.InputStream stream = null;
! H8 p) O& B  i0 I9 i7 f  try {9 _, M& ^- |* P/ ?* b
   stream = new FileInputStream(sugarFile);
2 U. d5 W( T4 k+ I  } catch (FileNotFoundException e) {
  Z, g9 W/ Y3 ?7 x   e.printStackTrace();
( J  K/ o& M) f% `. T/ A. ^  }
3 D' _- ^# l. A4 p& T  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
9 U/ n+ v( q0 m! R- m. @/ M3 L- j- `: {  init(in);: w6 i2 v% U7 O
}
8 h8 h3 z# v( d% V% b: {5 |0 Q private void init(BufferedReader in) {9 f$ B  [$ k$ o# D7 z, \
  try {) D* s& g( H: o# ^& C# D% F
   String str = in.readLine();
0 W+ k& g! O6 s& |& |   if (!str.equals("b2")) {  ?! d! k* a2 e( c9 d0 G" |; a
    throw new UnsupportedEncodingException(
7 |. r; s# F0 q: N5 [5 g$ N      "File is not in TXT ascii format");
7 `; `9 @7 ^- W   }
  e5 b' M9 O! e2 |' Q) ~- E' l   str = in.readLine();; V" O: ?( Z4 V. l0 m
   String tem[] = str.split("[\\t\\s]+");
, {' L: X  D% E; H: ]   xSize = Integer.valueOf(tem[0]).intValue();
; t* V6 w9 ^8 K# f: F% w   ySize = Integer.valueOf(tem[1]).intValue();% }3 W# q% H' K! h
   matrix = new String[xSize][ySize];7 o9 n+ Q4 A/ ?( p9 B
   int i = 0;$ L, }. ]8 X+ I/ l
   str = "";
# n- Y, L7 w  ~; t5 Q   String line = in.readLine();
) q5 |3 h$ [1 _- J( y' M" F   while (line != null) {
; P: @$ j3 b) g$ ~    String temp[] = line.split("[\\t\\s]+");
* m, ]4 K- u4 v4 P    line = in.readLine();2 o  D4 o- A8 i$ \  X" e* _' R
    for (int j = 0; j < ySize; j++) {
' c- t( n2 M; o( l     matrix[i][j] = temp[j];
3 @+ l& Z8 U8 m; T( N    }/ ~% Y# b3 b6 @3 G$ e9 f
    i++;; G' R. D1 T' K1 {- T
   }
% M; r$ S, v: Z( D1 R. _+ {5 ~   in.close();4 U) s% P7 x# d0 R' f( ^" x9 N
  } catch (IOException ex) {
9 R  m7 O3 D, [3 O- }* C5 B( T   System.out.println("Error Reading file");$ z* N. \. D3 f/ @
   ex.printStackTrace();
" V6 f/ o% \# N  }/ w, V   System.exit(0);
# y7 u/ {! u  h  }4 b! ^7 S8 w+ `$ x. }
}
4 G4 I6 A+ [8 O$ N* {5 s. M public String[][] getMatrix() {5 y$ W7 V2 z; K. E& Y6 Y% T
  return matrix;. ]4 X7 I4 z- J/ ~: T5 d8 C
}
: @5 x! X) [5 E}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-4-29 17:11 , Processed in 0.013983 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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