设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10643|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
/ p, `# ?/ A( K/ T; D/ ~import java.io.BufferedReader;
$ i: f/ B' _& n* {# a6 r) @7 Kimport java.io.FileInputStream;& C0 o; h) e0 Y! Z8 d, N7 C
import java.io.FileNotFoundException;
5 f% A5 C. L) m/ x$ C* Cimport java.io.IOException;
  Z; x  [; @4 R* d8 Cimport java.io.InputStreamReader;
6 ?6 j# ^4 {6 r' }import java.io.UnsupportedEncodingException;
- @8 f/ [$ v) J; pimport java.util.StringTokenizer;
( o1 o6 f5 k" j# J8 @public class TXTReader {
9 `9 x$ p3 d% E5 z# e protected String matrix[][];
8 o- k; r; Z, X( F, \3 t protected int xSize;
5 B/ y( a9 i6 R( Y: O( f protected int ySize;
$ h' a. ?/ ?* k) T$ H) H public TXTReader(String sugarFile) {: u2 b- ^3 ?9 T3 v4 S2 `, L, ~; U
  java.io.InputStream stream = null;
) S9 U/ X" i7 W3 g( U- D/ i, k  try {
& O, f  _2 J9 j% [) {* ?   stream = new FileInputStream(sugarFile);
( {4 H+ t6 m2 _6 H( K  } catch (FileNotFoundException e) {
8 @) M1 n9 Q0 ?; B& }. V- @4 j. s   e.printStackTrace();
) y) f5 d: ]8 I$ O3 |7 ]) X  }
2 N  f5 s! c2 K; B  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
5 L, x$ P$ j, u  init(in);
% x4 g# _4 z9 @! s% j9 k }2 l. ^. V" V4 H
private void init(BufferedReader in) {% g! v0 a- _! z+ b; _, P2 e& Q8 T
  try {
7 }7 _" ]6 V0 N3 G   String str = in.readLine();( X: \  q& K4 \- H1 t5 i
   if (!str.equals("b2")) {
9 R" y: H  I4 [% Z( T    throw new UnsupportedEncodingException(; I4 f, Q! V7 S4 r6 K; H, Z5 C
      "File is not in TXT ascii format");, C+ `: U  I& P$ \0 @
   }
6 _7 [0 R% X& y7 b- c/ x; I   str = in.readLine();
4 h4 D" J% v* L: O( c* L   String tem[] = str.split("[\\t\\s]+");
; w; S& w$ y" Z! m$ e5 e6 F   xSize = Integer.valueOf(tem[0]).intValue();, s  A) P5 _- @1 u
   ySize = Integer.valueOf(tem[1]).intValue();. h5 A1 W' ~# [5 E0 L
   matrix = new String[xSize][ySize];+ f3 U9 l  x$ U) ~) t
   int i = 0;/ c9 C) Y. c* d" F
   str = "";" w; J* O1 w% |2 e
   String line = in.readLine();, l  `% W1 x) Q% w+ K
   while (line != null) {8 B; g1 i; o! d* m9 O. C7 E# I
    String temp[] = line.split("[\\t\\s]+");7 X% I. Y' [6 L4 a+ F/ A
    line = in.readLine();
' ?. o, O) ?! z; j! i    for (int j = 0; j < ySize; j++) {
+ t9 ?* A# e) y/ M; L1 n- m     matrix[i][j] = temp[j];0 m* @3 [' Q& c" Y8 e
    }
6 @" l5 ~2 c4 a- G    i++;
( J  ?& r4 ?- C   }
. ^6 f3 r9 ~3 t, E: e; d2 b7 u% ^   in.close();0 [% x* G0 h: x, s  Z& k
  } catch (IOException ex) {4 J+ E0 k3 F, H% G2 ]
   System.out.println("Error Reading file");
3 }: ?$ y+ P) a* b4 c$ Z4 e, ~+ @   ex.printStackTrace();8 X4 Y' _7 t+ G) M+ t
   System.exit(0);
4 n9 h( l" i/ F; T' Z  }
% p- j& r# M' p0 m4 F, W7 w }
5 I: b% q/ ]3 T7 q public String[][] getMatrix() {
' V% v* L/ E  P7 A3 Z  V  return matrix;6 I/ }7 x5 T# Y1 V
}/ Y5 T- X0 T& y, R2 X
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-20 20:01 , Processed in 0.015564 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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