设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7512|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
: A# }9 v- O6 _0 eimport java.io.BufferedReader;* c  A" i1 n* z9 @
import java.io.FileInputStream;) x+ \* F/ Z5 |5 m+ }! {1 R
import java.io.FileNotFoundException;& I7 Y% I0 n( d# n. `; v( [4 {$ s
import java.io.IOException;
  m8 W/ b! V2 P# G! ~import java.io.InputStreamReader;  h8 H. A( v* \* A2 y0 m
import java.io.UnsupportedEncodingException;
# X' o% y: i' y/ ~  m. Gimport java.util.StringTokenizer;
/ j6 z- u  o$ ?7 Npublic class TXTReader {' T$ D* l+ u; P- A. [
protected String matrix[][];! F' b  S2 B% U9 h, O2 c
protected int xSize;
5 B$ B) }' D" j2 ?. A protected int ySize;
7 o  J+ Q! @4 U% a+ V2 ` public TXTReader(String sugarFile) {
8 G/ {* c. v; H6 c2 I& F  java.io.InputStream stream = null;3 k& w/ r4 w% O, }" N# D
  try {& ?$ w8 d7 F2 d( r8 f* s
   stream = new FileInputStream(sugarFile);: ?& L- y6 w4 j4 Z, p4 c
  } catch (FileNotFoundException e) {' t1 D- n/ q7 x7 K- ?
   e.printStackTrace();, P. Q4 [7 |1 N- G
  }
) C0 m5 g/ r% k7 e0 X  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
$ `  `9 X8 E" l7 W  init(in);
+ H9 I- Z0 ~$ ?, r }
9 b+ u$ H6 o) m$ B  g) Q, h$ Z7 B private void init(BufferedReader in) {
/ P5 h% N5 N9 s$ [# q" H2 K8 V  try {
: z! I7 v/ x& M' L$ Y9 n8 v   String str = in.readLine();
3 Q2 N0 O! z! A4 L& `   if (!str.equals("b2")) {/ k, `. k  J% t! f6 I
    throw new UnsupportedEncodingException(
. J+ x( U0 i" U' M7 @9 {      "File is not in TXT ascii format");/ @" N7 [6 ~# i4 [1 r9 k( M/ R
   }+ D0 G- t" X! D6 ^( N& g- A
   str = in.readLine();% M% w, `6 I) _7 x( p
   String tem[] = str.split("[\\t\\s]+");" I- Z0 t+ c0 T3 ^4 m
   xSize = Integer.valueOf(tem[0]).intValue();6 F" _: _) @" `  s' M
   ySize = Integer.valueOf(tem[1]).intValue();
3 J. I' T9 y1 `; L4 ^& D$ `$ @   matrix = new String[xSize][ySize];
8 n& P' i4 |# J8 N   int i = 0;8 C8 @$ S# {) U7 P& U: v
   str = "";
; T8 K2 Z/ m, U6 c: p8 T7 \   String line = in.readLine();
. [8 S3 f" A3 ^8 q! V/ k/ _  l; U% f+ |   while (line != null) {
2 w% ]& J# x0 @4 y: Q    String temp[] = line.split("[\\t\\s]+");
) U- x' E$ g$ e    line = in.readLine();+ t6 \% H; j$ n$ u  `* @4 r
    for (int j = 0; j < ySize; j++) {
, @5 A$ ]5 q/ q( |; `0 f* I3 k6 [     matrix[i][j] = temp[j];
2 }3 G# N% d/ i: [' |    }$ `) w" H, K/ T1 ?% T* _3 x2 h
    i++;
9 M1 I& `3 S. N* I+ T% N   }
# @1 z4 H9 K2 x; G: B/ z! s   in.close();
/ K7 X. l1 l% P( S7 q" Y0 {1 ]  } catch (IOException ex) {
6 t0 k2 ~+ i' C  k7 o- |* A( p0 i   System.out.println("Error Reading file");3 G/ Y- j9 r2 ~( f! s
   ex.printStackTrace();% d  }8 i( J" D5 W/ o/ L* R
   System.exit(0);
6 m- D9 i# a, a; V+ }  }9 G7 x# _! `& ~' J6 D
}
9 A) Z+ s# p5 j1 D! w public String[][] getMatrix() {
0 U9 f! R& [1 F% ?3 x  return matrix;
6 r) V! N3 y9 V2 k! L& A }8 Z5 r; h3 V  \6 W& d- @& i  i
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-23 07:38 , Processed in 0.023435 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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