设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6807|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;+ c/ k* x$ H) A7 g* m0 B: I5 B/ _4 V
import java.io.BufferedReader;
. m: k9 r; V8 R4 H4 a" i1 nimport java.io.FileInputStream;5 V9 _  v' y4 E# F8 k. _
import java.io.FileNotFoundException;
1 P( M' k/ f7 Y- W# m! }) Pimport java.io.IOException;+ m, E* j5 N! v
import java.io.InputStreamReader;
# M, t4 U# N5 l/ R/ q8 Mimport java.io.UnsupportedEncodingException;* t6 i6 F+ l- D) M) i: ?% c
import java.util.StringTokenizer;
% x5 v  w( B1 a* Upublic class TXTReader {  G; |8 W' x/ Y+ r. V! A
protected String matrix[][];; }. p8 \4 S% y8 e# C) f4 d
protected int xSize;
( d4 \% b( h3 J' K  C protected int ySize;
$ r5 l) O9 g- X7 A/ M8 X; G# S public TXTReader(String sugarFile) {* W  C* c8 S' Y* a: a
  java.io.InputStream stream = null;
' h2 y4 J2 c" Q6 A1 F  try {
. E2 o1 {) h, A   stream = new FileInputStream(sugarFile);
5 t1 z- u/ h8 N1 j) f2 U  } catch (FileNotFoundException e) {
: o9 B5 x7 |& Q& f& V3 d/ ]0 Y! x: {   e.printStackTrace();8 }9 b% M  x, ~7 {5 e
  }0 o2 z$ M1 b  d, B6 n( C3 v$ k1 \
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));, R5 D/ m' N! x$ x: |4 b% R
  init(in);
( d. [# u& \* A+ p/ ]5 _$ q }' u3 H, C( P' k' N
private void init(BufferedReader in) {
4 ?( s3 p4 z! m! q  try {. e3 _6 x% Y9 m$ Q
   String str = in.readLine();
& Z6 K8 d  G. r5 @4 z2 @6 X   if (!str.equals("b2")) {' A/ D) Q3 R) e% ?( Z
    throw new UnsupportedEncodingException(6 D: P$ W2 Z* j9 v$ K) Q3 d
      "File is not in TXT ascii format");
4 G( u% E6 Z' n8 W. H   }6 t* r( D& Z2 }9 F1 D0 q# P
   str = in.readLine();; j+ U. L6 c7 {2 i! T
   String tem[] = str.split("[\\t\\s]+");
- D2 j5 V; `9 E3 h* `9 U   xSize = Integer.valueOf(tem[0]).intValue();. C* a( i. f( p3 i5 ]  g
   ySize = Integer.valueOf(tem[1]).intValue();  Y/ C2 s- E+ T( D. E& S; S
   matrix = new String[xSize][ySize];
' e/ ?3 d4 \1 m% l- p   int i = 0;5 z( U! d+ y& u, k
   str = "";
  X8 k, c" B7 i   String line = in.readLine();7 b* u0 a+ @3 S; h& Q
   while (line != null) {
$ y" n1 n$ O2 y+ L" Y* z    String temp[] = line.split("[\\t\\s]+");
  J( b4 m. x$ V3 c* R+ u    line = in.readLine();4 N7 b: J" D+ r6 L# ?
    for (int j = 0; j < ySize; j++) {$ j  B, p  W# x8 e
     matrix[i][j] = temp[j];
/ E# q2 f4 Z: w- m+ E; O8 y( f4 N    }
" P: ~' L6 d& h. w" g    i++;* m* }# ]3 u7 a; {* U. n
   }
& q; |) S9 M0 P, i5 u; ~  X   in.close();8 n5 W9 M) k1 ]' S6 u
  } catch (IOException ex) {, A9 N2 i/ v3 a9 {  I2 A2 [
   System.out.println("Error Reading file");* j& I  ?6 o& k% w+ M% k( Q7 u
   ex.printStackTrace();
5 N; f8 ~7 p. i! m   System.exit(0);
8 V# b1 H' J: Q7 W  }
5 e/ e' H  ]) p! w0 Q  `' @ }1 l8 m8 Z6 T( {$ B9 j8 J
public String[][] getMatrix() {
" \9 G2 o( `* \  return matrix;
& m# E5 W# }! M# k5 N; z2 s }7 e* ]0 N, H: B: n! {' I
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-5 01:35 , Processed in 0.015348 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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