设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6676|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
& y% L4 }) O$ F( g/ pimport java.io.BufferedReader;; Y# v! S0 y. }: B2 E
import java.io.FileInputStream;
- N5 O! i3 n6 h5 P/ z( gimport java.io.FileNotFoundException;7 {7 S; ]; `0 g; b; p7 x
import java.io.IOException;
. _, Z$ {4 _7 E7 A+ r6 g6 jimport java.io.InputStreamReader;
- ]: h/ U' U, D0 Rimport java.io.UnsupportedEncodingException;0 o8 }7 m3 G. E4 x3 W8 `
import java.util.StringTokenizer;
3 }" R0 I' K4 j& C& z& Cpublic class TXTReader {) Z; d" l# }4 u* W
protected String matrix[][];
8 q8 e4 S6 M; ^$ i4 K8 d( F protected int xSize;
. z" x. T* V' m4 T/ a protected int ySize;2 Y, H' a3 D( a) M0 _
public TXTReader(String sugarFile) {# S$ c; j/ C9 l: s* s2 k. e
  java.io.InputStream stream = null;2 z* q2 h. i& L
  try {- X- k# v  D6 H/ r) v
   stream = new FileInputStream(sugarFile);
# q* }: W5 v' O  } catch (FileNotFoundException e) {) H: s8 @. E5 b% y
   e.printStackTrace();
1 g" L6 e" @0 {6 a3 t: D  L( c  }
4 N( U/ z8 q" _5 p  V  BufferedReader in = new BufferedReader(new InputStreamReader(stream));: Z7 f" _! V# @9 t# ]0 H
  init(in);; u3 y6 ]4 {4 u7 i, y5 D3 x7 p
}" d8 e* i+ B5 ~5 A
private void init(BufferedReader in) {( t; i* C$ B- v# v" w8 i4 P
  try {3 x1 v% }- ]8 I  H
   String str = in.readLine();9 K* \$ ]- n4 @: P; V+ L
   if (!str.equals("b2")) {
4 O7 X, ^. Q2 i; B    throw new UnsupportedEncodingException(
& U% E- E7 f" j" {4 d      "File is not in TXT ascii format");
. ?# G: i6 n: ^, x$ b  F   }, N+ J% Y* k& ?3 k0 P
   str = in.readLine();
) ~& }0 }, ^" U  d* `! |   String tem[] = str.split("[\\t\\s]+");
- P, X5 H+ h2 o( M   xSize = Integer.valueOf(tem[0]).intValue();. [8 P/ J- h+ f
   ySize = Integer.valueOf(tem[1]).intValue();; g- f: C( }( a4 |. X, K& m1 b
   matrix = new String[xSize][ySize];
7 H2 H7 ?1 j) p   int i = 0;4 B* x# a$ f; g: }  X0 B  v8 ]
   str = "";
1 q& F/ {! d; g" f' G7 e$ W   String line = in.readLine();0 a) v" w& I+ ?* C1 L) K
   while (line != null) {
. C  U% w/ `6 ]( P) Y6 Q    String temp[] = line.split("[\\t\\s]+");% I% v/ {# X' @; E4 E* s
    line = in.readLine();$ J: e# t* E8 \. b5 k* a% _
    for (int j = 0; j < ySize; j++) {" u4 \! {: E, S
     matrix[i][j] = temp[j];: n" @8 K8 v6 V% ^! S$ i
    }
( y3 M2 G, T( y- X    i++;
  M8 S5 [! ^1 }, R: B, \   }
- V/ H; p4 j/ F   in.close();
, C8 I. }, c+ L# k0 ?1 U. V( K  } catch (IOException ex) {
% L; @% Y& y7 i   System.out.println("Error Reading file");- o; I2 ^6 H( z$ }% o7 s: n( S
   ex.printStackTrace();
- [. P) v/ U$ a5 s  e+ f. m   System.exit(0);
  r! ]( A( |, G! e$ g, z# g5 t  K  }
3 i9 T6 [3 C6 H  k8 n }8 y5 Z7 m$ Z$ p& e- Q
public String[][] getMatrix() {/ _7 H) D+ C0 P0 m* ]& l5 q- L. a9 X
  return matrix;
9 v1 g  _- x2 l2 J* ~ }
, t; W" q; p# s; u% |}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-26 13:41 , Processed in 0.018846 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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