设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10158|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;$ @) n. v4 z& Q5 ?% @, p& O. K
import java.io.BufferedReader;
5 D; i# @6 @) ~/ p" S' H; pimport java.io.FileInputStream;
# |% l7 A- [( a6 o# p2 jimport java.io.FileNotFoundException;" q3 ?' O' d" B8 K4 j# p$ b
import java.io.IOException;( a/ |& i- e; ]
import java.io.InputStreamReader;3 T$ N( s" t+ `, J8 N" a
import java.io.UnsupportedEncodingException;
* @8 Z3 b) H& W' h7 I2 {1 |import java.util.StringTokenizer;
# k3 n3 T: [8 e! ~public class TXTReader {/ @+ x' U( \2 j; y$ s
protected String matrix[][];. M2 g; o- e) m* C+ h% g9 X% E
protected int xSize;+ ^8 s: q- z" s" M
protected int ySize;  Q/ p8 u% T/ v! M* a+ x. H; @
public TXTReader(String sugarFile) {
2 f& P1 {4 i7 I9 ?1 E' b  java.io.InputStream stream = null;2 I& |# H. ^( |
  try {
1 R2 q2 i; h1 d$ k1 Q# K0 w* B9 T   stream = new FileInputStream(sugarFile);
# L  W2 y( O: y1 ]/ S1 R# o6 V  } catch (FileNotFoundException e) {; g: {( _: u, \2 ?  d6 W, b
   e.printStackTrace();0 m, m4 Z3 a* K
  }/ \, R1 j6 B- `8 @6 J- _; {" E6 M
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));) m: B5 F# x, {! |. f
  init(in);
4 ^6 m/ I( T) Y3 _9 O }7 M- p0 h' i  x* n
private void init(BufferedReader in) {+ M; G# w- u, o3 B  h
  try {
9 \1 |1 r: c7 ~; ^1 ]   String str = in.readLine();
) L2 x! J' B( V  B+ R+ M2 Y' g& N   if (!str.equals("b2")) {
) j. k& U3 J) Q4 U* B% u4 g    throw new UnsupportedEncodingException(* A4 R7 x- N7 v* l3 l
      "File is not in TXT ascii format");) n9 y: S" |; a. S
   }
0 b7 j( w: Z* Z( w   str = in.readLine();1 N( W" Z- k) _- A3 Q; ~3 f/ W
   String tem[] = str.split("[\\t\\s]+");
7 a8 M3 g/ A- m( O- H; i; E   xSize = Integer.valueOf(tem[0]).intValue();3 Q/ U: T( \' ~6 J& I: X% V$ e
   ySize = Integer.valueOf(tem[1]).intValue();+ ]6 _! t* u& z
   matrix = new String[xSize][ySize];
; z8 I3 K* @% m   int i = 0;
7 U) p, U  O  X1 L" [   str = "";- c% z0 t# S  q8 ~. U$ y8 \
   String line = in.readLine();
9 s) J$ H5 i0 r( W& Q5 d   while (line != null) {
$ E$ {/ L, ~3 o5 ?8 g) Q, Q0 L& b7 _    String temp[] = line.split("[\\t\\s]+");7 f3 d7 u/ \+ k, m0 i( T+ e9 |$ r
    line = in.readLine();$ H) g. X* j6 g
    for (int j = 0; j < ySize; j++) {  {' j8 o& O* i) f! s
     matrix[i][j] = temp[j];
6 n5 R7 p; [. z: L0 C  l) b! F    }
2 q2 u' g( @' C    i++;
& ^( @" X6 F. ~7 H* }   }8 Y% l% l0 s8 `1 A' x
   in.close();
4 P* r* @  q; e  } catch (IOException ex) {- _& e% M6 r: C
   System.out.println("Error Reading file");
8 S1 X/ F4 ?/ o0 U5 q& [& Z   ex.printStackTrace();
& g( v5 V& r2 S# g5 o   System.exit(0);( {9 c/ k) x, X
  }
) c& P. ^0 y6 B! j4 o( S8 U }' U  q! q% }/ o5 O: X$ ^/ S. ~$ w
public String[][] getMatrix() {, O- ?, D0 M" m* o- G5 E
  return matrix;
; y. y; h* _/ E' _% ^/ Z }
( X2 g, `( Z1 F; `5 M}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-25 19:38 , Processed in 0.016635 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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