设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9063|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
5 ]2 R& l* Z0 y* _# Gimport java.io.BufferedReader;% M$ ?" N: x# x$ D2 |) f; l% P+ O
import java.io.FileInputStream;2 O) M5 V, g+ d, Q) h9 N% ^
import java.io.FileNotFoundException;: ~0 J5 E1 f- Y7 z$ h/ I0 ]6 Y
import java.io.IOException;6 {. N& U& z+ y$ P# Y
import java.io.InputStreamReader;
9 t) m0 E% b/ ^7 }- j- ?6 Nimport java.io.UnsupportedEncodingException;4 c% g. q, l6 h$ s/ A- p8 l) D/ m0 }
import java.util.StringTokenizer;
' F; S- m' S5 x, Ypublic class TXTReader {
8 r/ G6 Z( J: U0 ?9 M7 {0 v+ ~ protected String matrix[][];
/ j/ F7 v" W# D5 m3 P/ y9 F protected int xSize;
$ p3 l) h$ L" ] protected int ySize;& c. _5 u: X" s+ y2 W( \9 T! h
public TXTReader(String sugarFile) {
/ R# e4 M9 \8 b- b7 w7 s7 W- Z  java.io.InputStream stream = null;" j, |8 I2 n1 @4 [
  try {
% u) _+ c: g1 t% L' ~/ M   stream = new FileInputStream(sugarFile);
4 W9 U$ i8 J4 {% E5 \  } catch (FileNotFoundException e) {* ?3 m: ]/ B$ Q- ~. b) r
   e.printStackTrace();
. M8 V9 R6 M% B* L7 [  }* T" ?6 j* u( |) C  n2 ~/ j
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
/ g' X$ A8 N+ U  init(in);
. Q$ T  x: |/ Y/ Z2 u5 w }
' k( s* n8 U: B, l1 s+ r1 ? private void init(BufferedReader in) {
, J6 T. B0 }/ V9 ?  try {2 E( d, R6 O, h3 a# p; T4 J
   String str = in.readLine();
/ f/ C; M6 T) a2 ~* i   if (!str.equals("b2")) {" t3 e* A! G% V* U2 c
    throw new UnsupportedEncodingException(
+ \5 Q" q- W* B! W7 E      "File is not in TXT ascii format");
8 m4 l5 a1 ^7 F   }
! F! D' j, H# K. y: n   str = in.readLine();
. A+ H9 G* H/ s" t, C   String tem[] = str.split("[\\t\\s]+");( Y9 ]7 [% q. w+ m0 l
   xSize = Integer.valueOf(tem[0]).intValue();+ u/ @( E- k  A$ K5 T+ F
   ySize = Integer.valueOf(tem[1]).intValue();
( Q; w' c0 g3 O( b" C: J, T7 y   matrix = new String[xSize][ySize];3 ?. z+ j% N2 L$ T, S& f
   int i = 0;) f5 _5 P; D6 B9 ^" X
   str = "";
, y  x& L# q, W1 K, L3 l   String line = in.readLine();
/ ?. p: ~2 c  {+ l* I5 p/ l5 z( I7 g   while (line != null) {
6 g0 s  E1 f( s' Z0 S  T0 b- z    String temp[] = line.split("[\\t\\s]+");
8 c6 `( R. t, Y    line = in.readLine();
" ^3 F# b! u! m' A3 h) V    for (int j = 0; j < ySize; j++) {
8 r! D& ^' t! M) ?% ?; M     matrix[i][j] = temp[j];
' H4 S' }7 _9 a' m( d6 ?    }
2 k+ u! |# |/ Y    i++;
: u  p# L3 [8 B  g: g. r. v   }  s; {( _4 K) b6 T
   in.close();' y, W( s& {% H  f: A  X$ A& m
  } catch (IOException ex) {- T1 Q. t" }0 F+ P4 ^
   System.out.println("Error Reading file");
+ r5 J5 g  L7 {7 O' ]8 |8 l+ [   ex.printStackTrace();- H0 ]( j- K- h9 Q& A% M5 @, X
   System.exit(0);
6 K8 ?, B) c8 b5 f" ?2 a. [  }
/ }4 b2 t' R6 b! H }8 Q( }  C; c/ X/ W
public String[][] getMatrix() {" k$ f  F# Z. U! a) k9 J0 u
  return matrix;- z( s2 o# r; n1 D" L
}
- C" |# N2 w; B8 @}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-29 12:20 , Processed in 0.013762 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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