设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7012|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;4 e) p+ y' l' N
import java.io.BufferedReader;# V" k5 z; ^2 K2 O. t
import java.io.FileInputStream;9 w9 j8 |5 M0 r  S0 p
import java.io.FileNotFoundException;
7 f  _" G, ]; D2 U1 P2 ^import java.io.IOException;
/ N) D2 \6 f& @0 v* [import java.io.InputStreamReader;& [1 g; i9 @" K& y: Q8 ?
import java.io.UnsupportedEncodingException;% G; y% K! v, h* r
import java.util.StringTokenizer;
3 U  b6 N! W6 h! q8 \1 hpublic class TXTReader {
) u& o+ w$ `' T& ^& X# p( D protected String matrix[][];
) j5 K4 J8 f! H; p protected int xSize;
4 f; V3 w+ Z4 \, M, Q protected int ySize;8 H8 \! e3 k+ i. k# @7 j
public TXTReader(String sugarFile) {
& Y0 q! V" ^) f9 u2 r7 d- |  java.io.InputStream stream = null;6 N; @& @2 F+ b" r
  try {  W" F1 B/ x# R% \+ w
   stream = new FileInputStream(sugarFile);
9 K3 p% l: n6 K  } catch (FileNotFoundException e) {
9 ]: e  {" ^8 W9 e+ P( j   e.printStackTrace();. G( E8 L+ E7 L' p( {& w
  }
+ O! ]; J' _" \1 N) L  BufferedReader in = new BufferedReader(new InputStreamReader(stream));% Q# E% t( q# ~% R( r8 m
  init(in);
- t. ]" X3 h5 r1 @ }/ n+ k9 H4 K# O  c1 Y% [' t
private void init(BufferedReader in) {# G9 B: j7 w/ X" U# r& G- e
  try {9 }3 ]( x! n, U; S1 k
   String str = in.readLine();
- A% V; H& n! V) S' r6 c   if (!str.equals("b2")) {8 M, d. U' v  a0 k! @- r! D/ D
    throw new UnsupportedEncodingException() X1 G  V1 ^# f
      "File is not in TXT ascii format");& e+ F# M1 L; l" t) Q! z: }
   }
! q' L6 U6 [; ^- P: b   str = in.readLine();
( q( X4 g1 |) S  s5 p' O. ?   String tem[] = str.split("[\\t\\s]+");1 p0 Y2 K; y. Z$ b. i- p' \
   xSize = Integer.valueOf(tem[0]).intValue();5 e9 J# a, @1 B
   ySize = Integer.valueOf(tem[1]).intValue();
! @  ~  ~" O2 J; I* E   matrix = new String[xSize][ySize];: w/ J2 J3 c4 x- a9 z2 E& u: W- h1 S# p  L
   int i = 0;
' g3 w1 t# i& N. v/ R  G( Y. y* N   str = "";# N  O2 k5 s% s" \! O) d! {
   String line = in.readLine();
5 o2 L( J/ w- X   while (line != null) {
' q4 z5 C6 j9 j% S4 U( O    String temp[] = line.split("[\\t\\s]+");
! n$ P$ ?* m/ X* K; V. c    line = in.readLine();, ?" T# J8 Q: P/ ?+ D0 m
    for (int j = 0; j < ySize; j++) {' v  h; h5 |( G1 A% j2 s
     matrix[i][j] = temp[j];
* F+ R) r7 J: D* t7 ~    }2 Q9 o) a. N0 s1 u& C$ ^" {
    i++;
. g% o7 n. d6 M; u0 F, v   }8 u# \' j& ?" L7 D/ Q
   in.close();
* D5 R1 ~; @) _: [. U6 T/ O$ v$ \  } catch (IOException ex) {
6 I: C2 c# `3 h  T/ p   System.out.println("Error Reading file");
  s2 ^, f* m7 I& E, {& M   ex.printStackTrace();
+ L! b5 d0 |1 T+ f" C9 j- M   System.exit(0);6 ], {: T8 m9 u; t; {& c
  }) G, Q/ I) z) I/ }
}
, n" P( f1 }: e7 N" m2 t2 u# m public String[][] getMatrix() {& o) r8 b/ ^2 x! s9 i# W$ q) x. O" t% I
  return matrix;: y( m' ^2 Q5 \$ Z1 _6 `
}* T) z/ H2 [6 k$ @0 _  Y8 Q+ q
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-15 20:55 , Processed in 0.020028 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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