设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7371|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;& v% ^" {, T9 u. t9 e$ @) j4 r
import java.io.BufferedReader;
& y& g$ o# H* Kimport java.io.FileInputStream;
0 w1 t8 q7 ~7 x7 f, Yimport java.io.FileNotFoundException;: d/ T1 s. o% L6 m
import java.io.IOException;
8 q0 B/ k  ?  k8 I# w( wimport java.io.InputStreamReader;
" p2 v1 _: m% ?8 s/ jimport java.io.UnsupportedEncodingException;
0 {9 l  K, A  U5 Q& c8 Yimport java.util.StringTokenizer;
; y9 r* U+ f/ n; Xpublic class TXTReader {
: p$ i8 }; h0 ?- U  h protected String matrix[][];# l; L0 G. M( |0 O% P
protected int xSize;- F1 i- q$ P& {( Q- ]- \2 K6 ?
protected int ySize;
, `* i4 O% B+ H+ ~2 x public TXTReader(String sugarFile) {1 p8 S5 y1 \7 b4 {# p4 X
  java.io.InputStream stream = null;4 _, D' w0 v( F1 |& ]. b, j
  try {7 S0 B, E# ~# v1 t' x/ ~9 S/ [
   stream = new FileInputStream(sugarFile);
, j3 |% M2 r: q' l4 _, L7 ~  } catch (FileNotFoundException e) {
; G9 n2 |, E9 R   e.printStackTrace();/ Y, c7 C. b7 l
  }
5 t0 u$ _7 {+ [1 w3 I6 w; @0 |  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
2 ]3 u4 v8 l/ \" K  init(in);
5 E0 B$ u# ~& d# R }* _0 X+ Z. y- @) Y! V$ k
private void init(BufferedReader in) {# `) P! a4 k* ?% }/ B$ U1 Q
  try {& ~2 X/ ~6 ^' \8 N
   String str = in.readLine();3 F3 W$ }7 A7 s) e2 @4 d/ K2 K
   if (!str.equals("b2")) {! Y3 o& u8 L% T3 }1 \; e
    throw new UnsupportedEncodingException(
* G  [& j3 o% M+ w      "File is not in TXT ascii format");
% \5 v9 K% N: x( y   }" x, x8 Y3 b5 ~, _+ H* b% t" g6 Q
   str = in.readLine();: q0 b! [  E7 ~0 L5 Z3 G- C
   String tem[] = str.split("[\\t\\s]+");# N' i3 g0 H2 Q
   xSize = Integer.valueOf(tem[0]).intValue();: F6 Q; H" z' V5 P
   ySize = Integer.valueOf(tem[1]).intValue();) b# c1 `# E( g: |
   matrix = new String[xSize][ySize];
+ s+ [+ l, F+ q  a, w2 u5 P5 Y   int i = 0;% I7 x# \3 T7 b" F/ S  V5 `
   str = "";
' c0 c1 `6 ~$ F$ k' h) \  j0 r   String line = in.readLine();( n* T  H0 n+ K& A) x% o+ }: ]* k
   while (line != null) {5 X8 t) |9 u' B9 F/ C% A$ F
    String temp[] = line.split("[\\t\\s]+");; y; x2 F5 ~+ p
    line = in.readLine();
& ^. k- i$ C, }0 |- o& X- I. T    for (int j = 0; j < ySize; j++) {, R. A* J$ D2 x3 k
     matrix[i][j] = temp[j];
1 H8 t( r. n! K5 A    }: K9 U- D9 ]( b  g
    i++;5 X* p/ n* K+ W' A
   }: `" D, h2 p: j; B
   in.close();
4 a4 v$ I$ P, H  } catch (IOException ex) {
  a3 N5 D+ x" p# q$ y: h   System.out.println("Error Reading file");) S- H6 J! q8 ^
   ex.printStackTrace();& T! H% D8 [& I, `8 g( y! f
   System.exit(0);
/ A5 w, d3 ^. Z* v  }4 z' _2 B5 ^7 f
}
4 ~; M5 I9 g* k7 ]7 }* C; v7 ~ public String[][] getMatrix() {
1 c1 b: ^3 n3 n# L  return matrix;+ v$ Q6 C* q& D
}
/ X( ~- b. t- @2 z% n2 q4 D}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-11 04:32 , Processed in 0.015959 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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