设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8205|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;7 |2 k8 _) I2 H* c. l5 ^) {
import java.io.BufferedReader;
+ `1 B! b! Y& n' ]2 ~import java.io.FileInputStream;6 ?( P# D* i* ^4 l6 E8 v
import java.io.FileNotFoundException;: y2 p+ t. y" U! Z) x' f$ ~5 O$ a0 \
import java.io.IOException;2 p9 Q/ b6 q0 \6 K0 ~
import java.io.InputStreamReader;
/ \2 K5 m0 D5 q" f" k, T$ yimport java.io.UnsupportedEncodingException;
. J8 N# g) T' S8 bimport java.util.StringTokenizer;4 F4 c& B$ K, ]/ K
public class TXTReader {/ S  A, [! j2 l  U9 O1 {8 n1 h# L5 m4 \* G
protected String matrix[][];$ C) J# G, r3 I! d
protected int xSize;
4 N( p# I  @1 \# X8 P, d9 E! d( Z protected int ySize;9 d+ w" O0 w/ v0 v6 E& ]1 B
public TXTReader(String sugarFile) {- g" c0 _/ u, e$ U5 d0 Y0 y
  java.io.InputStream stream = null;$ q& R8 A+ d1 I" s. |. B' X- @
  try {; v8 U3 r  R, s; L
   stream = new FileInputStream(sugarFile);
& r3 t2 A; j% N! ?% \! R! k  } catch (FileNotFoundException e) {
9 W9 j! V& n4 [) ?5 G/ y; ?( B   e.printStackTrace();/ y. F& `: ]5 B0 Z, ^
  }
' N. R3 H& d+ q1 U  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
' W- F/ ?8 A6 |1 I5 a6 m5 H  init(in);/ N: R2 C  x/ I# `! k  Q
}- o" L" p* D& T+ U/ A& N
private void init(BufferedReader in) {
' F2 B" R6 u. r8 t  try {
* F' Z0 N% k0 s; c- n" q3 \   String str = in.readLine();$ a9 {; w7 n  c$ U- b' m
   if (!str.equals("b2")) {) b5 {2 h; Q) O/ |( e2 B1 k
    throw new UnsupportedEncodingException(
4 n# L- }0 N$ Z0 n# u+ g& o, e      "File is not in TXT ascii format");( o/ B3 s( Z7 A3 F
   }
5 L% A' ^) d. v) d, H7 m1 h   str = in.readLine();
4 V( c$ E) E& ?) [   String tem[] = str.split("[\\t\\s]+");, T+ X. y9 ]4 H$ p
   xSize = Integer.valueOf(tem[0]).intValue();
: N; }7 E5 z+ Y! w7 J6 j) s   ySize = Integer.valueOf(tem[1]).intValue();
7 B9 a5 o* D$ p8 z* w1 {7 P   matrix = new String[xSize][ySize];5 i0 \6 c* B: h/ P
   int i = 0;
* m3 C  r& z% G' q( A7 z' e' f% [   str = "";
- F% U4 T0 q; }0 }" @   String line = in.readLine();
$ B; N3 _: \. q   while (line != null) {
6 j( ?: g4 |/ T4 J7 M+ g    String temp[] = line.split("[\\t\\s]+");6 S, f4 ]4 }3 Z! m; S1 Y9 s1 G
    line = in.readLine();7 ?  J9 G# T+ k5 c6 z" X" e2 ~/ A) n4 `
    for (int j = 0; j < ySize; j++) {
) C. I  q# q/ U0 [% b     matrix[i][j] = temp[j];
1 J7 p& K3 j: z$ A3 S! y. p    }
3 [. [9 q/ k, }6 i& ^$ [    i++;9 d+ k- R5 C7 m" \% J
   }: W9 l3 a2 H8 q9 y/ q0 w3 D
   in.close();% `& _8 ^0 }2 I* [4 S7 a
  } catch (IOException ex) {: y! L' z+ a6 r) o- G; b
   System.out.println("Error Reading file");8 M# j4 Z( c6 i4 _) r
   ex.printStackTrace();
8 V6 S- ]0 P  \7 b% I( F  q0 m. t   System.exit(0);
* h5 ~& T) x5 G% m- Z  z1 f  }6 L5 R. ^4 D9 d* c0 }
}  _; ?  p0 f! X4 t) c
public String[][] getMatrix() {5 t+ Q* K. p7 h* h7 L6 q
  return matrix;) `% [$ T) G" e& t" ]: Z
}
5 C, R* i5 y0 u- |) U2 K7 l8 t}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-8 03:29 , Processed in 0.017652 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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