设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6248|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;/ K4 U; d) f  @! t( n
import java.io.BufferedReader;2 R% l7 K& F! \9 r, ^5 ?2 s, m; _
import java.io.FileInputStream;
- X0 j# E3 g2 eimport java.io.FileNotFoundException;0 b1 A0 \1 _1 r  J
import java.io.IOException;1 C1 _) D* q3 J4 V+ J+ ^
import java.io.InputStreamReader;
5 [# B* F" j; M# L7 C# qimport java.io.UnsupportedEncodingException;
5 x3 X$ B. a3 v* t0 ~' g2 fimport java.util.StringTokenizer;/ i$ \. S( t! f# s9 |0 u
public class TXTReader {
# C% \- G, f( l( I6 g, [# H protected String matrix[][];
$ v) n4 {( a! q protected int xSize;* T; B! ~( Z$ y8 k: ]) D( X
protected int ySize;
! y, d, G6 Y" X/ i  [ public TXTReader(String sugarFile) {: y  A' \, @- n; v  Z+ y
  java.io.InputStream stream = null;
5 c4 T0 N" K/ N4 n, K, @  try {
1 T+ \: l" r1 w& M7 M   stream = new FileInputStream(sugarFile);
, V9 z1 L9 d1 l2 d. @2 W: r  } catch (FileNotFoundException e) {/ N6 L3 j6 h7 d. X' d; V. L
   e.printStackTrace();( C9 ?9 K3 s7 W4 \3 d  e
  }
0 g' U7 q" }4 ]. B$ v  G  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
& j4 T2 ^, ?5 l  C) A  init(in);' X+ R, V4 N8 R& h! v
}- K# \% n* J% m
private void init(BufferedReader in) {1 D) ^4 \3 U* [3 z" M9 d' H' {
  try {  M, k+ L! Q& J5 J4 ]" N. \& f
   String str = in.readLine();2 a. I" p9 O* q1 w
   if (!str.equals("b2")) {
4 H/ @9 ]& g1 M    throw new UnsupportedEncodingException(
, Z+ i6 F4 P, }- k8 ?) l      "File is not in TXT ascii format");
1 \0 v  o/ x5 Y# F   }
7 I0 ^, ?- Q0 H! g+ e: ~( m, R   str = in.readLine();
2 }) `( b" g0 A4 [4 M7 A5 l0 Y1 P   String tem[] = str.split("[\\t\\s]+");3 \, x% o* b) A8 e2 t7 X5 u
   xSize = Integer.valueOf(tem[0]).intValue();! M6 E- K6 H4 q# n$ y0 T  t
   ySize = Integer.valueOf(tem[1]).intValue();
7 b- W' q0 h. n- ^# ^7 w8 v! J   matrix = new String[xSize][ySize];
* @% P1 k9 P- o  H  X- y   int i = 0;
# L* {2 c1 F. c. t7 g/ O- J& k   str = "";$ J, c% }& p6 q6 J8 ^5 P9 \
   String line = in.readLine();
/ i7 D0 d6 X8 |$ q( e4 T2 Z   while (line != null) {
: p: d& K* s8 x% W& J. D    String temp[] = line.split("[\\t\\s]+");# O' E; I6 v2 b+ ~6 f2 j" {
    line = in.readLine();
" `7 V1 g+ t" b( U+ j* x    for (int j = 0; j < ySize; j++) {
8 {/ b/ ]4 E6 V! }     matrix[i][j] = temp[j];
: i4 F+ K/ b0 ]9 t+ J    }4 |- @1 t* e& y# W
    i++;" J# f4 m( A5 t: y2 I( {8 O
   }
/ x  \# m" e! k0 [; e( t. }   in.close();
! f: q, t/ g7 O. P. B  } catch (IOException ex) {
( U3 g' c. X7 d/ u2 ?) Z   System.out.println("Error Reading file");/ I$ k! p. p) M6 r6 c/ @
   ex.printStackTrace();
8 p2 S+ H3 w+ Z2 ^7 q; \3 ~   System.exit(0);
; B4 a* P. c$ R' n2 d6 ~  }5 a& b- U6 U: h4 [; V$ w
}: T( `9 q9 Z1 w% j- Y/ l& n8 S
public String[][] getMatrix() {7 S  z* P/ X" _0 V; e
  return matrix;
' |# T1 z; E4 f+ v" L }( a' h2 m$ S4 K, e8 ^
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-18 12:11 , Processed in 0.016849 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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