设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6956|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;. l6 o/ p3 D( r4 U+ j8 `; Z) E6 g
import java.io.BufferedReader;4 ]; P  T% P) P& D6 l
import java.io.FileInputStream;
4 @' S% Z5 d. ]; W' aimport java.io.FileNotFoundException;
; l" `4 M- C( @2 Y7 ^import java.io.IOException;
! n1 o# v( C+ O4 t' Pimport java.io.InputStreamReader;$ e( j/ d' x. u6 S
import java.io.UnsupportedEncodingException;
. p- ?" ^$ f( Pimport java.util.StringTokenizer;6 P" z+ r: s+ ~: h0 A' ~1 R
public class TXTReader {
- p& T( d; K' y) a5 O protected String matrix[][];" h% }. N$ ~; q+ P! N
protected int xSize;
- p' O( q* Q& t protected int ySize;7 Y: j5 @- {$ @. B. L9 g0 g+ c
public TXTReader(String sugarFile) {
" l) `; \/ F7 u( V( w& \& l  java.io.InputStream stream = null;  Y5 D, V1 U2 S) e2 s" h
  try {
9 Y  |$ V- s6 Z4 i8 A   stream = new FileInputStream(sugarFile);
% {7 |  l; C( N. A- J3 I8 L% H2 A  } catch (FileNotFoundException e) {
+ m4 {3 x  \. |6 `1 B& b% R   e.printStackTrace();
% c* p% F6 e) x  }
% H+ ^- z! h" N  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
9 D" v/ R2 B  P5 _: ]  init(in);0 K8 t/ `8 |) R+ w6 r
}5 h3 u) j. n, @' D& {
private void init(BufferedReader in) {
9 A. N" b# t1 R3 \$ J: Y, l. k  try {6 o( [+ g. @$ M3 E2 }0 V' m7 `) ]
   String str = in.readLine();
6 Q/ D( M2 O5 c- \& k4 A- M   if (!str.equals("b2")) {" l' j  |( u1 V  y
    throw new UnsupportedEncodingException(
/ u. M/ E$ q' A4 U) h      "File is not in TXT ascii format");) `( h" ~$ M3 a+ c
   }
* q1 y# x' Z# X; I3 Q* O   str = in.readLine();+ ?9 l( }' ^5 s- l
   String tem[] = str.split("[\\t\\s]+");  M* w4 Q6 F& C+ X# ?" _' K5 J; d. `
   xSize = Integer.valueOf(tem[0]).intValue();
" Y; R" R8 R6 w5 h   ySize = Integer.valueOf(tem[1]).intValue();; ~# m$ S+ T2 d- C8 r+ i+ T2 g
   matrix = new String[xSize][ySize];
4 j. H! O7 V2 d. E2 m: X, U6 A   int i = 0;
: P" r$ N) X4 q; {7 C# @3 a$ `6 Y+ p   str = "";
1 K/ K' f/ d9 Z, f   String line = in.readLine();6 J& K. b- O/ |/ _5 A5 O
   while (line != null) {
4 i) _6 f7 T( F" L3 |    String temp[] = line.split("[\\t\\s]+");
6 ?" x! f% p; a$ c8 q+ k4 L    line = in.readLine();2 p2 Y' U6 L( ~% }( I, j
    for (int j = 0; j < ySize; j++) {8 x! n7 O( S9 P) {
     matrix[i][j] = temp[j];2 w4 W" [8 F. Z! J% ?( G
    }# y8 q7 s, x& X) s3 h  @
    i++;( D% j" R3 P8 h: G8 a
   }
4 k3 J/ H8 \9 I; o( J   in.close();
2 O: I% _  U: p$ B% c  } catch (IOException ex) {
0 G% \1 _6 t% C! W2 w4 s   System.out.println("Error Reading file");
+ P* M0 P" t# g0 G6 f1 T   ex.printStackTrace();
5 q& r  Z, c8 e  v* c   System.exit(0);/ b& h2 r! j/ C0 t, V$ Y/ x
  }
4 ~; e8 t5 ~: E, @ }
3 u% \9 R" q' h: h1 I1 \& U public String[][] getMatrix() {
# s3 A% ^9 P0 {) K. B$ Q  return matrix;$ U: [' p9 o8 z
}
1 _- v6 U1 C* Y! C' ~' O7 ^3 p' s}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-12 11:35 , Processed in 0.019290 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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