设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7367|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
( m& @! s4 [/ Q. W* n* s1 Aimport java.io.BufferedReader;5 B! K1 G- f  e
import java.io.FileInputStream;
# q' a) b4 o' \import java.io.FileNotFoundException;
+ r% T* T0 G: ^+ j! b* himport java.io.IOException;
; K1 C- L. h8 ~& v' I3 h8 f& o4 V5 l/ h7 [import java.io.InputStreamReader;# B" x8 K- H; Z1 l! d% d
import java.io.UnsupportedEncodingException;2 P: n, t# l7 o0 `) i
import java.util.StringTokenizer;
! T6 j# R( o2 O8 D8 \public class TXTReader {
4 b; b( _3 \4 Q protected String matrix[][];0 H' S) h/ e! @" h% P% z! k
protected int xSize;, d, l7 a' f2 Y8 p/ j
protected int ySize;
( k9 w7 O  b' h( ~8 i. r) I public TXTReader(String sugarFile) {" i4 S/ L! V! h2 R' k, d- o) \
  java.io.InputStream stream = null;
# E! I5 F! Q3 |6 Q8 P  try {+ h+ `. G' m  t7 y+ i! E) z- r  |7 J
   stream = new FileInputStream(sugarFile);
; @- L! e8 Q8 X0 B* {  } catch (FileNotFoundException e) {+ f# n4 V3 u& n9 G
   e.printStackTrace();* G; z" {: i9 ?+ \0 e
  }1 g  t% Y3 K* X: L2 m5 [$ s: C4 w
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
) L( l. a$ w9 v/ ~! E! u& h' o  init(in);
$ J  a5 z  A- h; {  H/ x }% d, J# `  C- ^' n: `. r9 ]
private void init(BufferedReader in) {
7 K4 a9 p4 k- E" h7 u" ^' J& r  try {
& ?( x' G' p  v' v8 f   String str = in.readLine();( T" V  s5 }- q
   if (!str.equals("b2")) {* C  j! `3 L( q
    throw new UnsupportedEncodingException(1 W1 k7 }# `4 G! h  S
      "File is not in TXT ascii format");0 b6 @& o! L# L! E) n! E0 Z
   }
  b4 d7 k2 g) g7 |. q6 {+ K   str = in.readLine();
. o& R$ w# }/ o5 X   String tem[] = str.split("[\\t\\s]+");
' s$ d5 [. s4 G$ h   xSize = Integer.valueOf(tem[0]).intValue();
) d, e1 X: P, E4 x- }# y5 Y   ySize = Integer.valueOf(tem[1]).intValue();3 q- l, f& ~5 w) W& g& a
   matrix = new String[xSize][ySize];
$ t' z, F9 a- z8 z' @: M  a   int i = 0;
" _; \( R/ U/ W2 U, b   str = "";% I3 d  J/ v+ S* M
   String line = in.readLine();; H/ T2 d7 G4 ?$ C; J4 T3 e
   while (line != null) {
- G! a8 g8 Z* ?6 T' f0 g! L    String temp[] = line.split("[\\t\\s]+");
7 d5 Q8 i' W) D0 q3 W6 g- L( G    line = in.readLine();
; M5 D/ x; |! i    for (int j = 0; j < ySize; j++) {
) e: V/ a" z. g: I     matrix[i][j] = temp[j];% H# R6 f$ Q* z  R6 l
    }
! V: O4 h+ J7 H% w  E( o    i++;
  {/ b3 `2 T8 `- ^. ?) ^   }3 S5 d1 n1 H. |3 H
   in.close();
: p9 |' t1 X2 Q; m+ O  } catch (IOException ex) {
/ l; j$ g- ^5 F* L1 o$ m   System.out.println("Error Reading file");
$ o1 u/ q$ Q5 s3 `5 g   ex.printStackTrace();4 A' Q2 G+ A$ t
   System.exit(0);7 K( f% |2 M6 ?' b# {4 ^" I$ [7 c
  }
) S0 |1 e7 I5 T6 u5 H }$ C$ F0 {$ \3 E6 b, l  y! ^
public String[][] getMatrix() {
2 R( M' H  e: [  return matrix;
  N& u( k5 u8 L; m. \* a- Y }# k  R& Z6 W% t) ]* e% T
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-10 21:57 , Processed in 0.018306 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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