设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9636|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;7 m/ |; g& C3 g1 G5 ~
import java.io.BufferedReader;3 k7 b& P2 E4 W2 ?" `) n
import java.io.FileInputStream;6 c* ?" v  t( |  z7 i" s
import java.io.FileNotFoundException;; ^2 m) A; i) S* W! b  w! u
import java.io.IOException;8 F' N  I- u! z1 y; ?6 L7 f
import java.io.InputStreamReader;
+ y5 ~9 A: F# M+ bimport java.io.UnsupportedEncodingException;
# Y7 K: M6 I6 O5 t- `3 gimport java.util.StringTokenizer;' Y! m, g  s% N) q5 r& G; d
public class TXTReader {
. [8 D3 v$ {; K/ A# ^9 g6 V, H protected String matrix[][];
% C0 K2 d6 E9 u1 n6 p! i protected int xSize;! u+ \1 O' F: N% [
protected int ySize;" S8 f- B' _* F5 w! v8 P
public TXTReader(String sugarFile) {1 @- y) I6 L5 H+ m  K9 z/ G* }
  java.io.InputStream stream = null;
# {* b/ E# |5 o  try {1 h, j6 Q( p: d' b# i
   stream = new FileInputStream(sugarFile);
6 Y# B) Z4 Q0 g" N- h9 ]4 ?  } catch (FileNotFoundException e) {
) e+ c/ d7 V( s) k0 Q   e.printStackTrace();
& k" a6 W% U  ~. C$ I  }) M( \2 Q* y0 D: V0 H: `9 U
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
7 b1 y4 V/ U& w5 R' ~" y  init(in);8 p7 @$ S( w/ X  e/ f7 V
}
2 V, K$ Z  N+ s' G private void init(BufferedReader in) {4 g# V& s) v* s  ^+ s
  try {
( U/ q" ^9 W; \* _   String str = in.readLine();
) L6 ]3 M& Z  J   if (!str.equals("b2")) {. N2 K% K! ?7 d+ Y
    throw new UnsupportedEncodingException(
3 r6 [; f, D3 {* R8 {2 z      "File is not in TXT ascii format");
% m: ]0 `& n  y# F, K. R   }) a0 ]0 w. n# ~6 T& k
   str = in.readLine();
% M3 L! c* T2 ~7 _   String tem[] = str.split("[\\t\\s]+");6 }- }1 |' k! A; {! N; e
   xSize = Integer.valueOf(tem[0]).intValue();5 C" h( s4 X7 v9 @
   ySize = Integer.valueOf(tem[1]).intValue();
$ \2 T* y: S- _& V7 ^7 i+ ?   matrix = new String[xSize][ySize];. J  f: T% d1 M+ T+ z" l- |
   int i = 0;9 l! H* ^( F: c) _
   str = "";  E- c2 m8 R0 a0 _9 n! L) o. n( b
   String line = in.readLine();
8 M& P# F% t$ b7 s0 \) y4 b   while (line != null) {, C7 [6 V2 \/ [! x/ v* K" d
    String temp[] = line.split("[\\t\\s]+");
6 J! j; Q7 U, @    line = in.readLine();
8 `2 Z+ z$ Z# l: c* U; r    for (int j = 0; j < ySize; j++) {/ W; O, x! ~+ K* G1 O+ \/ c
     matrix[i][j] = temp[j];
6 x% _) e, f4 L( y6 ?' y" m1 G$ _    }
/ N* c. T) k$ L    i++;
/ p# ]( {  ]2 a* m   }/ f3 n4 j( @2 l% O
   in.close();: i, w+ T* M, ?7 y: e. d
  } catch (IOException ex) {: V6 G- z3 X; ~& g" k! R& N
   System.out.println("Error Reading file");
$ w- K1 y/ |! M9 f4 [   ex.printStackTrace();
; L3 m# w0 e0 ^; E   System.exit(0);
7 u# x5 Q2 [5 Q/ F( R* K8 [+ o6 v  }
* A3 z( j8 E+ ^1 D6 T }
+ ]3 ^' K/ n) g' s public String[][] getMatrix() {: f9 I) K1 i) }8 }% k0 [
  return matrix;& N  p0 O, J6 T0 A  a4 I9 C5 `* p
}
3 b) w- z) A8 V6 \0 _. R8 o4 d9 G}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-29 00:28 , Processed in 0.014152 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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