设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10685|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;7 V* V) s: d% o9 y  G
import java.io.BufferedReader;
% O5 J7 J/ p' M8 H) Mimport java.io.FileInputStream;0 x9 r4 `* c: C) a
import java.io.FileNotFoundException;3 n1 v: ^; r+ a: y+ s6 m5 q" c, C8 a' n
import java.io.IOException;3 [: ?% |; e& }, e% G% k* e) P" x
import java.io.InputStreamReader;$ V% `1 M! k3 o9 Y. g
import java.io.UnsupportedEncodingException;; m! v" y2 U0 [( I! y$ u3 J
import java.util.StringTokenizer;. Q4 G- f# {, y. h/ X
public class TXTReader {8 b8 J! A% V, \3 j1 l5 p- N
protected String matrix[][];/ f, f: K& P6 \/ [3 N* S
protected int xSize;% G* f$ w% ]& x; {3 k1 ?
protected int ySize;) c/ c. C. r: S! S& {& v4 B
public TXTReader(String sugarFile) {
5 E4 {# B5 s$ |9 Q  java.io.InputStream stream = null;
' ~( u$ i) P" P" i3 r9 [  try {. R0 @: q. _% ?+ Y! s# E2 g' n, a8 o
   stream = new FileInputStream(sugarFile);
  w9 f6 U" \0 P2 ~. A! s  } catch (FileNotFoundException e) {
4 u; A( m  o0 F/ W0 U$ R# K! C   e.printStackTrace();
1 N' k0 Z3 {1 u+ k: m  }
/ W- h6 b" f& x! \  BufferedReader in = new BufferedReader(new InputStreamReader(stream));9 c% v) [/ t' T: \  I9 m
  init(in);
9 p" }+ O* c$ E  q, q6 e& A9 X }# h# n) Z' y- z/ u2 f- I+ t# L4 t
private void init(BufferedReader in) {
9 E$ r- y  H- ]$ n, x- ~4 c  try {
4 A, u0 B6 T0 G, C" u' k   String str = in.readLine();
& y( M! V. P" A( e- `   if (!str.equals("b2")) {
, I; H5 s( W- {    throw new UnsupportedEncodingException(
# m0 u( R' m! d3 J& d      "File is not in TXT ascii format");0 J* P7 S" G- }9 o4 K
   }1 N2 O. H: Y; T  s
   str = in.readLine();
1 ^: \. H7 V6 h   String tem[] = str.split("[\\t\\s]+");
- H; K1 |* e2 J5 N/ i0 u6 \   xSize = Integer.valueOf(tem[0]).intValue();
; `* ]+ u' s+ K; T! Q; D1 ^: S   ySize = Integer.valueOf(tem[1]).intValue();
/ O* ~) c) {% R   matrix = new String[xSize][ySize];
9 N# i+ E; [- j   int i = 0;
' P& X0 M: a- N' _5 |   str = "";
6 [$ S  d4 e. h) q, K% x7 F   String line = in.readLine();. l+ `4 r3 C8 ?
   while (line != null) {( A& Q* G) J7 b- B7 J. [8 ?! V6 D; x
    String temp[] = line.split("[\\t\\s]+");$ E' x2 H2 |; v! w: n
    line = in.readLine();
9 S2 J: {0 v7 Q0 x! c' V    for (int j = 0; j < ySize; j++) {
, w) O; C% H# f# Q( _, G1 l3 ~6 I     matrix[i][j] = temp[j];* |+ [) e6 g7 K% _8 [% S
    }6 W& u+ S7 R1 N, m8 ~
    i++;; I. H6 U! a4 @8 o
   }7 z/ x, r% V3 E4 S/ }
   in.close();5 e9 u/ I" H& T* S$ s
  } catch (IOException ex) {
+ w' ?! T4 `, \7 z   System.out.println("Error Reading file");2 L5 T' L! S2 m7 a" n3 O
   ex.printStackTrace();
% P1 s/ Z3 B, a3 C   System.exit(0);! ?! e$ c! h! ^* h3 B/ @! I
  }7 i+ I! P* [0 f
}
$ k5 ?+ G8 r, F/ B public String[][] getMatrix() {- J! }8 a) O, O$ d* b& [
  return matrix;
( v" }: `( k7 I! z }% B' H" D) Y: D9 {
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-22 02:30 , Processed in 0.015273 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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