设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8191|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;' G' h# w: v4 S/ V& _
import java.io.BufferedReader;0 ^. g9 @9 N' S+ k1 A  S
import java.io.FileInputStream;+ N0 _% |5 @- d" a
import java.io.FileNotFoundException;" P) c1 m3 g4 N# l8 S7 O
import java.io.IOException;5 H3 i5 g1 r; ~6 a& d" h( M
import java.io.InputStreamReader;4 t. D. ^7 a) h+ w) H
import java.io.UnsupportedEncodingException;7 w" X# S9 {7 W8 N% `" |
import java.util.StringTokenizer;" S; ]' r' q; z5 ~3 A* b
public class TXTReader {
: P9 U1 q$ W7 d* E2 \ protected String matrix[][];
8 Z( [& `; _! ]9 o protected int xSize;/ [" c+ |2 y. n
protected int ySize;' J4 r' ]+ f% E8 M2 a1 Z
public TXTReader(String sugarFile) {
) _: s5 G  p; C' g- X& C7 v- k  java.io.InputStream stream = null;2 ?% l8 A' |- z
  try {
6 s# \; g# z6 m% n   stream = new FileInputStream(sugarFile);
- a$ [' I( t8 {( m% U  } catch (FileNotFoundException e) {
1 L0 [6 P6 g+ H) [# Z   e.printStackTrace();9 ?; D' B5 }* i: ~5 t& W- C
  }
! I7 w; b  A. |% _3 Z2 A% ^  BufferedReader in = new BufferedReader(new InputStreamReader(stream));  }4 T/ |9 H. T# i, M
  init(in);
. k/ \9 c+ V# h }
9 Z; y3 S7 H/ o" h* d! I5 O private void init(BufferedReader in) {
* y) \- l, b- l7 O, i1 O3 O- D  try {
& l# X$ F$ H8 D8 R   String str = in.readLine();
. V! N. E, ^# f& J" }) ^   if (!str.equals("b2")) {6 J  c; |# A5 M, b( u* z& ?/ \2 H
    throw new UnsupportedEncodingException(
* ]& |& |' V: \' e! z1 H+ {      "File is not in TXT ascii format");
; i9 m! T5 V$ H) s' y6 L+ s+ K   }
4 i8 a- Z, F) v& x. E   str = in.readLine();3 q) n* I9 D7 }8 N! h
   String tem[] = str.split("[\\t\\s]+");
4 u. u, j! o+ p   xSize = Integer.valueOf(tem[0]).intValue();
. O# X) P4 `# A   ySize = Integer.valueOf(tem[1]).intValue();
. h- L7 _. }7 Y6 K   matrix = new String[xSize][ySize];8 l" ~7 h! l: F* J. Z
   int i = 0;2 u* q  K8 [) i( D6 C
   str = "";
- |: V. x1 |2 r8 `! M   String line = in.readLine();( m! F9 y5 F7 y1 F0 u4 O1 U
   while (line != null) {
3 r  W% _( `2 \2 I: r- h' W    String temp[] = line.split("[\\t\\s]+");6 C! \; {/ F+ [
    line = in.readLine();3 z4 v/ k( h) X( }) z- X' B
    for (int j = 0; j < ySize; j++) {
+ v/ U4 q8 o& F( j     matrix[i][j] = temp[j];
! @8 g  ?6 Y. C# S    }
2 n3 I. K/ R" {- Y! z8 G    i++;: |4 C) a; V9 Y
   }
2 H8 F' N6 Z/ e   in.close();; ?' w  f4 E' }9 m, F
  } catch (IOException ex) {
( w3 L  B% z# p' \   System.out.println("Error Reading file");
0 p, {$ r3 k% ?! f   ex.printStackTrace();$ R! ?( e, Q) C( N
   System.exit(0);
9 i2 ]2 j/ ^" M/ }4 I1 z. O" ]. Y7 v  }
$ e: _: K* y; a; g$ G0 f }2 ^2 p+ S  }7 ]4 `) ]! f6 _' h1 l8 R
public String[][] getMatrix() {, W" K( p4 R' B2 D( Q
  return matrix;
/ R! k, K7 d0 G2 _- r8 x }% B9 Z& ]* W. b  l, I# o, ^
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-7 16:21 , Processed in 0.021043 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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