设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 3720|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;  H& Z3 y. }: e: Y' [) W' R. o
import java.io.BufferedReader;
6 {1 j) U0 |# Eimport java.io.FileInputStream;# A, k1 a; r3 m, ]" p
import java.io.FileNotFoundException;
: @$ S1 Z! S. u1 Y5 f6 x: Zimport java.io.IOException;
$ E$ k! n& f9 \import java.io.InputStreamReader;3 C* I. q" y* L( U  h
import java.io.UnsupportedEncodingException;
8 Y' o) e6 s4 G. ~- i6 Z. C4 Qimport java.util.StringTokenizer;
! ?  X* t) I3 o- i7 N3 T: Z; w8 _public class TXTReader {
8 Z% ], |8 n* |" C2 H4 o protected String matrix[][];* M, D* n0 _. ]# b+ j7 H
protected int xSize;
; C% c0 W, Y- J5 T protected int ySize;  E, Q. u/ N: Y: ?  [
public TXTReader(String sugarFile) {: M8 ^. j/ |* g. e6 r$ g
  java.io.InputStream stream = null;
! M% |: `- c) J$ r% i! E7 i! W  try {1 K1 O0 w& L3 Q2 z) u8 B
   stream = new FileInputStream(sugarFile);
# X$ K+ }  F' t  } catch (FileNotFoundException e) {
+ y( e2 Q  S6 T& K) t& m   e.printStackTrace();
- I- y" {+ D/ u* p$ [& ?  }
0 Q6 j( ^, s! u( D( q) l  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
5 }" m1 K; i! I7 D+ U. s  init(in);% |0 f8 Q( z/ r
}
/ R" d: g' B3 D  ~. x private void init(BufferedReader in) {7 x0 G/ H7 c: D/ O
  try {! d6 D3 M( g& S2 ]$ d6 s$ {$ S8 i
   String str = in.readLine();
' l5 v8 s$ e! t* M4 I* W   if (!str.equals("b2")) {# E7 d1 k9 z( g( J) k: @
    throw new UnsupportedEncodingException(
: Y0 Y: S9 D- I1 E2 f      "File is not in TXT ascii format");: g1 ]2 O, s" ~( q9 O: z6 v
   }
5 K/ O) I* f" e4 [   str = in.readLine();
1 ^, z4 Z+ W7 n, a* ^7 Q' s- d6 d& K   String tem[] = str.split("[\\t\\s]+");. n8 s4 p! |, A% R2 Z2 t' F( y! P. R
   xSize = Integer.valueOf(tem[0]).intValue();
3 d- L* V8 d+ @4 Q; Y   ySize = Integer.valueOf(tem[1]).intValue();. ]# p! @7 Q; t. A2 x2 V
   matrix = new String[xSize][ySize];' n1 Q$ C, R& t$ |
   int i = 0;4 H3 [+ _2 g0 O6 y! A! x
   str = "";
4 U$ q+ o! K" i8 ^% e; G   String line = in.readLine();; n: J  C3 e- i, Y. V
   while (line != null) {5 t( v9 w# p" b5 a( P1 F- J
    String temp[] = line.split("[\\t\\s]+");$ d+ d( g8 d2 L1 r
    line = in.readLine();: }) |- H. Y- \9 j% `' ^, \7 i
    for (int j = 0; j < ySize; j++) {5 z. S( |! g$ p: h  |
     matrix[i][j] = temp[j];
% b0 T8 P% b% d1 Z% t2 q8 @    }- `" ]& k. Q( J7 d' l* _
    i++;7 [5 y' m: C# Y& X
   }
( z) o7 F. Z  C$ a4 ?: q: e   in.close();
' B5 Y& x; }6 g' L  } catch (IOException ex) {$ C. w; `/ d( h! U
   System.out.println("Error Reading file");
! Y2 }! a9 t. N% k   ex.printStackTrace();7 s1 c5 o! A, t9 J3 F4 c
   System.exit(0);+ E/ V2 K2 ]; q% x( r
  }
3 a4 x3 k% k" E9 k' \ }8 t' }+ L+ R1 n0 L! A- s+ C
public String[][] getMatrix() {
2 a2 L0 N$ X+ B3 ~+ b* a  return matrix;' C. r- A5 i$ l1 P/ t) e
}3 X5 r7 A! F$ f
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-5 17:40 , Processed in 0.011843 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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