设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8165|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;- L) x5 l/ i6 L+ q; q. [
import java.io.BufferedReader;( W: E+ V; d7 O1 V2 U/ C
import java.io.FileInputStream;7 b5 o  @: ^' a
import java.io.FileNotFoundException;
# r4 R: S" z3 f9 G, o* Kimport java.io.IOException;) x& {9 p: V7 {* H
import java.io.InputStreamReader;
! D1 O* F+ X+ k0 zimport java.io.UnsupportedEncodingException;6 B' P3 t$ C9 Z2 W# L
import java.util.StringTokenizer;4 i5 j' ?6 I, T; {
public class TXTReader {" Z$ q: g! B) H
protected String matrix[][];
3 L1 T1 |0 {2 J* Q protected int xSize;
4 k2 O; s' S7 r0 q protected int ySize;
: o. c. h3 ^, @8 j1 E& j5 Y8 j public TXTReader(String sugarFile) {9 l" m6 y8 M" \
  java.io.InputStream stream = null;
7 A4 z% ?5 {$ j' o: P8 M' w  try {  m+ U2 X. d+ o3 Y8 c0 d
   stream = new FileInputStream(sugarFile);3 v  u% R( A# o
  } catch (FileNotFoundException e) {
$ k: ~5 V& M, n* }; A: x% `   e.printStackTrace();
# q' o  ?  x6 l9 y  }
; t5 d. k; m: D% [5 n; Z% |  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
& l1 F" X! |. e" W% x  init(in);9 L" n8 _. n+ r- J! d
}0 k" p  l: Y, h* H+ A
private void init(BufferedReader in) {$ Z; d5 c, G' `
  try {
7 h8 B- ~, H9 ~4 b, G7 x   String str = in.readLine();+ r' F. ~. z. l; a
   if (!str.equals("b2")) {5 C+ @) O/ m, c
    throw new UnsupportedEncodingException(0 b1 @3 M) N0 I
      "File is not in TXT ascii format");
. x( W- C- t# y& f( R# |* d# Q& M   }
; o9 k! q0 O. t8 _! y. M, H   str = in.readLine();- G  E0 P/ K5 A) f# t
   String tem[] = str.split("[\\t\\s]+");* S% `! |  s4 a$ x* L7 J
   xSize = Integer.valueOf(tem[0]).intValue();/ {. P: F1 K" ?& s5 u+ S3 Z
   ySize = Integer.valueOf(tem[1]).intValue();
( }# {0 F: q0 t2 {9 F2 V( Z   matrix = new String[xSize][ySize];% Z9 w- P9 z& g+ {
   int i = 0;
$ e  J4 f4 _7 J$ V& A. [   str = "";
. O7 u; h. Z* P7 j   String line = in.readLine();
% c8 W' A# H9 L, j2 b% ~' ?   while (line != null) {
, q8 q% S+ q) R! l) ~, c; a    String temp[] = line.split("[\\t\\s]+");
1 V3 J, U+ h3 L0 u# [& z    line = in.readLine();
$ y! c0 \* J7 ?) s4 K    for (int j = 0; j < ySize; j++) {
# {, C) x! M2 G2 R( X     matrix[i][j] = temp[j];& w* M2 d8 G: \2 D
    }
/ {' [! \2 `  ?    i++;
' o1 U6 q' l+ }7 q4 K   }$ n( m! t# f  V$ V/ |
   in.close();6 U6 F- y  K9 d4 ^: g+ r# V
  } catch (IOException ex) {
; S8 |  X2 Y: |( b   System.out.println("Error Reading file");/ b9 o' ~2 ]: Q6 C; p) C
   ex.printStackTrace();) t, Q6 s; l' ~+ o/ Y
   System.exit(0);& I* _/ Y$ N/ o1 [9 Z+ ~5 P6 _( A
  }5 e- d. U% E7 a: Q% Q4 W' h; T- U
}: [2 ]: k, g4 ~' K/ z9 N  x
public String[][] getMatrix() {9 R2 B  A. x4 ^  h; _* D
  return matrix;0 Z  H; C8 s' a/ k
}1 S$ M) g% F0 K  ?6 k; M1 M! Y
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-6 10:46 , Processed in 0.016934 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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