设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8480|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
4 o8 L# x, e. j3 u$ ?! L/ bimport java.io.BufferedReader;
) d! B4 h- A1 w+ C! |0 ^import java.io.FileInputStream;
, g% i7 s* G6 X& |) Y5 C& Rimport java.io.FileNotFoundException;
* l2 C7 T, G% D" @9 I1 L3 yimport java.io.IOException;! H3 ~2 p. ~( `7 }
import java.io.InputStreamReader;( o2 K: |5 \6 v
import java.io.UnsupportedEncodingException;$ I/ Y; p# g, N" ^6 E: L' c8 W
import java.util.StringTokenizer;6 U( O( L9 u1 o( V
public class TXTReader {
& w* M4 S" {3 i, \1 H protected String matrix[][];% R) o3 \% [8 X0 R2 F2 v, D# {; G
protected int xSize;8 ?: C+ _) N8 s  [
protected int ySize;& Q6 z/ L4 z: O% I
public TXTReader(String sugarFile) {  k& O5 ~; t7 c$ X% G4 \/ S7 o
  java.io.InputStream stream = null;
3 X6 p' G. s! q0 c7 `, L* s5 `  try {( i* ]% i; W2 K0 C) e: @
   stream = new FileInputStream(sugarFile);" k3 F6 b2 }3 I
  } catch (FileNotFoundException e) {# E+ c: a  a. g6 M/ V5 _
   e.printStackTrace();* P$ M6 P6 H+ d: O
  }$ c- z6 a" {1 ?4 o8 X* H7 \
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
. c/ _7 o$ o2 ^  init(in);
' Z7 ~7 u6 Q8 t9 c  @) i$ A }
: l6 ]# W# J% F$ S private void init(BufferedReader in) {& N3 }0 V2 v% o9 h, Y7 w. J
  try {
8 @( x9 [1 [+ h, ?) y   String str = in.readLine();
# O6 w, f5 G* J( y1 ~  T' ]   if (!str.equals("b2")) {
# c; ~( K, ?3 ]0 N# |7 Q    throw new UnsupportedEncodingException(
, {! h( \1 Y* m* p      "File is not in TXT ascii format");0 Y. ?/ b( z1 e* d" Z& \; t
   }- l' ?; p: E- a' G# s- A/ h
   str = in.readLine();
1 I8 I  v, S% ^: \# b) M* ?- s   String tem[] = str.split("[\\t\\s]+");# A/ b0 ?, p1 C. h: o
   xSize = Integer.valueOf(tem[0]).intValue();
# ]. ^% P  J" M. F8 H   ySize = Integer.valueOf(tem[1]).intValue();
7 U: Q+ D2 w# L   matrix = new String[xSize][ySize];
7 ^- F: ?$ z& j/ c2 L   int i = 0;
! V# |$ x- b/ W   str = "";
3 m) s! b( [# E* X   String line = in.readLine();- g$ D9 x$ j. e0 z
   while (line != null) {& m8 ~/ f8 h0 T, y# ]9 }
    String temp[] = line.split("[\\t\\s]+");# f5 Q& e3 M# t& f
    line = in.readLine();
" U# s+ E' s3 T7 v# o3 c8 `    for (int j = 0; j < ySize; j++) {/ I3 S4 L& G/ Q/ D" B
     matrix[i][j] = temp[j];. x9 W# Q2 @2 m8 t9 J
    }0 Q* T# s8 N- z2 R2 u2 B  e4 o
    i++;* T8 K2 {$ `% O3 \$ F
   }
0 M: o1 t" F, ~7 m   in.close();
# n! `7 @: r2 e+ j, F5 l  } catch (IOException ex) {) p. I, y' {+ f* _0 b4 o) W
   System.out.println("Error Reading file");
9 L: }; N# T" x+ q   ex.printStackTrace();' f4 [- ?: p% ?: o
   System.exit(0);0 q; g1 ], F/ }2 n( C
  }
4 a* U2 x% z' G+ t  A }9 y4 _2 Y$ q0 J
public String[][] getMatrix() {
6 m4 r, S# k  E; j9 T  return matrix;
% |- \- c; t; x) Z9 J }
1 y7 u5 V+ j# G- a9 Z0 A/ M) W: Q' ]}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-24 22:00 , Processed in 0.018063 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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