设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5258|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
- J$ [1 T/ z" O5 g  @5 t% Qimport java.io.BufferedReader;
" N) ?* o5 g% x. q0 t) R& W# @5 C0 S' Qimport java.io.FileInputStream;- u$ n5 P8 X6 m! t
import java.io.FileNotFoundException;
9 J* j. b0 }$ y: H  y# V! jimport java.io.IOException;. I  U6 E  M) l! N' y
import java.io.InputStreamReader;
$ K( y) i6 }/ T7 ]0 a# R" Wimport java.io.UnsupportedEncodingException;4 d4 R( N$ t: v  p4 ~8 Y
import java.util.StringTokenizer;
& T0 \9 m4 ~9 F# f1 m* Epublic class TXTReader {# W6 e- p! f3 M' S5 p/ E
protected String matrix[][];1 d; q8 U" e" o" U* k
protected int xSize;
% u/ R" I/ G$ s! Y+ v& q' a protected int ySize;
" J; x% O" v& \$ P public TXTReader(String sugarFile) {
1 w6 {9 Z* G6 m9 i8 @  java.io.InputStream stream = null;
4 l% y0 \" q0 L4 Z  try {& Y' F1 ^8 U! h2 K$ F. t
   stream = new FileInputStream(sugarFile);
+ \- v2 r* T/ k5 G  } catch (FileNotFoundException e) {
# w: u' }! v) M, M   e.printStackTrace();+ L: i% i1 a. ~# s5 j7 G" I
  }
4 L& O  j3 c) F5 z4 P  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
; k$ _4 S. d0 m  init(in);
, x( {; O, g* L% ? }) f/ Q$ ?" }! W1 ?" r9 i: s% u
private void init(BufferedReader in) {9 s( g* c+ z4 h5 ~! N/ q
  try {
5 ?! S: B2 x: t5 B   String str = in.readLine();
" O2 T' _6 W& |" o( H; M   if (!str.equals("b2")) {# s) W7 B7 r4 p0 W
    throw new UnsupportedEncodingException(
1 L) W: `! S$ H9 n2 i      "File is not in TXT ascii format");) R* |9 u: @9 O& `8 w8 Z1 i
   }/ y5 o. F+ x& j3 x3 E6 \
   str = in.readLine();% R8 }  E" v; C* t; _
   String tem[] = str.split("[\\t\\s]+");7 t  U3 ]! L! @! o6 K
   xSize = Integer.valueOf(tem[0]).intValue();5 L: s7 R5 E" Z1 p0 {0 m1 n" b/ P
   ySize = Integer.valueOf(tem[1]).intValue();
( ?9 p  E$ Z2 [/ ?9 C8 d3 }$ t: W   matrix = new String[xSize][ySize];0 h/ O! u; I+ P& Q+ O
   int i = 0;
+ I) t, O  D# P( o3 h- }; w   str = "";
& }2 @. o/ l9 A6 V   String line = in.readLine();' n# W. G( M" x& Z+ J% ?8 a" k2 d
   while (line != null) {
# K" I0 |' M  t! T    String temp[] = line.split("[\\t\\s]+");
% K1 [6 p* Z2 y" H8 f1 Y* y    line = in.readLine();
& \: x& c# R9 N) N( f1 }: L: E    for (int j = 0; j < ySize; j++) {
$ ?7 t8 y+ P7 E: `6 g8 u8 Z     matrix[i][j] = temp[j];. k1 g7 e; b! c' G
    }7 {. e, F6 Q3 ^
    i++;: M$ e% {  D2 p9 h# ?
   }
3 K3 \1 _& j  T3 L   in.close();
2 R4 s& [3 c" k7 _/ f  } catch (IOException ex) {
& k/ v+ F9 C9 t6 |+ p) ^/ c   System.out.println("Error Reading file");' r# b- q1 |/ w$ ?6 y8 j
   ex.printStackTrace();
' ]( C& U6 Y8 S% w% W; R8 I5 _2 J   System.exit(0);, V( @- l2 d. t4 `, B! P
  }9 D) d  L  U! v1 Y; A2 Y
}, y. P, f3 k# s8 m+ X
public String[][] getMatrix() {4 ?' _. [8 v) i* o1 u$ ^; `
  return matrix;3 @6 O& T$ `! z( f% {/ q2 n3 w5 j
}
* v" z2 O. k& D; n( h0 I}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-9-18 02:56 , Processed in 0.017612 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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