设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6891|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
3 M/ m0 g) _3 s3 z9 Nimport java.io.BufferedReader;
  j% j0 N* K4 f4 ?4 \import java.io.FileInputStream;
0 b& Y! x2 \) o# N0 K, Aimport java.io.FileNotFoundException;
9 P: @7 N- O# U) Yimport java.io.IOException;
- N+ V. X4 o* y5 X7 U: Cimport java.io.InputStreamReader;
3 s5 V$ i2 o0 ~4 kimport java.io.UnsupportedEncodingException;
0 F6 A8 _$ r& @' s: D$ N. q! W) jimport java.util.StringTokenizer;& H" f7 M% D# ]+ {
public class TXTReader {
' a  R$ F8 \! Q8 E: R protected String matrix[][];
3 P7 X1 \  l" o% e2 u protected int xSize;
- G7 _5 @; i' z' W- k8 E protected int ySize;' [% z$ A6 t0 W' m8 i
public TXTReader(String sugarFile) {& ]9 |$ t2 c2 \
  java.io.InputStream stream = null;
3 l; X7 }4 U8 Y: @  try {( t' x% Y: d' {' X8 |- H
   stream = new FileInputStream(sugarFile);
# \5 m( b! r& j  } catch (FileNotFoundException e) {
: `* M8 j& u" G; o: A5 R   e.printStackTrace();+ M& R8 H" C* N8 J; E
  }  s6 F9 m6 Z1 \
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));8 V& v- L. R6 t$ K
  init(in);  y$ K. L- p+ w' p# `! _
}$ Z( j; p6 k" m/ `
private void init(BufferedReader in) {
1 v$ G! o" N3 \  try {. D: d( C5 _3 C  I/ Z& p7 X
   String str = in.readLine();
+ x+ M( o+ V& g- \1 Y9 W/ S   if (!str.equals("b2")) {
# d+ [6 E- [5 I, g    throw new UnsupportedEncodingException(
- ^7 d% \; w( A) o# `; Q& @      "File is not in TXT ascii format");4 J" {/ }1 F0 Q7 @
   }5 \/ _3 m8 W" F/ c' ~
   str = in.readLine();) v- L7 }0 s+ d4 D  H2 n0 h1 |5 z
   String tem[] = str.split("[\\t\\s]+");
5 J: w4 l9 I6 A0 I" z   xSize = Integer.valueOf(tem[0]).intValue();( V6 B# L. V: _5 C1 K
   ySize = Integer.valueOf(tem[1]).intValue();2 v# c* s+ k6 {& P  s0 B
   matrix = new String[xSize][ySize];5 @( K# \! t& L3 n7 Y
   int i = 0;8 y8 H$ _6 F. L" G' h$ K6 U1 u
   str = "";% S1 E5 G5 m) Q* j
   String line = in.readLine();
6 N; ]3 x0 [3 r- ?* @( n$ P; }   while (line != null) {# @3 D" I7 X2 z3 W4 v
    String temp[] = line.split("[\\t\\s]+");
) m/ Q: ]) N- l4 ~; h5 ~    line = in.readLine();! U. W- R5 X& e" }6 @
    for (int j = 0; j < ySize; j++) {; T/ c( [2 L. L  `
     matrix[i][j] = temp[j];) e8 e* {4 g9 K4 w$ ]3 I
    }7 X- a2 f2 x6 ]
    i++;
+ G, q6 [% [0 L* F   }6 G2 r9 U( M" ~5 h9 M7 B
   in.close();
: Q+ X/ T' e# L) u  } catch (IOException ex) {# @$ N% g+ V; W& x5 U; O& n9 {) m/ P4 [
   System.out.println("Error Reading file");
( N# W! N) }; H; G0 C# c$ U) p   ex.printStackTrace();  a3 j$ F1 `2 J! e7 K
   System.exit(0);: A9 Q7 L! q+ `1 X/ E+ Z! E
  }2 `1 e: I3 M( Y( I8 R
}
0 \7 d5 W2 G' o8 n. w$ {* n* r public String[][] getMatrix() {
+ C# m7 b& n1 f$ T# ~+ \8 t, I9 s* L  return matrix;- d6 ]+ _8 e: P/ u/ I+ }8 p
}
2 F5 J5 F  `7 R- n$ ], U' U}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-9 13:25 , Processed in 0.016456 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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