设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10439|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;8 Q  f3 X9 O$ m+ n
import java.io.BufferedReader;/ m: ?  `  I3 M, d8 A7 h- s) S
import java.io.FileInputStream;+ }8 D& [' Y" O
import java.io.FileNotFoundException;6 ]/ U/ D' y' g2 v: s
import java.io.IOException;
8 Q  ~4 G( p0 v6 z/ ^import java.io.InputStreamReader;# U/ B' k  G( G
import java.io.UnsupportedEncodingException;; Q# c/ _& n5 ~! L
import java.util.StringTokenizer;
) S1 {" H' O! h+ e9 U- x2 Y2 dpublic class TXTReader {
" q( P+ A) j9 Q" X1 K/ r; P protected String matrix[][];, Z- B5 s/ M( R' G# c
protected int xSize;
; ^5 T: k. N/ M5 H. d. S8 M$ T1 `3 L protected int ySize;% i6 z/ p) c% `0 B4 ]9 H
public TXTReader(String sugarFile) {
1 _/ t6 G* S2 Z3 h7 s9 ?  java.io.InputStream stream = null;
+ [& [; R* L: x& Y  try {
6 X9 p* H; ^$ s   stream = new FileInputStream(sugarFile);9 [5 `* }3 T3 [2 u  c
  } catch (FileNotFoundException e) {
/ r. v: Q* F2 `   e.printStackTrace();
5 Z, O' \# c1 q6 [- B  }- G- C$ K- C  I; S# d+ N
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));* G' K9 b& l7 \5 n
  init(in);7 r- Z- L1 ^# f
}% z5 _) U- M, ^. c: `! J; [
private void init(BufferedReader in) {3 S0 [4 c" a" C+ G- n
  try {9 l3 b% h# J+ r3 P/ ]' z# i
   String str = in.readLine();
! r6 h) W" V' j$ [$ n, f5 R   if (!str.equals("b2")) {& I9 D% y/ K0 R+ A( B  f( a0 f, c8 K
    throw new UnsupportedEncodingException(6 v8 O# \( g2 v3 B6 b& B) R" c
      "File is not in TXT ascii format");
1 b- g) B- H& c2 A$ B   }
4 ?4 ]4 o& ~) m9 C( X" O   str = in.readLine();4 ]9 ^/ O. C9 ?0 D$ ?5 A" O% W
   String tem[] = str.split("[\\t\\s]+");. Z+ I, F: E2 _% l, U; a
   xSize = Integer.valueOf(tem[0]).intValue();$ z6 n3 X) x) X. |1 E
   ySize = Integer.valueOf(tem[1]).intValue();* q3 K3 y7 |, g: t0 u
   matrix = new String[xSize][ySize];+ s% b6 |) Q: [) z; T% {
   int i = 0;
! }$ E! ?8 T& Z# s: n3 K( h. ^' p   str = "";2 S& T" P0 v! ?5 J* o1 C1 {
   String line = in.readLine();+ E' Y# M2 ]" V! S6 `1 u/ [6 F
   while (line != null) {1 O7 H3 @* V) F
    String temp[] = line.split("[\\t\\s]+");5 o1 t2 Y( N$ ^9 {
    line = in.readLine();
/ I# \3 v; ^$ x# m    for (int j = 0; j < ySize; j++) {. r8 Z" A2 F5 W/ S
     matrix[i][j] = temp[j];
6 V6 V: i* l1 Q# |- G    }; Y& ]' A: o4 b2 Y4 w
    i++;
/ @) J; x5 g- e$ R3 R$ t+ u   }+ C" c$ D2 e, J  |
   in.close();
8 `  V- M) l) u0 r) J  } catch (IOException ex) {( @+ R) I& K* C; b; S. V' U% o  V
   System.out.println("Error Reading file");7 V' v$ A2 j/ Y! ], V! B
   ex.printStackTrace();
6 [4 K! }) y$ E/ |5 c  ]/ t5 {   System.exit(0);
, ~9 h1 c& F: }% s# v7 {$ r) \3 o6 I  }( S9 h7 v& M3 A1 y6 X6 l
}/ ^+ H( \& i1 G
public String[][] getMatrix() {
" O# k" A3 Y) f& Q4 B1 q  return matrix;  D2 ?8 h0 _% i/ F  K
}
# N) z8 C# m5 A& ?, I}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-10 13:36 , Processed in 0.016027 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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