设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7461|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;# W, G( O- w5 J4 i; p4 M" x
import java.io.BufferedReader;
7 u/ S# ]8 v7 e3 w1 X% himport java.io.FileInputStream;
# t$ v" i1 t- {/ Q# jimport java.io.FileNotFoundException;5 {! ]7 \8 g. k! {0 Q
import java.io.IOException;
0 y8 c0 e" x0 L/ e; k- V. aimport java.io.InputStreamReader;
% M- L: z6 D) x6 Z+ Cimport java.io.UnsupportedEncodingException;4 y; A- H3 m4 X( [8 u) g
import java.util.StringTokenizer;
( G) }5 l1 K; H; `8 lpublic class TXTReader {
$ \; L! o# @6 W6 w4 Y" j& B protected String matrix[][];
3 E" g( W% Z5 g protected int xSize;/ v8 }2 i' r% Y, a5 `" c2 u
protected int ySize;
; a, @1 F' F% a  f/ K/ g public TXTReader(String sugarFile) {  I: B9 J8 v# ^0 A& q7 t
  java.io.InputStream stream = null;7 \8 t' d6 I1 X
  try {6 q9 n- Y; c+ `* f7 q) d  v
   stream = new FileInputStream(sugarFile);
6 g# Q2 H0 p" ?8 `5 g+ Q4 l  } catch (FileNotFoundException e) {7 L) @1 L1 K+ Z- R
   e.printStackTrace();
* m) h+ x# H9 {' Z/ b. u3 ?  }2 ?: Q, s$ \5 a3 o" D; a4 {2 {
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));* A' D' z, V3 N) H3 k7 [# k7 C
  init(in);
* C7 q% \* J5 d, n9 V }
- D1 M7 A3 X- j- M/ V private void init(BufferedReader in) {
, a4 U( w0 m9 M$ d  try {
; u  o, a: B# C5 v7 I2 a   String str = in.readLine();
$ g. M& ?% k+ r   if (!str.equals("b2")) {
& u( L& _7 o4 d" M% L3 R% H3 N9 h- j7 _    throw new UnsupportedEncodingException(; q/ D) T( |- q* F& V& C  B% C4 Q
      "File is not in TXT ascii format");  D% [" k1 F/ j- J/ Z& w
   }: v  w9 [- w. T
   str = in.readLine();- A+ G4 e) @( `1 u1 I
   String tem[] = str.split("[\\t\\s]+");
* F  E7 x% U& u: X0 [) F   xSize = Integer.valueOf(tem[0]).intValue();
# S/ h1 A- F( T8 c4 `   ySize = Integer.valueOf(tem[1]).intValue();
. w8 s* V% U8 C0 @2 X   matrix = new String[xSize][ySize];
1 d; ?6 y* S5 r' q   int i = 0;, w2 [. ^5 f) Z- `# u1 j
   str = "";- o+ x' Q2 d* g! N
   String line = in.readLine();3 V3 X3 N# t/ s) c/ x6 h
   while (line != null) {
+ T2 Z) F7 G7 H    String temp[] = line.split("[\\t\\s]+");# R9 {, S+ j2 I9 O4 O
    line = in.readLine();3 A; Z' _- t2 S& v4 Y
    for (int j = 0; j < ySize; j++) {) q8 f/ m' p. ?7 p6 o2 s
     matrix[i][j] = temp[j];
! A0 a! o0 C3 w! s3 J3 F. ^    }0 e! `7 P  Q0 R9 i
    i++;  f2 C) S" B! @6 D: ~  Q
   }" f4 ]3 b  `$ M) Q( I
   in.close();
9 I% W8 Y1 b( _/ Y* \; Y' d  } catch (IOException ex) {
+ p6 Z  c+ A. ~4 W   System.out.println("Error Reading file");
& ]% G! L3 @6 ?- g# {, g' K9 K   ex.printStackTrace();+ ~6 t6 Y% G& z- x
   System.exit(0);/ Z( B8 f) ~6 s2 ^2 J
  }8 c; I2 X: }- L+ s
}
: i9 |5 A  Q* u/ v public String[][] getMatrix() {
$ S" `# U9 |, Q6 D2 B, c  return matrix;8 ~, f9 ^# n8 I, H* d/ V; H
}# L" a$ S2 t! G9 |$ U
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-18 08:43 , Processed in 0.015635 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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