设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9589|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;5 a( u9 H* {. P$ h  x; C  `0 B
import java.io.BufferedReader;: Q, U7 J4 e2 T0 ]! A0 ]
import java.io.FileInputStream;+ e. C  f+ e0 e  Z2 Y+ Q
import java.io.FileNotFoundException;- H9 X& X# v. H2 n/ V
import java.io.IOException;
7 H  y0 s9 M  I7 u' @# Yimport java.io.InputStreamReader;4 ]% q) S9 b6 V* c- l) {
import java.io.UnsupportedEncodingException;
6 c5 I' p& b; L% x% }import java.util.StringTokenizer;
3 A" n5 r8 i; Z. Ypublic class TXTReader {; z' X6 j+ I) A. ]
protected String matrix[][];2 Z$ V+ l7 K4 i; E
protected int xSize;
" f4 i0 r" S1 A) H6 v( | protected int ySize;6 P) j( X* ?0 T% Z3 ^7 \6 C& s
public TXTReader(String sugarFile) {1 s& F% b2 ]& A' J" s+ f4 E( t- V
  java.io.InputStream stream = null;
: ~, S8 {( U$ ]! r' N  try {2 r; j9 _/ f3 a" R% h
   stream = new FileInputStream(sugarFile);
% m; @: A5 O; P  } catch (FileNotFoundException e) {0 n1 c7 E2 X2 Z3 k: ^, {' J; ~
   e.printStackTrace();
* M5 e% o) b( r) N) p1 k( ^  G4 b  }% A1 P/ }- v( ~; s0 y! Z, `
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));' s- r9 q$ V3 T/ E. l6 `! J4 g  H
  init(in);5 F, m- Q# Y/ s9 y, p% Y: Z
}
1 V2 y! R  ^: m6 m' A private void init(BufferedReader in) {; l4 N* H" q- m' T! b
  try {5 N1 J$ M& a8 S6 g# c
   String str = in.readLine();
# d' A1 k$ F5 ^# z4 C9 q0 Z8 P7 E   if (!str.equals("b2")) {/ Z3 [$ L( A$ L
    throw new UnsupportedEncodingException(& l) T# y" ~# ^/ M4 H% }
      "File is not in TXT ascii format");! v1 d% I+ ]6 U2 O. k. q. O0 L/ [
   }
- A6 V" |) V7 V   str = in.readLine();( h2 {1 r8 U' g
   String tem[] = str.split("[\\t\\s]+");! K2 h3 v- r4 R
   xSize = Integer.valueOf(tem[0]).intValue();& u! c* W  g  h$ y0 u0 d# G" ]
   ySize = Integer.valueOf(tem[1]).intValue();8 L8 M8 ~8 T0 X1 s# l- x3 ]
   matrix = new String[xSize][ySize];$ k! d, r( ~3 t
   int i = 0;2 L" P1 R! t* Y
   str = "";
* g" A( q" p/ t: U& d$ _8 I. g8 q   String line = in.readLine();/ q8 `. a6 c4 s2 r2 \% k: {
   while (line != null) {
  D; D  T8 V. A- B% J0 O    String temp[] = line.split("[\\t\\s]+");
: i6 P7 E5 L  |  o& u    line = in.readLine();
5 F& n+ s0 Q9 W% t$ O    for (int j = 0; j < ySize; j++) {/ \8 l  p" K- [, A3 p5 x9 K( l
     matrix[i][j] = temp[j];  R4 g; z9 h$ O; m* Y! p/ [
    }
; l0 d7 s! l1 h& W$ ~1 N8 I7 n    i++;2 e1 T% q5 q+ d& c# t7 U9 O
   }& Y# w' J) H$ E  q' `
   in.close();
% y+ A% @+ q: l8 v; o  } catch (IOException ex) {
+ l- l. m1 G! ^% E% l& c   System.out.println("Error Reading file");+ Y9 q' _4 w& I2 \9 i
   ex.printStackTrace();. `, R( g2 o/ z/ d1 H* w2 y3 c& n
   System.exit(0);
1 x* \6 K, _0 w/ r9 p  }  i- _5 N9 o% I
}2 ~* G6 E% C$ X$ o; J5 c7 H
public String[][] getMatrix() {; M) x. x) [6 ]' H/ u7 e9 d- \% N
  return matrix;' a4 X" b3 D+ G9 H, g0 s4 Z
}
# w2 [; Y" P* K; s+ ]}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-26 05:51 , Processed in 0.014071 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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