设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7760|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;% V2 c, U& q  Q, F# D
import java.io.BufferedReader;
7 j0 c8 q3 ~; P9 \  ^" }import java.io.FileInputStream;
8 r, M! X8 C3 j: \. d# cimport java.io.FileNotFoundException;
5 f* ]' j( ?( j; z( i" A8 _- Himport java.io.IOException;8 _: R- }( R. E! Q; y
import java.io.InputStreamReader;. ^6 N% {% v2 }4 V4 k' y
import java.io.UnsupportedEncodingException;7 C# Y. q& G  S& A2 t* N  }4 p3 A
import java.util.StringTokenizer;- o' l7 H/ E) l* j0 t
public class TXTReader {# ~! P/ T- Y/ v. R6 f
protected String matrix[][];, R7 s1 ?3 X1 B# y
protected int xSize;) N4 ~7 p  k* P
protected int ySize;
6 V) }  g6 r* W# u public TXTReader(String sugarFile) {: R8 M1 d" m) l
  java.io.InputStream stream = null;9 [; D1 P$ u6 E+ P! \( @  `4 {
  try {4 d8 Y& }' k- z6 O
   stream = new FileInputStream(sugarFile);3 o+ m- A; s/ G" m
  } catch (FileNotFoundException e) {
) i3 `! |7 ]7 e" b/ X& |   e.printStackTrace();( z, i% u1 f% @' q7 G( Y
  }. C" O! D( X# c* ?4 |( P0 m
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
  J8 l8 V7 Z0 l7 F. m  init(in);5 i% X6 O+ w( s0 j9 D1 E6 w
}4 Z/ B; a( {3 c" I9 K
private void init(BufferedReader in) {2 j. e7 O4 Z% i" ~; b/ P
  try {1 o1 l, N5 X8 |- l
   String str = in.readLine();: q7 r0 E/ x& {
   if (!str.equals("b2")) {6 f: o/ u2 z/ Z9 M9 j4 R$ }0 @
    throw new UnsupportedEncodingException(
8 B) ]3 Y' C8 {) I( O: Z      "File is not in TXT ascii format");
! O2 W5 p8 }2 i8 F( W# @/ k   }
5 a* n# D7 Z- u, ~( [2 Z   str = in.readLine();
8 P; \. ?6 a: R3 R. ?   String tem[] = str.split("[\\t\\s]+");% K' P& A' ]) t( _
   xSize = Integer.valueOf(tem[0]).intValue();
7 U+ @2 `8 N5 \/ D- z   ySize = Integer.valueOf(tem[1]).intValue();
0 D2 W# s: p; V8 _   matrix = new String[xSize][ySize];" A% e6 i" F# a
   int i = 0;
6 t, m! T, R8 n/ N( a" M1 u9 j   str = "";
' R) d9 b( w; D! e* \7 j/ d1 q/ r   String line = in.readLine();
. t3 m( ]7 M5 q' K2 g   while (line != null) {
  N& Q9 J; C# v: B    String temp[] = line.split("[\\t\\s]+");% i4 u0 ~. s+ }) ^
    line = in.readLine();( e/ V  Z4 s  `
    for (int j = 0; j < ySize; j++) {
1 ?0 N6 T5 f' ]5 H$ \: N2 c( v     matrix[i][j] = temp[j];: B# e5 ~* m1 ~, t0 e
    }
7 D( Q* m; D! ?6 F9 ?    i++;9 i8 T2 z  z  h
   }% r' Q, v6 ?" I9 D" [; [+ K
   in.close();
2 f2 M. y# n6 C+ I7 N) ^0 J) w& q2 g6 h7 u/ B  } catch (IOException ex) {
$ U3 w: C' N% o/ K" O2 N0 S   System.out.println("Error Reading file");% F) x# K) l7 S) E
   ex.printStackTrace();
. l6 l& }7 S0 r7 D   System.exit(0);
9 |* t2 u9 Q3 w" y  }$ j% w$ c" B" E. m
}
1 I9 z/ o; y9 e$ f; a public String[][] getMatrix() {/ O! n6 V& H1 O& ~+ [! h
  return matrix;: e0 C* d1 e, s
}% B5 s' J* M! n$ s; L6 k( s* O* q: @
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-4-12 08:22 , Processed in 0.016054 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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