设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6086|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;8 ?5 l9 i7 D4 T; w3 [% F& c- O
import java.io.BufferedReader;
7 G. Q! s8 }/ f0 U- Gimport java.io.FileInputStream;( v3 h' ~$ k: ~( V: @2 U
import java.io.FileNotFoundException;
& s! L; f1 _) |: B$ T0 z; rimport java.io.IOException;; k+ V" I2 Q2 g
import java.io.InputStreamReader;
! j, t/ Y' q  e' s0 Kimport java.io.UnsupportedEncodingException;
" r1 P, x- b; ^+ O5 J' N% aimport java.util.StringTokenizer;
( ]" c0 H& r3 a: E5 Mpublic class TXTReader {9 b3 E: ^7 m0 C$ e
protected String matrix[][];
& D* A4 Q+ N8 u8 W# a5 X0 }; L protected int xSize;
3 ^* Q0 e7 g, k; J' L: b protected int ySize;
5 o" `" g# C& j public TXTReader(String sugarFile) {
$ Z" F1 z9 o' |1 r  java.io.InputStream stream = null;8 I' B, m4 U6 ^0 `5 s! I
  try {, v, ?5 p! P, c$ I
   stream = new FileInputStream(sugarFile);
2 s+ f! w" \# F: f! i: U7 T  } catch (FileNotFoundException e) {
, s3 w* S- M  {( u1 a  C+ @   e.printStackTrace();
5 i) {# S5 o! \( G6 }( Y  }
# c3 \$ I1 h% N, f: F  BufferedReader in = new BufferedReader(new InputStreamReader(stream));6 e" U+ j% z% m& M1 m
  init(in);8 k! U: N0 h. _0 W
}
/ a) T% I4 K( j private void init(BufferedReader in) {! S: D' S0 t; P+ B
  try {* X" h. F5 Z5 {" A& i1 T( E
   String str = in.readLine();: b1 e- C( a" a+ p
   if (!str.equals("b2")) {
6 e& Q9 h2 g' |9 T' r" v    throw new UnsupportedEncodingException(
! L$ x$ I/ R) A5 x' u: y! r5 i      "File is not in TXT ascii format");
+ D* J# ]9 U/ Z2 F3 \' Y   }
! _9 i1 ]! U% ?4 I  p   str = in.readLine();
2 B6 a0 I; |( `" t2 v( i6 a: ~   String tem[] = str.split("[\\t\\s]+");
- z8 w% M7 p& t' [   xSize = Integer.valueOf(tem[0]).intValue();
! n5 C. u, C* J9 e3 I' _1 G1 ^   ySize = Integer.valueOf(tem[1]).intValue();" B1 K1 X( `+ a$ v5 ?/ P; Y
   matrix = new String[xSize][ySize];
- T0 j- Y' f6 \5 e4 G: P, }   int i = 0;
: E7 A1 a- I: l& V5 Q) {   str = "";$ O6 T" e" z- P& U6 c& y$ f
   String line = in.readLine();/ k5 \! i2 P! ?" w
   while (line != null) {0 R) l1 w8 F. J" ]0 {* Z: \8 q1 {
    String temp[] = line.split("[\\t\\s]+");3 m8 C" I" a8 K2 T/ w7 Q# ~- p
    line = in.readLine();
1 x- j; P7 B! g' U    for (int j = 0; j < ySize; j++) {4 U- i8 e) ?% s4 c0 B
     matrix[i][j] = temp[j];- ]4 i7 ]' H# h; W% M
    }
: J& s; f% S, P% i7 N    i++;
& e3 N6 t+ W% k% g4 T( z' v* a: u   }; o7 E4 ]' m2 x/ C' \2 O+ y; w
   in.close();" }6 n% Z" ?! {$ V$ i
  } catch (IOException ex) {
* Q8 G! K. e1 x( u   System.out.println("Error Reading file");' E$ |! L  @3 ~! @. n1 y$ z! R" j
   ex.printStackTrace();! M/ o+ \( e# f- Q2 K( M8 e
   System.exit(0);/ I' X  U9 Q! F# q
  }
. f4 V2 M, J& D' x }; U/ V+ Q7 v9 W9 R- `
public String[][] getMatrix() {4 Y) W6 P2 a% L. I4 k; i* I# e
  return matrix;! P3 G* T( m# t
}* N/ n. l8 ?/ v' B" E7 a
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-10 19:28 , Processed in 0.015605 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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