设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6483|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
* o7 J  m0 P2 i1 Oimport java.io.BufferedReader;4 t  x( G- C$ u3 k/ e" p" ]
import java.io.FileInputStream;
. r0 B6 E4 I1 |. b- _9 N6 \import java.io.FileNotFoundException;
* K/ _6 c3 u+ |import java.io.IOException;
/ Y- R  U. A1 {# j3 k0 dimport java.io.InputStreamReader;
/ a. l' ^- H% Aimport java.io.UnsupportedEncodingException;
' @  i8 S/ u) o7 simport java.util.StringTokenizer;& M! j& }. V. P( Y  N
public class TXTReader {5 n" |' T( Q* n: @
protected String matrix[][];  Z+ y) t" @5 x' a
protected int xSize;' g" r) N2 {$ S: ^) z
protected int ySize;
4 g) K* J7 C( H( b5 \ public TXTReader(String sugarFile) {9 W' _, ]! G# m5 }( G
  java.io.InputStream stream = null;* K" O) D. Z& s
  try {$ |" _- r. h" L. q7 e
   stream = new FileInputStream(sugarFile);
/ a" ?8 Z& K5 t6 b  h4 i  } catch (FileNotFoundException e) {  _7 P4 r, d+ x
   e.printStackTrace();9 @/ m2 f) }. X
  }
! f. f. i+ P) f  BufferedReader in = new BufferedReader(new InputStreamReader(stream));$ E8 H) n% b. z: E
  init(in);$ k. ]( `' R; g4 [, U
}
/ B/ c9 v' N( O private void init(BufferedReader in) {$ Y, h7 K! a5 }- j7 d: ~0 K/ d
  try {
0 R$ u0 @( m% s' m* Y  M   String str = in.readLine();
' V" Y" u: J$ f) q   if (!str.equals("b2")) {
, L$ w; N1 X. U5 w, j2 i4 R    throw new UnsupportedEncodingException(
6 J. y1 E  w) z( l  ~* T. J1 Z      "File is not in TXT ascii format");4 \" m$ p6 w& t+ ^% m" A
   }6 F$ u* H8 ?9 H+ F+ x! j
   str = in.readLine();
# ~# o* g) X* }6 _4 g8 k7 H   String tem[] = str.split("[\\t\\s]+");
' r5 u; O" N  S8 V. k. o8 ^" y( N  H   xSize = Integer.valueOf(tem[0]).intValue();
# W# |' H; m+ o7 z- S   ySize = Integer.valueOf(tem[1]).intValue();7 U* J* P* M7 n' Z, @9 ]
   matrix = new String[xSize][ySize];
; A; M6 n3 Z# ]% a   int i = 0;
- C: I; k& @+ t7 u   str = "";- |% L) b9 v# q! B7 B% m
   String line = in.readLine();0 [( u7 K3 C5 C  l6 H* K5 i
   while (line != null) {
; ^4 @4 a5 f: V( r    String temp[] = line.split("[\\t\\s]+");5 J; D' U- h+ Q% ?
    line = in.readLine();
& Z0 a. X+ c# Y5 R+ ]    for (int j = 0; j < ySize; j++) {
+ @6 \0 v. }/ R     matrix[i][j] = temp[j];2 C! Y3 Q: x% A8 R. |% `
    }$ {) x; n! H: i* f& b- G7 O
    i++;2 V3 c3 N- h# R
   }
) ]& ~1 m# X, f7 f) i   in.close();
/ N* w3 F$ r# @1 _& L& R& ~% v  } catch (IOException ex) {
& u5 G6 s2 F; `* f- a   System.out.println("Error Reading file");( |3 E2 a+ p. Z" g0 F
   ex.printStackTrace();) D) i& I/ |6 Q
   System.exit(0);
0 [& O! W' m; c5 X$ I7 x9 @% B+ u  }
* `3 g/ N+ U# Z# q4 a7 y" V }
$ H  {8 m0 ]! H5 X, X+ ~& ~ public String[][] getMatrix() {
+ ~( C) e* b, ?$ M: A: Z9 Y6 O6 t  return matrix;
/ g7 v% t  g; P  B- A+ u }
9 h# J" Q7 K, B" r/ M}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-13 09:43 , Processed in 0.018422 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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