设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10207|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
, r) W2 ~2 I* d/ jimport java.io.BufferedReader;
* y; l% Y5 U# C- y! W( @: W, Simport java.io.FileInputStream;* }4 e8 \8 d1 Q  ~- l
import java.io.FileNotFoundException;1 @+ k  E1 R( I8 b( D$ B
import java.io.IOException;2 ?& i; @  c2 q* Q
import java.io.InputStreamReader;
8 g: }: J# |- M! B% Q& \- Pimport java.io.UnsupportedEncodingException;: i6 Y8 `" C3 s# p0 P
import java.util.StringTokenizer;, z8 B5 V* _! f+ L' b
public class TXTReader {
9 S( C2 f. @" t2 v* x$ H4 q! l( J, E protected String matrix[][];
- f, p4 K+ R3 e! s protected int xSize;7 i2 E6 k3 _$ e# H6 C; i
protected int ySize;. Z! \& [& q6 b
public TXTReader(String sugarFile) {9 ?) k1 {% X9 l2 m" r0 G
  java.io.InputStream stream = null;7 J* N% [' M: ]! h+ x3 v
  try {% k) h7 {8 \" ?. R, U0 h; T
   stream = new FileInputStream(sugarFile);9 P( l7 _  T, @
  } catch (FileNotFoundException e) {
4 D" b- H. z8 T5 Q! I   e.printStackTrace();
7 b% W7 G1 n# V% i) ^% `& s7 D  }8 u7 T- j4 \. Z$ `  D6 B% }
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
4 J! j9 |, E7 m/ t4 U  init(in);; Y# C% w* @7 A! A$ U/ [. N
}5 F' P, I) U( ?3 r2 L- C% t8 V
private void init(BufferedReader in) {
& O# ]0 s8 J3 Z: T, E$ [  try {* p) I3 o) b& N, b2 p. f( r4 m
   String str = in.readLine();8 H2 M5 N' m. }8 a0 H3 y
   if (!str.equals("b2")) {, w  E' J  |9 p7 q$ i+ C
    throw new UnsupportedEncodingException(# J/ {4 l# ^3 M% I  k9 i( N2 O' G, G
      "File is not in TXT ascii format");: k5 d$ C2 }& H1 s1 i
   }
1 R7 O/ e6 i% ]" h3 L4 I/ P   str = in.readLine();
) B6 o5 B/ M& Z! f% {7 v   String tem[] = str.split("[\\t\\s]+");5 B; D. I' D+ q" C1 W3 Y4 _" r
   xSize = Integer.valueOf(tem[0]).intValue();& X! y9 O* U7 ?9 s1 C" D/ z) I! d. ^
   ySize = Integer.valueOf(tem[1]).intValue();1 E; w" A2 I* f. D4 y6 a1 N
   matrix = new String[xSize][ySize];2 I! X% A4 Y& c7 X1 w9 C  a$ n/ w
   int i = 0;
  o5 w- [; D" l0 U6 v* [; ?   str = "";
( v- k7 S$ [% _3 e' i   String line = in.readLine();6 K4 i  a7 T7 }' S# t4 i8 H/ P
   while (line != null) {
, M5 w) ~+ H2 ?3 I    String temp[] = line.split("[\\t\\s]+");
' h% G7 ~$ T! y4 P    line = in.readLine();- \! n/ {7 e' r" {- I9 s
    for (int j = 0; j < ySize; j++) {
, f$ r6 J# S! j" G% e* u     matrix[i][j] = temp[j];% E7 l, U9 v# u7 q0 W4 s
    }; U+ q3 [+ M2 F0 [* T
    i++;. ?! ^" i, b8 J  A9 y) \3 I
   }5 J9 ^5 U) n. h
   in.close();* Q4 l3 q9 Z5 l" }' u- `8 T0 q
  } catch (IOException ex) {
% z' p) Q1 {) h& C3 \   System.out.println("Error Reading file");3 m9 Y9 Y9 D9 y! X' [. A
   ex.printStackTrace();8 |2 h. k6 x5 k8 Q
   System.exit(0);/ |4 T) ~9 s  h7 d# O: L/ L
  }
; D  u4 F0 g) e& i# N, G) }$ Y }( r5 C8 Q2 L- p  G! y, l
public String[][] getMatrix() {/ u. C$ M) w, t0 W
  return matrix;
7 Y; c& r0 [3 _' l, X6 ^# D# g7 B }
5 Y9 X' S1 {8 ]9 G# Y7 `, x6 N}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-28 15:47 , Processed in 0.018451 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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