设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6526|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
1 ~% W8 w, N) \( W  y7 @import java.io.BufferedReader;
1 s" G1 D0 M* \8 Fimport java.io.FileInputStream;
# ]4 `5 i4 {& S$ |import java.io.FileNotFoundException;2 f  o8 O+ x& _+ w8 ?/ r& H
import java.io.IOException;/ @9 c, p5 ]( {( _2 x
import java.io.InputStreamReader;
/ J6 G2 ?2 d( E! R! q0 a& [import java.io.UnsupportedEncodingException;
9 F5 Z0 i8 {3 Limport java.util.StringTokenizer;# f! H' ]2 ~5 k" M, `* r8 J
public class TXTReader {
% B8 e( A0 K0 ]/ C) ^( U7 U* P+ \ protected String matrix[][];
# j0 V2 z0 |5 E& F protected int xSize;
; Y) K( V$ {2 Z9 j, S+ K' j protected int ySize;
, y/ k: x7 n0 { public TXTReader(String sugarFile) {
4 T  q; b* b2 k0 f  java.io.InputStream stream = null;+ G+ w  Y& _1 N
  try {
, x' x, Q$ }6 Q5 M* ]: n0 T3 m, W2 E. _   stream = new FileInputStream(sugarFile);
: K  V/ _: k1 R- |  } catch (FileNotFoundException e) {- v. U7 s! L7 C
   e.printStackTrace();
; ?6 Z  J, p: F, A* ~  }9 [  B7 a5 g; K
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
; {  b  g2 u" e$ b: d* j% d  init(in);
0 y9 B- T9 K4 Q' J( G }
5 ]8 E+ c8 u- O* B private void init(BufferedReader in) {
7 T& g; I6 D. S' S+ j% C! R& J  try {" c; q3 X% g6 ]  _7 X
   String str = in.readLine();$ B0 `1 U* e! [
   if (!str.equals("b2")) {7 N* \2 L: U- A! Y5 ]1 s
    throw new UnsupportedEncodingException(
$ _" E5 T, P+ S' N6 e( m      "File is not in TXT ascii format");( ~9 y; K% f0 q" p0 |$ S0 M
   }8 Y; G6 u7 l) i) a5 O5 m; w$ h
   str = in.readLine();
3 N9 |; V& F1 ?: L6 q' G   String tem[] = str.split("[\\t\\s]+");* a  A2 Q0 ?: z: |  H: ?
   xSize = Integer.valueOf(tem[0]).intValue();
! }$ m' t% f2 T. ?! v1 N   ySize = Integer.valueOf(tem[1]).intValue();
8 a, ~- t7 D1 R6 A: e' R% `   matrix = new String[xSize][ySize];
' ~* z. V/ }2 r( b& E5 ?: F   int i = 0;
3 [: h9 C( c2 \& w: {   str = "";5 h5 \# p3 M- @# w4 [2 l
   String line = in.readLine();$ j+ u' T- g7 ?& Z8 K, R. t
   while (line != null) {
  q% M% H! o2 L1 Z    String temp[] = line.split("[\\t\\s]+");
: g* h% U* h! M2 X6 p" D( U9 ~    line = in.readLine();
* e. C& z# x6 V* X1 Q    for (int j = 0; j < ySize; j++) {' r0 N5 I$ A; {7 [& i# R
     matrix[i][j] = temp[j];
0 A: {8 ]; U/ j. x& \' M; {    }
6 U+ _2 Y) U  M0 v* B' a/ L    i++;
  f7 f; w' F0 C% n+ F6 ~1 V   }) S7 v; d+ U7 \+ `
   in.close();
* w; s0 ?& z+ g( j7 m  G/ h$ f& F  } catch (IOException ex) {+ K# T8 s" I/ z9 H% l
   System.out.println("Error Reading file");) o, m; s( r4 \, \3 J5 E& V: W
   ex.printStackTrace();6 t  M0 w. D# |3 i( o
   System.exit(0);7 @0 U: Q4 I/ s: F
  }
8 L6 r  t  M' @  }. B6 Z }; u# D% Q; [2 w4 Y4 @" c" o
public String[][] getMatrix() {6 t7 Q7 y( v- b
  return matrix;
- T% Q0 n7 R" v }- x' R+ m2 t4 d# g$ |
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-16 02:58 , Processed in 0.054777 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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