设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10469|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
9 `" x+ d7 [, ]; himport java.io.BufferedReader;6 x6 b- z6 T# P' P0 a% }' ^$ u! I% n) Q
import java.io.FileInputStream;
4 N. T; @# S% ^1 N8 `8 ]import java.io.FileNotFoundException;/ D/ H8 u6 g% S9 F
import java.io.IOException;1 z- @  @0 X% o4 |. i' Y9 ]
import java.io.InputStreamReader;/ {7 C  b: A: }: Q5 D6 E% o, I
import java.io.UnsupportedEncodingException;
1 I7 K& y# n( n. t% ^  q2 uimport java.util.StringTokenizer;
( P4 K: p, l% c5 p4 kpublic class TXTReader {
5 I& Y3 G6 F: t protected String matrix[][];  ^; C0 W! u3 @8 m
protected int xSize;& @; g* f9 A# q  }
protected int ySize;" T# S* \3 f/ G- i+ Y4 R
public TXTReader(String sugarFile) {
+ f5 X3 V) p1 n1 h. e8 ]9 f' V  java.io.InputStream stream = null;+ R8 [! w" K2 S$ W3 x7 L
  try {
* q6 w/ Y; w( A   stream = new FileInputStream(sugarFile);1 ^2 B. H" A+ Y
  } catch (FileNotFoundException e) {; y: \4 u$ k& y$ @: I# v
   e.printStackTrace();
) V6 E! c% }& H5 K  }
$ O0 N4 ]# {. `  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
. Z- j1 G9 n* n+ k: P) I5 C; J% z  init(in);
$ T  _! N# O& ?: R }& k; z% O+ P( @, y' j  k
private void init(BufferedReader in) {) N- o" p. _6 I% m1 v1 I
  try {* P9 l7 A- s! |$ s
   String str = in.readLine();
! E5 I/ k1 g/ y: |6 A   if (!str.equals("b2")) {
9 d2 p' Z2 [8 E# K: f/ n% M* q    throw new UnsupportedEncodingException(
1 _/ K! e; S7 E! m8 M9 I' D      "File is not in TXT ascii format");
8 A1 D$ e+ S3 }  V2 h5 o   }
. B7 l6 u; o* I3 ~  d9 b   str = in.readLine();
" X- m5 |6 T) @$ `) l  @* t- Q   String tem[] = str.split("[\\t\\s]+");
0 i# [( C# l9 P& r5 U( r   xSize = Integer.valueOf(tem[0]).intValue();
2 E& M) _0 B: |# e1 a   ySize = Integer.valueOf(tem[1]).intValue();: A* u4 X9 a4 S, w* z" m: a
   matrix = new String[xSize][ySize];
8 t  x4 G" Y+ P/ v3 I   int i = 0;# z) [6 X; Y4 ~+ ?8 c  w% N
   str = "";) N9 n5 g$ {% y, R! C6 x
   String line = in.readLine();6 p/ G0 H5 y( |; n8 p1 b
   while (line != null) {, D+ L' S$ ~  g9 _
    String temp[] = line.split("[\\t\\s]+");
  Z! {" h2 J5 G    line = in.readLine();5 G; x# k6 @5 O  R  m/ _4 Q
    for (int j = 0; j < ySize; j++) {8 Y  r8 C9 v  Y! k$ ^# `3 R
     matrix[i][j] = temp[j];
8 g1 \' \1 J: a; V! C# z/ o    }
, w6 x( y9 C: @$ [/ c    i++;6 j. J& d/ x( z
   }
. Y8 X* i1 z# @4 S6 {6 N- s9 q4 a; I$ x   in.close();( S$ n& j$ v+ x
  } catch (IOException ex) {. w* v. j4 C. f4 b! I$ E" k& m
   System.out.println("Error Reading file");
2 _" b7 Q/ F! d( j   ex.printStackTrace();2 }2 y& H6 B+ y2 F0 g6 h3 \7 F
   System.exit(0);
# X7 `! B: t+ I- C* H, {  }
: F! }/ ]9 ]' ?* A }. o* a  o- P" N2 S
public String[][] getMatrix() {
2 f: d8 R3 o4 O& M  return matrix;
) G+ J: s7 l, j& B, Y( } }
& q9 I3 ~7 `6 X  t}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-12 07:22 , Processed in 0.018120 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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