设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5723|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;- h% j' P( U! N+ n  `( m
import java.io.BufferedReader;1 }  w' o$ }2 H9 [2 t1 G* L
import java.io.FileInputStream;
( F# J2 `0 Y$ F/ ~import java.io.FileNotFoundException;7 ]% N  g+ a$ m9 D8 K8 f
import java.io.IOException;! o4 K- ~# h6 j' L' {" H
import java.io.InputStreamReader;
4 P- Z! u9 H7 z8 N% oimport java.io.UnsupportedEncodingException;& W1 N  ?" Z( y- }, T  T
import java.util.StringTokenizer;6 a: _% o+ H( R
public class TXTReader {
  X( \' l/ Q+ x protected String matrix[][];
, M) R3 N) R7 T& L3 u, O" r  } protected int xSize;" d8 f+ e" t1 s
protected int ySize;. J5 t) A- Z4 g% m. X) H
public TXTReader(String sugarFile) {
4 p8 ?3 c) Y" W2 M  U; z  java.io.InputStream stream = null;
9 b1 a, i3 q# r9 x  try {7 H3 R( K8 @& d" `, v  T1 `) E
   stream = new FileInputStream(sugarFile);
; G, r1 @3 [- U& {  } catch (FileNotFoundException e) {
  @4 s' X$ g, v7 T   e.printStackTrace();
+ A+ y; _- `7 y. m- C) k& Z+ ~  z  }1 m9 W3 Q* b! j& z
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));% o! e/ q5 r& y% }9 i
  init(in);: B6 ]1 ?1 x, V
}: j* x! J2 v- ]
private void init(BufferedReader in) {6 ?; N3 ^) _$ M6 T; X3 j/ c2 B
  try {
6 y6 |2 s. P% R" V+ _! ~' |, o   String str = in.readLine();
2 ?4 o* R$ {2 U. S+ C1 Q3 U   if (!str.equals("b2")) {3 o7 a/ I4 o2 C& X- T* n8 \
    throw new UnsupportedEncodingException(6 J/ ]/ m! c, X5 {
      "File is not in TXT ascii format");- w- o& c1 ]; [& e4 z# E1 v
   }
  L1 n2 W: l4 _   str = in.readLine();
) `! ^3 K( O! P3 q7 U& ~   String tem[] = str.split("[\\t\\s]+");
% w& x& i5 G0 R- d   xSize = Integer.valueOf(tem[0]).intValue();8 O3 H' E. x- a& S6 x! I
   ySize = Integer.valueOf(tem[1]).intValue();4 B/ n4 {: B% Q) O0 b  F0 r0 H
   matrix = new String[xSize][ySize];
/ M* o4 F( M+ i& @9 y   int i = 0;8 d! F9 a7 \6 j# P7 D
   str = "";
1 z9 ^4 R1 k+ J4 ]7 y8 E   String line = in.readLine();5 n! }6 Z- b8 T4 B
   while (line != null) {3 K. f3 Y" v, o* ?( A7 ~
    String temp[] = line.split("[\\t\\s]+");
# L: R4 c6 B# {0 t4 ~    line = in.readLine();
% k- W  z8 P" L1 l1 }' E. s    for (int j = 0; j < ySize; j++) {# P0 E9 y7 t, A& ]% `
     matrix[i][j] = temp[j];* E5 Z) ~9 C* S6 c
    }
$ K1 e% `0 ]8 Y9 t/ ^! }) U. ~    i++;
7 x$ U) G: e6 J$ s& X" q   }3 }$ O' E8 u1 b9 Y5 ?
   in.close();
* P! g. `# s9 d, A6 B  } catch (IOException ex) {
  w; e5 R4 j" L+ c   System.out.println("Error Reading file");% p% l: a9 L5 f4 L  l% S  w7 L
   ex.printStackTrace();  i8 m8 I1 b& G& V- I: ]( O8 ^4 S
   System.exit(0);
$ ?  H( i1 a4 X, N* N  }. q3 T( @& S. B+ S, f1 k
}: V  W# S( I% I
public String[][] getMatrix() {7 [7 |: u- E( P; _) j, {; U. @
  return matrix;
. w4 S1 T* b8 A8 x2 z& ?( \0 y. I }9 P* C0 v0 T; ?; \7 i. H4 K5 w
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-21 08:42 , Processed in 0.018418 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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