设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7106|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;- ]2 q4 ]0 |! D
import java.io.BufferedReader;
+ F2 b  p+ o1 B- N( v# ]' Jimport java.io.FileInputStream;
7 G  x! c; Z+ Y  S4 x% R4 N. \6 I/ Mimport java.io.FileNotFoundException;+ n" b2 X7 h+ X( n3 R% ^" ?
import java.io.IOException;- ]8 H. f" \0 p. `0 m
import java.io.InputStreamReader;
$ ^% F8 e% t% F8 x' ]. pimport java.io.UnsupportedEncodingException;1 \3 S' y! S) B2 ^# F! u
import java.util.StringTokenizer;6 u, |, K; S8 a
public class TXTReader {3 w, g( r! f0 i* \
protected String matrix[][];
: p. Q0 i1 w4 {5 \0 N% [ protected int xSize;
1 k# j: e; @8 |& ~ protected int ySize;: t+ J! A1 W/ n1 t
public TXTReader(String sugarFile) {
# Y: _4 [$ T( z4 W' }4 e# G  java.io.InputStream stream = null;
+ y2 m0 V" Y. R% H( [% r. m  try {7 f: o5 y5 b6 ^4 O: D4 [/ q
   stream = new FileInputStream(sugarFile);7 @2 Z) ~7 f$ g% Y3 e
  } catch (FileNotFoundException e) {
4 W) `0 ~6 ?2 \. A   e.printStackTrace();
* ]3 h3 }2 x9 u0 c" \$ C  }
" u# k: R0 y! k1 ]3 x8 c* N2 C  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
# n1 t  l0 K4 ^- [4 e7 V. {  init(in);6 B2 B1 ?/ X5 u5 t
}
5 \- |" w7 |* m, K0 L private void init(BufferedReader in) {# z4 l% A- d) q2 o
  try {
; p( a4 `1 f  j5 N& G   String str = in.readLine();- C0 {% c+ I, V- k% U
   if (!str.equals("b2")) {
# C* N$ ?8 _- r( i    throw new UnsupportedEncodingException(
' O' c5 ~: J' n) f7 a      "File is not in TXT ascii format");/ z# b/ R8 J  d& T' Q
   }: G4 B- @' e3 x
   str = in.readLine();
- o8 [$ ~" r4 o* l4 V   String tem[] = str.split("[\\t\\s]+");) H, ?0 H% \+ d( Z8 j! V4 o
   xSize = Integer.valueOf(tem[0]).intValue();
$ `" H8 c, q. i6 o7 r   ySize = Integer.valueOf(tem[1]).intValue();
! N$ w$ u, I$ h* Y" n   matrix = new String[xSize][ySize];, F9 t* G- Y9 N# J/ w+ Q
   int i = 0;
+ \' I4 W7 K$ K! I8 F- [   str = "";
7 O2 B3 l: t* T' `8 [5 e   String line = in.readLine();1 q" W* I2 K3 A# {. S9 \
   while (line != null) {9 l2 ?+ d$ G) \7 _2 b4 a
    String temp[] = line.split("[\\t\\s]+");% \& _0 K& _& K/ X
    line = in.readLine();
, i! l# M9 B$ W; U8 f6 x    for (int j = 0; j < ySize; j++) {" o2 b" o( I, j" v1 S
     matrix[i][j] = temp[j];7 w7 v2 @0 m, e% F2 ^
    }1 W; C1 p! c* p7 g
    i++;% @2 @" K: }$ i  U
   }) A4 z7 P" R4 P! {( p' a, z9 d
   in.close();! B" @. _% u1 W* J
  } catch (IOException ex) {* V' g& D9 T: i
   System.out.println("Error Reading file");( C, B, P; G3 q3 ~- L% n/ y: X4 t
   ex.printStackTrace();
1 b# Z, Y. y2 Z# }   System.exit(0);, S  B% j) z+ P' M. P
  }9 k' v$ V/ ?! Y. N
}
  z- Z6 E" c; ^0 F  d: f public String[][] getMatrix() {' K9 L2 V. ?$ B# Q8 A7 h9 X
  return matrix;$ U- _9 Y# \& Q  I" `7 r+ A1 ?3 U
}- t1 v! v( l1 Q
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-21 13:17 , Processed in 0.016875 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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