设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5775|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
% ]* i$ ]+ D# ?$ G6 I0 S" D( ximport java.io.BufferedReader;
5 X; b8 D5 A1 ?6 Simport java.io.FileInputStream;1 l( p( ?4 E; X. `
import java.io.FileNotFoundException;
1 @+ k( w  k& w" w5 ~" uimport java.io.IOException;8 F% U: ?0 u* B; l
import java.io.InputStreamReader;# M8 H5 S  n, ^, R( R. _4 g- Y# l; q: k
import java.io.UnsupportedEncodingException;* V# P9 d  k- B
import java.util.StringTokenizer;) S  r' z" t, G; B7 j+ v: h
public class TXTReader {( Q6 l$ D6 r+ `9 w. K
protected String matrix[][];' |+ a3 q4 ~1 A- H' V2 `: x
protected int xSize;
6 f7 E+ a" c. h- Y4 v: g protected int ySize;5 [. D: u( I' }# d3 ]$ ]
public TXTReader(String sugarFile) {
) n3 e/ I- ?- J6 _: ~. _  java.io.InputStream stream = null;
1 K" ~- ?" p8 U7 e' }' Y  try {2 G: Y" f1 C# r$ @  i; O
   stream = new FileInputStream(sugarFile);
: l3 y* d/ ]% X/ y0 z$ W1 {2 r+ [  } catch (FileNotFoundException e) {
- r' Z, Y& N3 r   e.printStackTrace();
; K, Y- Q1 q7 L. r  }, c: V' _  c# I1 c; E7 L' I2 \# Y* V
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
7 ?# I# |7 g/ Q" R$ V  init(in);
0 s4 [7 P) N4 z! k }
9 L& Y" t! w" ` private void init(BufferedReader in) {
2 m  W3 X- p1 t- E0 }% \; E  try {
* t: t1 R! _4 m+ Y; l! O   String str = in.readLine();
& _  @( i1 X5 ^$ z* `   if (!str.equals("b2")) {
/ J0 v: u. J+ b- G1 P  y4 k    throw new UnsupportedEncodingException(4 V: e: m7 n) N: A+ A* L  C+ E
      "File is not in TXT ascii format");4 a1 @  a  l6 e/ R; Y
   }. f7 F' F1 m; l4 X. y- Z
   str = in.readLine();
2 J$ r' \+ G% V% _; b6 M   String tem[] = str.split("[\\t\\s]+");
1 d- i/ y2 Z6 v- C7 x; [9 H   xSize = Integer.valueOf(tem[0]).intValue();4 q, I; N7 W9 k. W! S7 v
   ySize = Integer.valueOf(tem[1]).intValue();$ ~8 J8 O) H9 g4 H! R% y9 n3 A
   matrix = new String[xSize][ySize];" Y* J2 P; i5 M0 n, O* T4 O2 Y
   int i = 0;! a6 U( i2 R! l# S" x
   str = "";
. D# M& T1 O$ N" n& g( u( X   String line = in.readLine();
9 }. ^  ]- B- s5 ^6 k+ z; k   while (line != null) {
1 v* C% A# c- z9 C    String temp[] = line.split("[\\t\\s]+");4 Z  O* a2 e( @% v7 ?. R
    line = in.readLine();
4 K% X" V- n/ h, }- G    for (int j = 0; j < ySize; j++) {2 ]( p  }  P: {% Z+ d( q& |6 @
     matrix[i][j] = temp[j];0 u4 F" N7 ]- Z& ~
    }% f/ h+ r2 B; n6 F
    i++;# b  d/ M1 v( o. }  j
   }( `4 o% G' a7 M6 p$ l4 C/ H
   in.close();
3 l0 C$ s) s) u; o4 V# x8 _2 A  } catch (IOException ex) {& p* r, J1 c9 z8 [9 d: N" X9 N% Y' A
   System.out.println("Error Reading file");' t$ i' V2 A$ k
   ex.printStackTrace();
7 M  o4 \4 H( ~( ~8 N3 G   System.exit(0);6 b5 A- W5 J/ v. z
  }/ `# R, Z' R" M! s; M& s$ B; C
}
0 N4 ]9 @8 N; w public String[][] getMatrix() {  X$ N2 _( Y) ]& o6 ~) ?
  return matrix;
: p7 n0 O1 Z5 z8 x4 x }8 b& o% x; p2 d5 g: c! Q) ?
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-23 07:23 , Processed in 0.016678 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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