设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5822|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
5 a! ~* s, m( y9 jimport java.io.BufferedReader;8 s/ ?2 `0 F. l, V0 |; [
import java.io.FileInputStream;6 O! X5 W" ^/ C1 V: u
import java.io.FileNotFoundException;! ~$ Q+ F5 G$ s! S& o5 U' L  c7 z& D
import java.io.IOException;
. ~4 m) A4 B: Y0 Himport java.io.InputStreamReader;
8 t% h' R( ?# h6 Cimport java.io.UnsupportedEncodingException;3 Q* l- P7 J) \5 x) B! l
import java.util.StringTokenizer;* S. B6 _( |- i3 K' s
public class TXTReader {
3 F- O$ k' `+ w+ N) p protected String matrix[][];! A% t1 t& N. B! G) K0 v
protected int xSize;
5 ?: Z+ I2 O4 f, y protected int ySize;
! D; q. g% d7 z public TXTReader(String sugarFile) {) ^' i6 o& J9 k2 U9 f1 ]5 @
  java.io.InputStream stream = null;; x0 Z2 e0 j- v9 X7 n# t
  try {
7 I; `1 P! x, a3 [. _  [0 c9 B   stream = new FileInputStream(sugarFile);/ V( M. Q5 P& C
  } catch (FileNotFoundException e) {
7 s- u: E& ^$ Z' I$ e# e& @   e.printStackTrace();
# F& `* n8 o/ q2 I" ^! q  }3 j1 v& e% \9 Y( q- S/ e1 e
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
. l5 v8 f! |- ^$ s! o' U6 S  init(in);
3 b6 K3 h! A# B5 V- B2 v! q }
1 x0 j/ |" b7 {* ~) P  G private void init(BufferedReader in) {& M- y( p+ Z& |2 |8 A$ s: `4 a  U
  try {- B+ a7 s+ e/ F) g' v- a
   String str = in.readLine();
+ p8 [4 U7 S; H/ T' p6 R0 U   if (!str.equals("b2")) {  j4 S/ @  W/ O) c0 d  K% `
    throw new UnsupportedEncodingException(
$ f! e" u* j% c      "File is not in TXT ascii format");
' Z# q7 b( V$ Z  R   }+ E3 H' x7 o5 ?6 }/ @7 M
   str = in.readLine();
! Q$ V. X- D: A/ S& T4 w, ^  H5 S6 O  o   String tem[] = str.split("[\\t\\s]+");
& X+ _! ~4 v: X6 l   xSize = Integer.valueOf(tem[0]).intValue();
9 f$ ?, ]1 B" i! l* j+ h   ySize = Integer.valueOf(tem[1]).intValue();
; K& x: y/ O# J7 z; f& }   matrix = new String[xSize][ySize];
4 b  |; R( ~. b8 r# f0 r   int i = 0;& ^3 d! ?3 \* Q: i2 G( a
   str = "";
3 M/ W. k) Z; `5 a+ l   String line = in.readLine();
! T/ U, @; }- V; b   while (line != null) {" S& M$ y0 t; c7 H5 m4 l
    String temp[] = line.split("[\\t\\s]+");: |+ r1 q5 j/ h, t' I0 ]" z6 t3 d
    line = in.readLine();
2 g- @: r$ C) I) j    for (int j = 0; j < ySize; j++) {1 G" E- [$ ^: R& i
     matrix[i][j] = temp[j];, g' d0 Y6 A- Y
    }
. O/ g! V( {. l+ t; X    i++;( f5 E8 N4 H7 f- q$ I% l& z
   }6 h4 T& z) Q/ P: _' \
   in.close();
9 a: |1 S6 U" S, y7 j9 m  } catch (IOException ex) {
! T: V- u6 R6 @+ }# k7 ]. w) n( K/ Y   System.out.println("Error Reading file");
; x7 w" `) ?/ o5 M- r2 _   ex.printStackTrace();- w/ F* w. g( S/ K- d7 d
   System.exit(0);1 c, F/ k  a* e% G( X
  }9 p$ J5 D7 ]: V. R
}; {* q1 r& |& r* \% O
public String[][] getMatrix() {* p4 D$ ]- h9 M# z$ [
  return matrix;
7 B) Z0 b8 u4 V. F }
) Z5 c' u4 ^& m) J' n}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-24 23:56 , Processed in 0.022372 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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