设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6725|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
) V8 n* Y8 a! \import java.io.BufferedReader;
% A& \" `' W0 j% w  ?' Kimport java.io.FileInputStream;
& r+ L4 N! f4 _import java.io.FileNotFoundException;
& D" X* R! R  v# Q- f5 dimport java.io.IOException;0 w! G9 P8 S3 m( r0 N) Q
import java.io.InputStreamReader;$ l0 _! C% k5 h5 Z+ A
import java.io.UnsupportedEncodingException;' D* K/ D3 Y# c% I: E$ A$ {1 t
import java.util.StringTokenizer;
) x) l' s( C5 H+ ]; _/ z. opublic class TXTReader {
% s# \9 q! m, q* f protected String matrix[][];
" g% W: Y7 C  X! M3 m( z protected int xSize;
, ?- z& \1 }1 @6 v# n7 C& O7 {. y protected int ySize;5 }* M( @9 I$ T8 U1 |) E5 z- T
public TXTReader(String sugarFile) {
# q1 A. |1 i# j/ `- I8 {. p- k  java.io.InputStream stream = null;/ s0 n6 z+ W+ w' J7 w, t% k7 J
  try {  S) @! G% q7 p  s3 C2 O
   stream = new FileInputStream(sugarFile);
7 r  f5 g: a& c  } catch (FileNotFoundException e) {
; ]/ H7 N6 a4 y# b# d  B! F( M   e.printStackTrace();
0 p# V" _' J: u; z$ [! ?, P4 H5 Q  }
1 i% X& T/ e% ^" y% V  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
* `- k1 R" O, `5 j2 w1 D; i( P  init(in);
6 G; t- ~( ~" Z }
# T/ r3 W5 h) E2 W) ?! g6 c6 z3 Y private void init(BufferedReader in) {8 {" y# q: D! N* o4 q6 ?
  try {
6 [. W, C8 W5 a; b# a   String str = in.readLine();
  y& n6 j5 y: L, r* M1 c   if (!str.equals("b2")) {8 M( C: U- f/ i0 P; D3 Q
    throw new UnsupportedEncodingException(
' W( D0 _: A! t  a. h( O      "File is not in TXT ascii format");  J  T6 Q4 p& v% W* D! H
   }
3 G- n0 V: |; ^$ X& P5 H   str = in.readLine();
1 W) N8 u/ W) d" O* U6 z0 k6 r   String tem[] = str.split("[\\t\\s]+");
8 ]# N% m1 {  y- G$ V   xSize = Integer.valueOf(tem[0]).intValue();2 t, z; I9 \  h! X; C% U; v: |1 S. M
   ySize = Integer.valueOf(tem[1]).intValue();
/ k4 H: r$ e) m; s' k: ~& g   matrix = new String[xSize][ySize];
) n* k0 _/ q/ {# V, L% v3 g0 ~   int i = 0;
, h& E, e8 _1 W0 ^: }. S   str = "";
  O9 O! U0 q/ ^   String line = in.readLine();2 b% A5 E7 d/ \5 f! B3 n2 X
   while (line != null) {
1 n% K1 _1 j1 r& p9 {    String temp[] = line.split("[\\t\\s]+");
7 f4 ~$ W$ I6 P' P1 i    line = in.readLine();* e; h3 P& G- ^! l) B
    for (int j = 0; j < ySize; j++) {# z' l# v6 j8 i. y# a
     matrix[i][j] = temp[j];6 [9 s% {; S' u! t
    }( x# w' z1 j6 z8 Y# Z) f, g
    i++;
  @. k: G3 U) K/ D! X! L   }
% O5 |6 w% a2 I- J& Q; }) M  O   in.close();# T* ?! J1 |- H5 r
  } catch (IOException ex) {$ C) x$ n/ c" v) m. R! h
   System.out.println("Error Reading file");
2 x+ Q5 ^9 m8 N8 }# p   ex.printStackTrace();( Z9 \; L) c4 \5 x% V
   System.exit(0);
# _; e' Q2 A) ]2 ]# p  }: c; J  q. x+ H0 d1 O5 D
}
7 j: ?" O  K5 F public String[][] getMatrix() {
9 Y# [$ b8 A- c& N( U  return matrix;5 E2 Y- o& r7 S3 S% s
}. c. [. a3 d( F4 A3 |
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-29 16:40 , Processed in 0.014039 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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