设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6954|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;! `, e) e7 V$ N6 J3 ~' {! y% H: H
import java.io.BufferedReader;4 R0 I3 R$ b$ C$ b# m
import java.io.FileInputStream;
" }2 ~: P8 K. H2 W, Q/ V$ jimport java.io.FileNotFoundException;
2 B- l* S' x, H% \( j. P3 Gimport java.io.IOException;
2 m) B* z! V/ c8 \4 {$ E' |( G, F% qimport java.io.InputStreamReader;( ~; _4 s; L0 w" w- ~! M7 l( s
import java.io.UnsupportedEncodingException;- Z+ H- x/ W8 B) i+ \$ ?
import java.util.StringTokenizer;
5 Y, O3 \. F3 k6 C5 a. w' mpublic class TXTReader {  l/ F9 M4 F& W
protected String matrix[][];
) T" r/ \" D, o8 ?# ]0 F1 L protected int xSize;" S, D0 f5 L' ~0 h+ a5 b: m
protected int ySize;
+ r$ L2 g* N& u; B0 |$ d0 W public TXTReader(String sugarFile) {
% z! h  w, h- H. h6 B; Z8 E, b, C  java.io.InputStream stream = null;2 e6 y" P; o$ \" E3 D
  try {7 S/ y( i* {# N8 o7 {, {& t* w
   stream = new FileInputStream(sugarFile);
6 L. S4 q. a( W  } catch (FileNotFoundException e) {/ v3 C- b% u8 Y) b- q, K
   e.printStackTrace();# J) c* h6 a" n* Z
  }
  z4 @/ U* G( |8 C9 b! C/ J  BufferedReader in = new BufferedReader(new InputStreamReader(stream));6 c$ ~) Q/ ~+ ]+ T
  init(in);9 W. L9 c2 {5 V' r
}
, K, S5 a$ H3 U  r6 G+ ]" }* l4 `/ V private void init(BufferedReader in) {
# ?- x2 V+ h" Z2 R# f  try {# a9 A2 P3 p' I
   String str = in.readLine();% P* _3 ~4 j3 ]4 I3 Y  |% W  {1 S
   if (!str.equals("b2")) {2 Q' x2 E5 H/ q7 c  Q
    throw new UnsupportedEncodingException(3 U7 F2 c! r  ~* r% E% P8 B# G
      "File is not in TXT ascii format");
2 A7 X# R( @  {" |( {7 k' W   }# I6 A. A/ @2 Q: Q. e
   str = in.readLine();$ B" ?3 V  B1 V: q- e" E/ s
   String tem[] = str.split("[\\t\\s]+");6 K( D) y- [  t5 E+ |
   xSize = Integer.valueOf(tem[0]).intValue();
8 T: u- ]7 d/ j" n- c3 s  j) q   ySize = Integer.valueOf(tem[1]).intValue();
) W& Q7 X$ {$ z/ P# k   matrix = new String[xSize][ySize];$ n! }, V2 A) O( v8 G9 v9 G
   int i = 0;: i$ p- n2 s$ d/ V- f' s: n
   str = "";
, p% m7 A5 ~. ?. C. R   String line = in.readLine();& Q0 x" e& E, T. S/ n
   while (line != null) {+ i/ W5 Y: M$ l" o% {- x
    String temp[] = line.split("[\\t\\s]+");
  G. i: J  K* r2 n5 g7 z! c    line = in.readLine();8 ~3 d2 G7 d7 }
    for (int j = 0; j < ySize; j++) {
$ ?% S$ v8 {8 P! Q% y, O: ?6 m     matrix[i][j] = temp[j];+ H: {/ C: @* |5 q- O
    }
. o* m- d! L1 y# |) [4 c! ^# m    i++;
- R3 Y9 A7 s5 B9 R5 D' P. u; U3 R   }
/ J4 p2 e8 s5 D! ~% O   in.close();
/ |# }- s! T7 Q; A) b: z  } catch (IOException ex) {0 B8 z1 i$ Q8 _9 |2 |3 L6 C+ O
   System.out.println("Error Reading file");
' A- M) x  r6 n/ I; M   ex.printStackTrace();1 v2 @5 t$ r9 |& Q9 H
   System.exit(0);3 l  G% r& Z! V. _
  }
' B) d$ j) U/ Z1 g1 D9 L, c7 M }
# h5 ~- c* K2 G( t9 `; \$ q# _: ] public String[][] getMatrix() {1 B" x- t( A' ?( m# n% \
  return matrix;3 e  U9 Q* _  H6 S& l# {
}7 {' F- M- Y: Y5 \1 V
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-12 07:38 , Processed in 0.019832 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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