设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10736|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
1 c# _2 o) A* k0 x" \0 }import java.io.BufferedReader;
5 j' `$ Q  E/ r' {/ f; {/ Bimport java.io.FileInputStream;
& v* {; H! B$ {. f6 X6 F3 U. oimport java.io.FileNotFoundException;
3 c; \& R# p$ {import java.io.IOException;7 o: F$ X% ?# b& |* Y  L. U, ~
import java.io.InputStreamReader;
8 y( J' n; |! m; i7 e" dimport java.io.UnsupportedEncodingException;
% z! \( \+ S9 F9 C) r% T& ^import java.util.StringTokenizer;6 ?+ F+ A; r. P" ^9 U, J
public class TXTReader {
+ \) {3 F* `, G0 F% b protected String matrix[][];3 d+ ~! b- s7 {0 w
protected int xSize;
3 F- k$ w; f3 v9 k+ E( a. q7 a protected int ySize;5 Q# o; A0 u( o: q
public TXTReader(String sugarFile) {9 I' f% v, C! ^+ U
  java.io.InputStream stream = null;/ j! t4 J0 X* |# L
  try {
0 a# f$ `& @4 K+ W   stream = new FileInputStream(sugarFile);
. N0 v! ~1 J! C" u- p- z  } catch (FileNotFoundException e) {( _, d+ ?9 X  a
   e.printStackTrace();5 f0 C: C& l' I2 W1 n
  }: c7 ]7 K& s2 g0 J
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));" g2 S8 H7 L* ~* V( X3 U
  init(in);
: B: _/ N& y4 E6 X6 R" a0 I }- j" O7 y0 ~5 E
private void init(BufferedReader in) {
! O2 j: |  H" L  try {
  X- Y* r- f, Y+ o8 i   String str = in.readLine();
2 B# K) X3 P2 N3 R$ c   if (!str.equals("b2")) {% x+ `, h6 ]* H. c2 r5 M% G) {2 `
    throw new UnsupportedEncodingException(
) x) N# b! ]1 b6 z' N9 J      "File is not in TXT ascii format");' C0 {2 d3 N% v
   }
9 V+ {4 {5 g  B9 `   str = in.readLine();
( q: `" i& A% B3 r: k6 B   String tem[] = str.split("[\\t\\s]+");9 u  B9 \8 y4 J
   xSize = Integer.valueOf(tem[0]).intValue();
2 o! |8 s4 A* p! R* o   ySize = Integer.valueOf(tem[1]).intValue();. K$ p$ i$ u% S
   matrix = new String[xSize][ySize];
) X9 R9 m' H+ `1 y# ?   int i = 0;) Z( y1 w) a  |* R; |$ m, x
   str = "";
. E+ W; q7 o( ]6 p1 o5 x( l   String line = in.readLine();
! ?" a5 m5 Y" H5 r2 w2 F# m   while (line != null) {
) S7 k% h6 B' K2 n4 N, ~9 H' |- x    String temp[] = line.split("[\\t\\s]+");
$ Z9 U+ I* V. ]# O$ n4 N" [    line = in.readLine();
& T% V% H* m* g, c    for (int j = 0; j < ySize; j++) {; e" s9 z% d( B* \- h0 @+ G  P5 Q
     matrix[i][j] = temp[j];% I5 x6 a% L- k5 n' F, q0 C
    }% e* Y: F4 |4 S% N1 S3 W
    i++;% l; y9 y3 H' s, O. `2 k6 ~8 ~
   }
3 i* e) K6 S7 m  Z: f5 L   in.close();+ S( t6 X+ E& r; t
  } catch (IOException ex) {
- E9 u% S" g7 F! H4 z   System.out.println("Error Reading file");# m' l  q- n% o, R' y& A, ~
   ex.printStackTrace();% E+ b: m* H7 r& n1 K
   System.exit(0);
) w& W+ Z( [$ m9 \( y" G# u  }
& Y' |- ]( T: j4 o' b# f& W }6 s; m. B) [8 p4 r
public String[][] getMatrix() {; U" P/ Y) @  k: T
  return matrix;( Y4 s6 r& D& K1 p0 T. c: m
}
5 ^4 B. z/ w- |/ n- W6 A# {3 w4 M}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-24 01:22 , Processed in 0.015705 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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