设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7526|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
2 g6 \; \: y( [& Nimport java.io.BufferedReader;* Y9 W1 i- u. ]1 e
import java.io.FileInputStream;8 R2 U& Y3 C6 Q1 `+ \# M0 X) Y
import java.io.FileNotFoundException;5 k7 d+ M/ x9 K  r4 P' ?
import java.io.IOException;
0 \% E) H" b! ]( Y* Nimport java.io.InputStreamReader;
4 k8 M' c% s2 ^$ Bimport java.io.UnsupportedEncodingException;
6 C+ @. Q/ e( K+ i1 _2 w. D; U# P% L4 qimport java.util.StringTokenizer;& d0 {* W' b% `8 k
public class TXTReader {
+ J6 h2 Q3 }5 Z" P' v, ]0 X; y" y2 I protected String matrix[][];$ S) [" u; ?3 w! A, x
protected int xSize;0 S" g, J; m+ Z1 ~9 P4 w6 I
protected int ySize;9 w7 m5 x4 Q' d  r. ~# s
public TXTReader(String sugarFile) {
. w3 C6 w* D2 p1 s# |  java.io.InputStream stream = null;
% W5 v) _2 V$ [! }  try {
0 s2 q" K1 F& p" U, J) f/ h* N, O   stream = new FileInputStream(sugarFile);
7 l* T' ~! U4 [9 |/ [) o& }0 I' O$ L' e  } catch (FileNotFoundException e) {
$ ^6 v7 [( j0 d* p/ H   e.printStackTrace();' D# V5 r  E' c+ S  W* J3 _
  }+ u/ y- {  r# O5 v
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));" m( I3 u& b5 b8 f! w9 k9 ]5 A
  init(in);
2 }! b) o3 D* W2 M' D( y& B }/ |1 N; B1 ?4 U# R
private void init(BufferedReader in) {. G0 ]8 X$ \$ c$ k9 S( ?% e
  try {
7 _8 R2 v; }6 q: M9 w( X   String str = in.readLine();' B8 R5 t6 b% k4 b
   if (!str.equals("b2")) {
% o6 Z5 ?5 D& B' H/ Y5 j% m: ]$ I' M    throw new UnsupportedEncodingException(
5 _( ~( a2 N$ U4 z7 Q" L      "File is not in TXT ascii format");
; ~& H) [6 Q' m# V5 r   }+ K+ _8 f3 _6 V) Z$ }) t
   str = in.readLine();( \3 ]3 c/ l4 t4 ~8 G
   String tem[] = str.split("[\\t\\s]+");  z& m0 c' |/ S7 [$ r% |2 J
   xSize = Integer.valueOf(tem[0]).intValue();! \1 ?* ?& v4 r0 n7 o
   ySize = Integer.valueOf(tem[1]).intValue();
' v) N; W1 m' u   matrix = new String[xSize][ySize];
. H4 R) }& }8 R+ ]+ t$ l   int i = 0;/ p: `3 K0 C/ `6 e7 }( b
   str = "";: U* h0 w: Y3 U6 c3 y0 ]( D
   String line = in.readLine();8 B  w6 i- H/ [/ O# U4 C
   while (line != null) {! [5 Z- A! _) `) l5 ^% ]( R
    String temp[] = line.split("[\\t\\s]+");
8 J' J. Z3 e9 v% v, ]; I! W    line = in.readLine();% ~; k1 Y; r; |
    for (int j = 0; j < ySize; j++) {# K+ c% |, u" h7 Z) Z! v9 W
     matrix[i][j] = temp[j];6 m1 E% r4 V% H; m7 A
    }0 d9 V! v+ ?- m) F2 c
    i++;
4 S  H* o2 r6 z6 C1 N6 ?6 |) Z4 v   }. U& o$ D/ m) L2 P7 V
   in.close();0 z: K, r: Z7 u- i6 b% K
  } catch (IOException ex) {
8 _9 I- h7 U7 i1 ~   System.out.println("Error Reading file");$ f0 k) a/ A3 v; G- c* g, ^8 E
   ex.printStackTrace();
& J. L- b  Q% K   System.exit(0);/ Y% G3 E) |' Q
  }) G2 ~2 T) @4 m
}6 ^2 ]" P$ c" N# l
public String[][] getMatrix() {
$ b! N8 \3 `9 J% a/ D" @  return matrix;3 H  G8 Z1 c: y* H
}. H8 Q% D6 A" ?) ]# f, @
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-24 18:42 , Processed in 0.018404 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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