设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10314|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;2 a/ |! @  u6 t' ~5 ~8 X
import java.io.BufferedReader;  n6 y* M2 j0 c0 M
import java.io.FileInputStream;
% T" u# ~$ @5 Pimport java.io.FileNotFoundException;
+ x! l2 p. ]5 L; ^import java.io.IOException;
, S; V: B5 k& n$ d7 `; Jimport java.io.InputStreamReader;. d+ i: Q8 H  J
import java.io.UnsupportedEncodingException;2 I0 G/ }6 F. @! h2 T
import java.util.StringTokenizer;
# F! J" z9 L+ q0 D& W  xpublic class TXTReader {: d$ \' t  u% M7 ~, p* q
protected String matrix[][];$ [1 R; }  r5 \+ V9 _) V
protected int xSize;
7 b! _2 _8 o3 m/ K0 }2 q protected int ySize;
, w) A5 |* E! |4 w6 V0 H public TXTReader(String sugarFile) {
- M. z! D2 ~# X6 D  java.io.InputStream stream = null;: f+ h) t! c0 U+ H
  try {
7 G/ S' U5 o- F! [( `6 W  c5 L: b   stream = new FileInputStream(sugarFile);
9 y; z+ M* h; Z; o/ U  } catch (FileNotFoundException e) {2 I  _( G/ `- {1 H/ {* w  W
   e.printStackTrace();9 Q) z. }' F- U1 O
  }7 y& m6 {, M& E+ w8 M3 ^. s
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
, P# L# g: `% W* T: c  J& R( E0 M2 j- c2 K  init(in);0 q2 v5 j% w8 z+ J
}
; G6 h- ]7 M  d% S private void init(BufferedReader in) {( o8 E0 N- B, q  u
  try {
  i& w( D  b- N* D% G4 ?, v   String str = in.readLine();
3 d8 T; a3 A' ^& q   if (!str.equals("b2")) {1 m( I' d! _4 A: R: U
    throw new UnsupportedEncodingException(
, T/ V% P# M6 T      "File is not in TXT ascii format");
" o( i5 q! k+ X3 @   }/ q% c# |% D4 c& h7 @- F
   str = in.readLine();
( \& N4 ]% x/ u. ?4 R   String tem[] = str.split("[\\t\\s]+");  i- P8 W) @, n
   xSize = Integer.valueOf(tem[0]).intValue();6 \5 v5 B. c; ^3 b) t
   ySize = Integer.valueOf(tem[1]).intValue();
' U& f. U- U: l   matrix = new String[xSize][ySize];
& P/ Y; ^( W8 y& K( _. f   int i = 0;
2 ~# l: G0 J: O, ]4 V4 l, R9 X% ^   str = "";
7 F0 |2 a$ H) O5 W* z+ u5 u   String line = in.readLine();. p9 B6 y1 G* U/ W1 f2 B
   while (line != null) {& G- |; l% c6 {2 W
    String temp[] = line.split("[\\t\\s]+");
* N$ g5 o& D1 C# z# V1 C' I- M    line = in.readLine();4 F+ N# u' C6 `. v7 J
    for (int j = 0; j < ySize; j++) {
4 S0 D4 K! W! ^     matrix[i][j] = temp[j];
! F: C+ H1 K5 Q: `$ w6 q    }
/ M$ L6 e. E' a: f. U- S    i++;" Q5 o" J! K' o1 S& D# E4 S, z1 \
   }
7 {2 J! I# M5 f$ n1 q* T/ G   in.close();
( I, l6 l# ?) y- [* \5 q  } catch (IOException ex) {
! _0 b$ g7 h& h0 M2 T( S0 b1 x   System.out.println("Error Reading file");
. i" b4 W  j( ~   ex.printStackTrace();
9 g# w# L0 G. @/ N  V  ~   System.exit(0);6 a0 g) a: `3 Z% K- R. o3 e! `
  }* F% k9 j# y' c/ h; |
}
& X* i3 Y6 j+ M& L4 z# f public String[][] getMatrix() {+ c: F" x1 V4 u  [' F# y5 w, A4 N/ m
  return matrix;* y  h( E2 e& s+ _0 M. H7 O
}* G5 u2 b& v  i7 c
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-3 19:33 , Processed in 0.016846 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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