设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10505|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
- M9 w+ o+ Y4 q* U) {import java.io.BufferedReader;) a$ E0 r' q  d' {( `8 m3 n6 S; S
import java.io.FileInputStream;& g; c) E% c$ h3 J3 B# S0 v/ E
import java.io.FileNotFoundException;
1 n/ U. c) |% A, wimport java.io.IOException;- Y. ]3 j" w; J, V/ a9 n/ S
import java.io.InputStreamReader;
. z0 R  X, f. G9 b6 J( H* M# K2 i0 |3 Cimport java.io.UnsupportedEncodingException;
  A+ J7 {. r* X9 l( Fimport java.util.StringTokenizer;
" r! T3 G% X2 npublic class TXTReader {
$ t) T) i& k5 @( S' G# t protected String matrix[][];
) ^4 f5 R6 K' h8 p' ]" r protected int xSize;+ W( D+ Z6 f3 ]5 ^
protected int ySize;
# F& Z# m# b: j" C% C5 n6 K2 [/ t public TXTReader(String sugarFile) {5 n/ t- M3 H" {) y  A7 U$ n
  java.io.InputStream stream = null;
# g: a3 |- m; f% U  try {" I) j( q& t) t  [' }5 l* o
   stream = new FileInputStream(sugarFile);
1 O- U. {% M: k( u' `8 o9 A) i  } catch (FileNotFoundException e) {4 s2 @: O0 B9 [* Q4 O: W$ F& I. L8 y
   e.printStackTrace();: c1 R6 d+ r( C" p7 U" x
  }
3 k: ?5 P% p3 g! Y) R: V  H# k3 Y  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
; j9 ?5 v  z2 k& |) j& z: I  init(in);6 S% b# G8 d7 n
}
/ Z, b7 N; R" F5 t% C& K private void init(BufferedReader in) {% n6 W' L  T; |8 @1 O  R
  try {& H- r5 N! k+ d4 E% x% o& O
   String str = in.readLine();1 D# c' Y( P' s! g' l7 m2 W
   if (!str.equals("b2")) {
* v: h! S, ?, C! ~3 R    throw new UnsupportedEncodingException(
# N5 O, J. e, \+ t6 i( e, ?      "File is not in TXT ascii format");
+ v" a; A- i: T0 `+ p% R- x# r2 e   }+ e, w, g0 a" u3 k2 Y, N
   str = in.readLine();9 R7 [0 T- O1 Q
   String tem[] = str.split("[\\t\\s]+");7 b9 z  o" k0 T+ E
   xSize = Integer.valueOf(tem[0]).intValue();
! w6 u% t' r4 I3 ]* u$ L* \3 l   ySize = Integer.valueOf(tem[1]).intValue();- j1 J# K, V8 [
   matrix = new String[xSize][ySize];; Q1 x/ U. A% [; G' ~5 C% c
   int i = 0;
0 l: \, Q4 t" `; W+ P+ N: @( q5 R   str = "";
6 k4 i5 |/ A9 {5 y% i  T" T- O# K   String line = in.readLine();
# D) x6 a/ A7 j  {   while (line != null) {$ X0 Z, l: K& o# t3 o4 K4 j
    String temp[] = line.split("[\\t\\s]+");+ X% p% v2 X' W% X: L
    line = in.readLine();
5 M' U' e: a+ {( k0 C- `" r    for (int j = 0; j < ySize; j++) {
9 |$ |) h& n, @) d0 j6 h. L     matrix[i][j] = temp[j];
7 b3 l6 c! Q4 Y4 _/ J& T) k9 v2 A    }
* q8 X- d# E. S    i++;6 Q! ^* x$ n3 B. A. X: K+ z
   }
5 Z" \6 F& f! \' @9 l$ b7 R; |   in.close();4 U$ ~" y  z: T' `9 |' q
  } catch (IOException ex) {7 q$ D! l/ h: c
   System.out.println("Error Reading file");2 U2 t7 @9 }: j: c7 `; `
   ex.printStackTrace();1 S: l& p& [% q* O
   System.exit(0);9 N- }* K( A7 i( E5 F
  }* {* R8 d0 x' ]9 _
}
( q6 D, }0 s4 S! v. o- C4 G: W: ^7 x+ ? public String[][] getMatrix() {
. A# s) y3 v! M" |9 O# L  return matrix;' q* ?# G, B1 \4 B
}& b( X! E+ V/ T5 P5 k2 }5 {
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-13 21:58 , Processed in 0.013324 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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