设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6527|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;' }* b$ `0 g- y" ?9 _1 u
import java.io.BufferedReader;
6 n4 m2 r1 f, D9 g) E: qimport java.io.FileInputStream;( K1 z0 \7 Y& s0 _: Y* E2 W& Z. R
import java.io.FileNotFoundException;# j% F) P* Y% \
import java.io.IOException;* M, V) f  _0 ^
import java.io.InputStreamReader;/ F$ u) i" y+ E5 e9 T% }: z% R* G, s
import java.io.UnsupportedEncodingException;
  S# j2 A5 h( o+ K! b; h: v1 Y" d9 Limport java.util.StringTokenizer;, W  ?6 `( K! M
public class TXTReader {
7 p9 Q) ^' [# c protected String matrix[][];
' R0 @- g7 X  P2 H, Z, ? protected int xSize;( z: Z6 k1 M5 S# d7 C+ E
protected int ySize;
; H' x" U0 D: ` public TXTReader(String sugarFile) {
( B# K0 z! I1 D& i  java.io.InputStream stream = null;$ }/ G5 m- i& l& @: i$ ?, S5 L
  try {* r5 M9 w' K( n$ w4 J) S
   stream = new FileInputStream(sugarFile);
* u# e1 H: o9 H/ g3 V+ }  } catch (FileNotFoundException e) {
7 S+ |. c, ?+ e/ @   e.printStackTrace();
3 p9 Y, t# K3 s9 W  }+ X' M5 O( V3 t  t
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
+ A% l8 F4 C* j" R  init(in);
! p4 S/ B+ X* P }/ R5 m% }& F. Q
private void init(BufferedReader in) {7 g5 s; h. K4 R& B7 v' ?
  try {
6 O1 Q' Q" ]: j5 u. Z! i* G5 Q   String str = in.readLine();7 R  P* F% z! P
   if (!str.equals("b2")) {
8 B. T: k% ]6 N' _% t& W: t    throw new UnsupportedEncodingException(4 y, }: t' b) \  w: B( j
      "File is not in TXT ascii format");
! F8 p' y" F' g6 @# d   }8 P% @& x& B$ d/ x: V+ z
   str = in.readLine();% Q0 F, l9 `9 B7 I5 T- ]
   String tem[] = str.split("[\\t\\s]+");
0 j; s+ H' f! y   xSize = Integer.valueOf(tem[0]).intValue();
& H4 L4 R5 q) }   ySize = Integer.valueOf(tem[1]).intValue();6 [" J" T% H" q- Z8 b
   matrix = new String[xSize][ySize];9 U# ^! y+ `- S' U7 b
   int i = 0;) r! f. o! h- h# x
   str = "";7 S7 V9 _5 h% I1 Z4 I" E" B
   String line = in.readLine();% v/ I: k( r# ^
   while (line != null) {# l4 d$ F) s/ O+ R
    String temp[] = line.split("[\\t\\s]+");' M9 R( N% `" O& N! L: E
    line = in.readLine();* M" L; g8 o: v
    for (int j = 0; j < ySize; j++) {6 N8 F4 J) O/ `, O( }  E2 J
     matrix[i][j] = temp[j];
+ C6 `* v* e6 `% S% `' C" j$ K    }
% K+ t, o6 H# q- M0 y    i++;. X% p/ W$ p& L7 k
   }! f: m7 I0 T; P7 v6 _: O
   in.close();) c0 c$ a! C1 }0 l; a, g
  } catch (IOException ex) {
! r, L/ e0 d8 a2 y- J4 E3 S   System.out.println("Error Reading file");) r( z$ K$ |+ B' P3 A% C* \1 w* J* ?
   ex.printStackTrace();
; _" }; Z' w/ R' j& X, A% C   System.exit(0);7 _! u7 J7 l$ x( F9 L" i7 {
  }& t  `7 U! u- R( l/ ?8 A
}+ Y1 L' B, B" C+ K
public String[][] getMatrix() {9 u# y2 |. I; x* @' j6 y8 _
  return matrix;& B$ E/ q5 |4 c# F1 E0 U: j. X
}5 c- ]- o  y, V8 |
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-16 09:07 , Processed in 0.023817 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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