设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9084|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;8 j* f" @8 z( N! }9 {
import java.io.BufferedReader;
# b' A* }. }% s. J: Mimport java.io.FileInputStream;
! }8 p& w8 f7 M  D( Zimport java.io.FileNotFoundException;& @9 g4 C) v7 A+ `1 |+ Q
import java.io.IOException;
* _* l1 z; R$ z0 L( O8 ^7 U) l! ]import java.io.InputStreamReader;
, \9 r8 }7 k# D  M# Dimport java.io.UnsupportedEncodingException;
+ p0 I2 i: I8 M2 s/ E. wimport java.util.StringTokenizer;
% Q$ W/ x2 g7 a, y  f0 fpublic class TXTReader {
+ i6 r6 `9 f, m9 Q- E# H protected String matrix[][];
8 L& n0 i% n) o protected int xSize;/ v/ F' x3 A/ i. y; ^! W7 J
protected int ySize;, \0 \# z& F: r6 Z
public TXTReader(String sugarFile) {, W7 `  k+ j: Z! N5 a; \
  java.io.InputStream stream = null;1 e) {% r* R3 A9 j8 B- H
  try {
1 a0 g* X9 U1 j4 Z5 P   stream = new FileInputStream(sugarFile);5 }7 s" i1 e$ O  Y2 [. b
  } catch (FileNotFoundException e) {
- k1 B2 j5 k% f& J   e.printStackTrace();
' k. w. S- ^7 c2 s/ A6 {* X  }  G' m+ k& \  c6 l7 {
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));1 T- M. R5 {& D
  init(in);
) S! x- [( q* l+ h5 ~9 i7 b2 ^ }
. q% _' {) y5 T( b/ B$ L  ?: j! C, R private void init(BufferedReader in) {' K$ m) h9 E3 s) V! o0 S
  try {8 X5 s, ^2 R0 ^0 i
   String str = in.readLine();
2 j2 N2 H( V8 w7 X   if (!str.equals("b2")) {/ l) g  ?8 r5 L. t
    throw new UnsupportedEncodingException(+ i* Q( Z4 {8 L7 l9 ?
      "File is not in TXT ascii format");* c) X8 Q) ^" f6 y  G8 x
   }0 Z; z) E& o" d8 y4 n
   str = in.readLine();
1 |3 ~6 r; ^! t9 P   String tem[] = str.split("[\\t\\s]+");
9 }9 V9 k, d8 w8 @) Z   xSize = Integer.valueOf(tem[0]).intValue();
* b2 J. R4 i1 S6 g5 @# h9 p   ySize = Integer.valueOf(tem[1]).intValue();2 ?* o0 H$ x9 g' C/ V/ w
   matrix = new String[xSize][ySize];/ k7 Y  A; |: i" F6 P. i6 `
   int i = 0;
9 [# _5 M' a- y' j3 T2 w   str = "";$ C$ c" V0 T1 c
   String line = in.readLine();3 J5 _4 ~7 C. A9 J5 _. ]6 |
   while (line != null) {: h% B4 r" N, h- Q. ^; y  K
    String temp[] = line.split("[\\t\\s]+");
7 z) A7 a# v# d; J    line = in.readLine();" {2 @" B5 X, ~
    for (int j = 0; j < ySize; j++) {. I# {0 e' i5 w  r- }
     matrix[i][j] = temp[j];& ^3 J& r; }. O; z0 }) V
    }
" O. e# s3 @. e$ r; U    i++;
/ t3 Y- T) D+ A, o6 x! N; L   }7 k& @/ ^# `5 r, @
   in.close();
2 k9 T2 x4 D* |' a' R9 Z  } catch (IOException ex) {
' o( L# ~. v7 [' W; c   System.out.println("Error Reading file");( D, L6 B) m+ a1 }* `. M0 l% t
   ex.printStackTrace();- D  D5 o6 v0 o6 Q7 W& g$ _" S
   System.exit(0);
) v3 V! W7 X* u  }
# w% q- {3 _; A, p$ z  c8 G }4 o0 z" W% K4 C% @
public String[][] getMatrix() {* P+ R5 ^) }) k( @! X! o
  return matrix;( \9 f1 f6 N& v' h: S0 z
}$ _/ u6 u6 W" W" E9 Q( j
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-30 21:56 , Processed in 0.016741 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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