设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5677|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
$ x+ @: Z$ Q* K+ d$ Timport java.io.BufferedReader;! Y9 t( F. ]* X3 |% ?4 w
import java.io.FileInputStream;
: ]# C, @4 F5 T/ h0 [/ Kimport java.io.FileNotFoundException;5 d/ H5 {8 g/ \7 Y; t
import java.io.IOException;
% j1 |2 v8 O- L8 C/ _9 Zimport java.io.InputStreamReader;
! x, l1 J1 K  J$ q# A/ Rimport java.io.UnsupportedEncodingException;
+ K6 I0 a6 G/ k# jimport java.util.StringTokenizer;
3 D% _" G# V4 I0 {public class TXTReader {
* G; X+ a& {2 q& x5 R# r! K protected String matrix[][];# e4 i/ L( a3 S2 h& @! m$ ^1 {; X
protected int xSize;
& x0 F. L) H+ B protected int ySize;8 p7 ^1 f6 ?! d. \  B1 J
public TXTReader(String sugarFile) {
% T) f$ Z1 O7 Q. a  java.io.InputStream stream = null;
# h5 b8 H3 W' @  E6 \8 X% \  try {
) ^; s* W+ ?. Y8 ]; v1 j   stream = new FileInputStream(sugarFile);6 l: Z. l7 S  T, g' ^
  } catch (FileNotFoundException e) {
; G# G6 t) o1 c* S8 E   e.printStackTrace();
5 ~% U, l: L9 c% I: V9 B+ F& Q  }, N/ f/ e) N8 F+ u+ R# d
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
) x; J$ _! Y: P2 x0 G  init(in);; \3 _$ l' U- y# O. R% i: h
}
3 r- G' }6 I3 ]' u& q8 x private void init(BufferedReader in) {1 x3 h8 @" L: N& c
  try {6 x( P: n1 u7 N9 @9 d# L
   String str = in.readLine();9 k' X7 X; v1 Q" m' N1 S
   if (!str.equals("b2")) {
% n- X4 Q+ y8 o: v- V    throw new UnsupportedEncodingException(
. r5 E* j4 I, w5 @+ d$ {& X$ N      "File is not in TXT ascii format");4 P' ]( M: U+ l
   }
' z' w( V3 K% P# a" b   str = in.readLine();8 w. _; b" l/ z& J$ i
   String tem[] = str.split("[\\t\\s]+");8 f) @4 R4 N* A7 n3 o
   xSize = Integer.valueOf(tem[0]).intValue();
# P: t3 n1 i0 W: }: a   ySize = Integer.valueOf(tem[1]).intValue();- f: J& l6 E  u" l0 a; s
   matrix = new String[xSize][ySize];
) h0 N& [) I9 ~9 p' Q. @   int i = 0;
4 w- T/ E' D  W   str = "";+ z: k$ F$ }# x1 c
   String line = in.readLine();
1 [/ a8 J( a* x$ l* f7 ?6 X" S# G   while (line != null) {# _3 p; E  \; U. i" A
    String temp[] = line.split("[\\t\\s]+");
4 m0 H, w+ {7 @( |    line = in.readLine();
" V  K- I! ~) v2 N5 {/ I    for (int j = 0; j < ySize; j++) {% g' ^/ r$ Y+ r# P
     matrix[i][j] = temp[j];3 s1 ]" o) `; r
    }: |1 {8 ^# r0 h+ m% {5 ^
    i++;; o! J% u: _2 Z* j* l% \/ W) p
   }
5 n  W' \8 t& `. u$ z   in.close();
7 _1 ]' v' H& |7 e6 K' @/ y  } catch (IOException ex) {
& r# x2 a: ^  I; Y2 G' j! }   System.out.println("Error Reading file");
7 e: Z/ ^' j. h, F! I, R, t   ex.printStackTrace();
4 Z; \5 ?1 q; W: D+ J' h" H3 ^   System.exit(0);
1 q5 _; g# L. {& U' a" H  }( I* J8 @$ f9 h; L/ C( M
}$ Z. q* }. R4 b7 ~" {* y
public String[][] getMatrix() {" D0 _/ o  f: O; d
  return matrix;, R9 {) G1 q3 [! n
}
, V2 x0 ?* f/ E, }/ w}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-19 04:05 , Processed in 0.024581 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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