设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6973|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;% S6 V1 ~% w! z7 B* h% P
import java.io.BufferedReader;/ t: @5 |, L- x3 M. d4 ?
import java.io.FileInputStream;+ K$ t: K0 R% l1 f- J, d
import java.io.FileNotFoundException;
7 R+ D. |! y) v% O6 |import java.io.IOException;: }7 v$ y% F) |/ ?- ?
import java.io.InputStreamReader;/ l: D9 y, b6 b6 z3 l
import java.io.UnsupportedEncodingException;
/ i/ z9 I  Z- a7 k' O. K# N5 Wimport java.util.StringTokenizer;2 R. ]$ L. ?/ \- j* S' c2 f
public class TXTReader {7 {' z/ H4 e; a7 l8 ?& j  _
protected String matrix[][];
( |. x. G. W! x) ?' ^! f) }% C9 }4 e) m protected int xSize;* z7 Y0 n" D% o- p9 R
protected int ySize;, ]3 m% D& I2 c- j  E4 b
public TXTReader(String sugarFile) {
0 g7 p" U% Y& g# s* i7 r  java.io.InputStream stream = null;
* P$ A- z- T7 d8 I' Q+ S! n  try {
) E# B4 U+ i+ q! A* [   stream = new FileInputStream(sugarFile);- F5 J1 ~4 h% F* w. C3 p% [
  } catch (FileNotFoundException e) {. n! p% y0 M( `0 N: D
   e.printStackTrace();0 h5 S  N! }6 U+ A! k) |- y
  }
5 d% ]  p6 k( f( \7 n  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
- T* \# n# E* S, g  init(in);
7 a" V  m$ \/ q }4 m7 c  x: O% f6 j! q/ E% s! T" R9 D- ]
private void init(BufferedReader in) {$ F9 j9 `  U; O5 w
  try {
+ Y1 X: K3 \4 S   String str = in.readLine();
* x4 P. J4 ?' t$ X   if (!str.equals("b2")) {" X4 ^) y5 n  H3 S' N6 k
    throw new UnsupportedEncodingException(0 \. s2 }4 Y& x0 z; Z
      "File is not in TXT ascii format");
, V" i% |+ h4 r  Q7 L, {+ `   }3 ?. ?. K0 g+ s5 e) s
   str = in.readLine();3 D4 D: A$ B. d# I. g
   String tem[] = str.split("[\\t\\s]+");
( g# Q0 i; K1 U$ p3 m   xSize = Integer.valueOf(tem[0]).intValue();
# \5 t! Y! ^# h6 f" o/ Q: E   ySize = Integer.valueOf(tem[1]).intValue();
2 q& J" N# \( S! e. @+ N   matrix = new String[xSize][ySize];6 D, p( n" p( p. T! s
   int i = 0;
# ?4 n/ O% k. h   str = "";
6 [/ Q  V, @+ e& J' F& M   String line = in.readLine();
% \) X! E% p. q5 |  Y% [3 k# x   while (line != null) {
2 C3 d5 b! J9 ]1 ]1 P+ K# T* F    String temp[] = line.split("[\\t\\s]+");
1 X7 h- u$ m8 ]    line = in.readLine();% q" \9 U/ G. U- u6 y0 w
    for (int j = 0; j < ySize; j++) {/ z+ C" K& N7 s$ v$ i  x
     matrix[i][j] = temp[j];
$ _6 ]. L; t0 C: ]& f+ G2 T& L    }% x0 M# ^. _& w7 ?9 X  E
    i++;. h$ @- w( t, O4 |; {3 q
   }3 _3 s. G2 f$ q/ N. Q9 W2 k9 [: O
   in.close();
# p4 [2 ~6 c/ o. }9 ~: g- {  } catch (IOException ex) {
. W' m& ^" U) S& `   System.out.println("Error Reading file");
1 q/ a5 k3 t8 H5 \   ex.printStackTrace();# R, W% r; B& v( {  p
   System.exit(0);, k+ y( h0 _& T0 a
  }0 A# k& s# y4 x2 y
}& P- d0 K. }2 s( C) s' r" w( q
public String[][] getMatrix() {8 F4 x) o- v; A0 R: Q) z$ o. u" a
  return matrix;
) o0 G7 j# E1 q0 t }
* K* }% w! j8 g}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-13 10:19 , Processed in 0.020136 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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