设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10384|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;/ u$ T. ^1 p/ {1 c* {/ w
import java.io.BufferedReader;
6 r6 R5 Q# J# U. l% q; _: Vimport java.io.FileInputStream;9 N6 N  {6 @8 B4 Z& i3 s
import java.io.FileNotFoundException;4 G& i4 J6 @5 W* y0 ^/ @- B# v
import java.io.IOException;
! t  b" W2 N5 R$ D3 Fimport java.io.InputStreamReader;
1 k8 y/ K+ i; }( l5 }3 }& Simport java.io.UnsupportedEncodingException;
, B" K. |& e9 fimport java.util.StringTokenizer;
1 A5 i1 d8 @9 i# c8 m& r: |8 g; tpublic class TXTReader {) p+ L! C% K. W9 H6 ~- D
protected String matrix[][];
9 q% g1 m; U% |/ U) ~0 i7 B protected int xSize;
/ O9 ~+ f' x1 u8 X" J! J) n0 g protected int ySize;
5 b/ j$ T; A1 P1 J2 A public TXTReader(String sugarFile) {: w) Y1 T; y9 {( O9 Z- v/ _
  java.io.InputStream stream = null;
: \/ k; C, k+ E! M  try {8 R0 ?" M5 P% g, `
   stream = new FileInputStream(sugarFile);
2 v- }4 P) k! m4 N5 o  } catch (FileNotFoundException e) {9 `) S7 j' I: P* u! I- I* N) U
   e.printStackTrace();
# P' K2 ]9 n( [* ^' e  }, \! g- ^$ K* \  I1 h# L
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
3 n9 g# c0 k- N1 h, j# O  init(in);
7 {/ _& D9 N0 c6 K3 N }. _/ A" [# h9 g1 T
private void init(BufferedReader in) {7 s7 {8 Q- d0 z  s, i
  try {- B1 ^  V% Z/ Y1 J( _
   String str = in.readLine();
, P: ?4 e$ x5 Z7 X' Q2 K   if (!str.equals("b2")) {! n( T- ]0 F  `' n0 {6 _
    throw new UnsupportedEncodingException(
9 ^. m5 {- K( ]      "File is not in TXT ascii format");' P! t% [& v* ]8 r  L  U
   }
( G& {" ?' W3 e3 g   str = in.readLine();! ^' J* z7 s  |
   String tem[] = str.split("[\\t\\s]+");, W! j2 ^  H2 M" u0 L
   xSize = Integer.valueOf(tem[0]).intValue();
2 Y9 d8 t9 `$ j9 w0 v$ o2 z9 C! ~" b   ySize = Integer.valueOf(tem[1]).intValue();4 Y8 ^# S1 ^: ^- S  l! o
   matrix = new String[xSize][ySize];
) i: W; r1 A0 Q9 O# B   int i = 0;
! J1 H& L8 @# Z1 C7 p: f   str = "";- E7 P6 V+ J( Q$ `
   String line = in.readLine();8 U6 Y+ V: I" i% E
   while (line != null) {$ Z. _1 u' S8 J; F% X  c; N  k
    String temp[] = line.split("[\\t\\s]+");
' ^( j, _, V$ s4 C3 `    line = in.readLine();
# i2 Y# q7 I+ ]9 b4 s- u" r2 n    for (int j = 0; j < ySize; j++) {
1 K1 h" S. m! o+ m2 [. V     matrix[i][j] = temp[j];
/ M8 _3 d5 h" n  `1 Y. M8 v    }8 M# A8 P) b$ a' x
    i++;$ N8 c1 ~3 ~9 M: _; H, W/ a
   }
8 C& U. M- e$ w1 V   in.close();
+ Y0 O% H$ [$ V- t. N! s  } catch (IOException ex) {
  ^" D: a- ?+ x! S7 I   System.out.println("Error Reading file");
+ v; R% e/ n' S: u   ex.printStackTrace();1 ?  j8 o! Q4 M, d- i5 E
   System.exit(0);
! f# H0 E9 a' ?  }# i) Z  ~1 Q) J5 c
}% m" p- h  H2 D; H' D, j- ~0 b
public String[][] getMatrix() {
9 L9 b6 a- K7 j/ f, V, u  return matrix;1 S7 ^  ^4 z) [4 ^% d
}7 w( l; f7 {8 `% |! b1 \
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-7 14:36 , Processed in 0.015528 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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