设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7549|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;6 I/ E  ~2 ^3 H* w( Y5 L7 y' d* I
import java.io.BufferedReader;
" t- T$ \* q. ^) {1 V! ]% r# Eimport java.io.FileInputStream;- b4 F  e. D  T8 l' m, X; [
import java.io.FileNotFoundException;; W/ K: z  ~# g( h! K8 y
import java.io.IOException;
" p1 _1 D' r; W# Cimport java.io.InputStreamReader;' E3 d& F) n3 q0 l. @3 D1 s8 Y
import java.io.UnsupportedEncodingException;( G( m' k3 z4 E, m0 E$ @, W6 `
import java.util.StringTokenizer;
% }8 S3 V! u9 N' i  o/ f& Opublic class TXTReader {: [  ?* g' G, m- @
protected String matrix[][];
. `; @. \& h5 w  D$ J% `3 A protected int xSize;
, e1 e% r# j1 i. @0 \ protected int ySize;
$ F  m5 L2 n6 L$ E2 G& ~- W public TXTReader(String sugarFile) {
: H8 g$ M; K: P& ?! c  java.io.InputStream stream = null;2 _- |+ _( a4 ~. d1 L: q, |* k: y, q
  try {2 i- a# u7 T  X4 g2 n2 b
   stream = new FileInputStream(sugarFile);
3 ]: j% B- P7 S  } catch (FileNotFoundException e) {7 H7 S  c5 }' U
   e.printStackTrace();8 o" ^& Q' [6 x/ i5 f8 I# g% z9 }2 c8 I
  }
; e  z5 B, K! I/ o  BufferedReader in = new BufferedReader(new InputStreamReader(stream));  D$ \+ g- ~0 N6 G- h
  init(in);
- l+ P. q) A$ l }, s* H4 b7 t2 {! ]& _
private void init(BufferedReader in) {
) n0 x$ Q, c0 R. ]  try {
4 ?  L- l+ F8 W% E3 y   String str = in.readLine();
. b5 _, y7 k, Y' v" f- o, x   if (!str.equals("b2")) {
. `. c% r- _  f+ X' k$ `9 ~5 n# D9 u    throw new UnsupportedEncodingException(2 Q; X6 C. c* D5 I4 A) {( Y9 l/ |
      "File is not in TXT ascii format");
/ u& i% U, a& D) h" n8 [2 s, q   }5 h3 ?+ n0 }8 `* Y+ @% d3 c7 y
   str = in.readLine();# a6 @2 L  e8 }" g' _& Q8 c
   String tem[] = str.split("[\\t\\s]+");
* z" g7 i& Y' q# O9 ~   xSize = Integer.valueOf(tem[0]).intValue();9 {9 X2 T5 e* g, H
   ySize = Integer.valueOf(tem[1]).intValue();
0 O3 f% e3 X6 H# }; v   matrix = new String[xSize][ySize];
) Z  C1 [" K+ F8 {! ~. |' }& A& j   int i = 0;
) j; p+ b6 ^6 q+ b/ Z% e+ e1 B$ H  L   str = "";
) I% n6 [  S) N- f" B0 m2 C: b, z   String line = in.readLine();
9 b2 `6 p9 g4 f4 [& B) d   while (line != null) {
* l5 e. T- A" m: \4 J7 n1 G    String temp[] = line.split("[\\t\\s]+");
- ?" Y& M3 H" U$ x    line = in.readLine();' P" U8 x6 x3 h# F
    for (int j = 0; j < ySize; j++) {
$ B8 d3 H1 r0 }! ?! ~     matrix[i][j] = temp[j];
1 N# _' c9 N+ ]$ v8 r! ]8 n    }
- j- R, L- J$ F; C# o    i++;  D6 Z. N9 @2 A# k
   }7 a* X+ y. E/ Y
   in.close();
1 t- K, K& b8 U( j4 v! G  } catch (IOException ex) {
7 r( ?' [/ t8 d7 [' d2 d1 K4 \   System.out.println("Error Reading file");8 c* D, S3 @' P2 r
   ex.printStackTrace();
+ U3 F3 |8 c8 K6 c   System.exit(0);
. ^, j$ H* b# S8 C0 W+ F: U6 W7 t  }+ \3 a+ D2 {7 s0 c+ I
}
8 l* t" A5 H% A public String[][] getMatrix() {
2 y) g/ q& B! `! D/ r  return matrix;( M; [1 a! O) h% }& V
}' s& G# W7 S, @/ X5 D/ X( x
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-26 20:27 , Processed in 0.018194 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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