设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8114|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;; R1 J8 n4 d  `' A0 z0 [
import java.io.BufferedReader;$ Q5 O* q3 l$ g3 F
import java.io.FileInputStream;3 a0 i6 e) L# U0 E* U3 G
import java.io.FileNotFoundException;* D6 g  x7 {2 N2 U& o- z
import java.io.IOException;  ~1 Y& D& i5 K4 T# O- I
import java.io.InputStreamReader;% \1 A( g; W8 D- x- _8 O9 R
import java.io.UnsupportedEncodingException;
4 s1 h: p6 N) A* r, O% R! ^import java.util.StringTokenizer;
4 W' j3 X+ T- X/ \5 `public class TXTReader {
2 e- @. L- k/ E protected String matrix[][];7 j# i5 q$ M, ~! l
protected int xSize;
7 Z9 J' h. d0 {4 u protected int ySize;
3 V6 K" q/ G0 \4 H) `7 G public TXTReader(String sugarFile) {# y8 D1 u( C5 T" H' O
  java.io.InputStream stream = null;
, w; x3 e  p0 h+ _  try {  v8 m+ t6 C% U! R% Y
   stream = new FileInputStream(sugarFile);
, s2 s& x) q4 ]; H! _  } catch (FileNotFoundException e) {' Y! Y/ T  c# }- s, _7 K6 {. f
   e.printStackTrace();* y5 n( y( o& M, R7 r3 p, l6 b
  }' ?" b7 S( p* v7 _2 R) k* w
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
+ h& S0 B3 W2 f! I6 G* Q, q3 f  init(in);
( X# ^& [% R4 U& E }& q. k- e4 m$ h! s: P9 G
private void init(BufferedReader in) {$ f& i7 D  R2 m; {; V" e* b
  try {& E% D: `3 I" m( ~( ]/ R
   String str = in.readLine();* }$ j' {$ T9 z4 |! o* s
   if (!str.equals("b2")) {$ F% r# P  d8 o
    throw new UnsupportedEncodingException(7 m% W7 O, P7 ^( {% }" L
      "File is not in TXT ascii format");
. ~+ V9 h- e0 J) v/ \1 i   }9 Q0 W; c% c6 o. P% i
   str = in.readLine();- d: Z9 n) w. F' x* a
   String tem[] = str.split("[\\t\\s]+");
1 z9 I' `8 l# Z) \   xSize = Integer.valueOf(tem[0]).intValue();
8 |* \2 |& o8 l- g6 H   ySize = Integer.valueOf(tem[1]).intValue();
: b& x& B' w, c) z: N   matrix = new String[xSize][ySize];
+ R* V/ H, r/ f: H# H   int i = 0;0 L" j4 d2 P: c4 T* ^4 M
   str = "";8 L$ \4 ~* P+ R" B4 t
   String line = in.readLine();- s8 L1 T/ X9 j; J( ?- }  j
   while (line != null) {
( @  B/ C2 p+ U: g    String temp[] = line.split("[\\t\\s]+");
# T0 A2 s! r& T" A    line = in.readLine();  O' a% _. Z$ D5 C! j- J" Q
    for (int j = 0; j < ySize; j++) {
: J1 H4 z. e/ t/ y     matrix[i][j] = temp[j];+ C6 l& H# \. H
    }
3 S3 a2 h: |% e  t+ M& k    i++;
6 p4 N% O) K  `( v$ o   }
9 O: \% a5 Q% d- ?5 D   in.close();) f9 J/ X+ e$ d0 K( {+ T
  } catch (IOException ex) {1 t4 q( X3 u1 B! Y
   System.out.println("Error Reading file");! A* @. b6 `; `: B. t
   ex.printStackTrace();
7 e+ _% U- z* w  k( h% j  b( I   System.exit(0);
  f' q/ H/ S, b4 I, B/ Y  }
& k, R! D% `9 \& P2 M' W }
! \1 u4 y- N7 ^+ M public String[][] getMatrix() {
) f+ x# b' V$ F  D% u( t  n, T  return matrix;1 S/ d* t2 A' y% B) {% l' ?
}
' r. C2 n7 U; E0 `, B" H}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-2 23:53 , Processed in 0.021681 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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