设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6848|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
. Z4 Q; L7 L5 s6 Simport java.io.BufferedReader;* A$ R% K/ @1 _' i0 W
import java.io.FileInputStream;6 x2 G8 A  m8 U6 {# `
import java.io.FileNotFoundException;/ r4 L" _5 E( F
import java.io.IOException;7 Y. u1 r" N1 O" i
import java.io.InputStreamReader;
; _- y  v) U3 @$ D$ w6 Gimport java.io.UnsupportedEncodingException;
% ^  f3 F+ y" P1 E3 J; L! v5 W- ^import java.util.StringTokenizer;
: r" i+ D; H9 }8 v3 F- r# opublic class TXTReader {
. L3 v, y) j( Z$ }5 b( j2 H protected String matrix[][];
" ~0 v1 }5 s* K1 S! B protected int xSize;
8 ^* L, A- Y. A: [ protected int ySize;  Y- F' P% i# a. p2 M
public TXTReader(String sugarFile) {* Z8 _5 C/ e* Q+ X; L0 X* r
  java.io.InputStream stream = null;. U6 @2 \1 ~  T/ x+ w
  try {+ P  v- p5 F# ^$ Z# ^( _6 \" s
   stream = new FileInputStream(sugarFile);1 _6 d& i; @5 Y; }4 o
  } catch (FileNotFoundException e) {
8 k+ @: h( D) g* t' G5 w& H   e.printStackTrace();- a( J! f' j6 Q! o9 B/ a
  }/ D! h* m9 S- M) M
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));2 P5 Z$ E7 M, m) A% f. b7 X9 [
  init(in);
( d" d7 {9 T9 w1 O+ v }
5 ?6 R8 J0 ^/ J) J4 F private void init(BufferedReader in) {
0 y" Y# H5 V" v- O  try {0 a6 ~# u% L- E( ]
   String str = in.readLine();% F' \, d) U; Z& F' l
   if (!str.equals("b2")) {- n+ v% v+ }( e. o- q
    throw new UnsupportedEncodingException(
9 f- ?3 p# ?3 ]# L  k9 ^  A      "File is not in TXT ascii format");
% K/ W. \& A& x8 w   }
6 B8 {! h7 X8 t5 f   str = in.readLine();
4 @1 J7 q. x) G" u0 n- C' u! I   String tem[] = str.split("[\\t\\s]+");, E/ `- e& b5 U  B4 S" j
   xSize = Integer.valueOf(tem[0]).intValue();
/ y/ c5 W4 C. K. p7 V% l   ySize = Integer.valueOf(tem[1]).intValue();
5 `6 w* y/ F* b3 m$ D2 i   matrix = new String[xSize][ySize];
8 r( k2 L' I! w, n   int i = 0;4 k  i1 a  _% j( c& F9 u
   str = "";  k! i+ R! X6 u) ?. O# q! U
   String line = in.readLine();& y" m+ j& B' W7 Z! u  \( K
   while (line != null) {* v, S9 j% C9 |, c& J
    String temp[] = line.split("[\\t\\s]+");2 T  S4 A- T9 ]5 y1 q
    line = in.readLine();( }* h  n! y& r
    for (int j = 0; j < ySize; j++) {
  }2 S1 H) d8 P9 ?8 p9 k! f# A8 f# w3 i/ K     matrix[i][j] = temp[j];! L' h# J6 P6 [
    }
) y( F, k$ z8 ^) h    i++;! c. H/ {1 b2 R5 a8 m
   }. p" l) E6 k0 Y6 X4 i5 R
   in.close();2 W$ v9 h8 W% V$ k
  } catch (IOException ex) {
' `, L. n: L! h0 n7 q   System.out.println("Error Reading file");
# m% c5 e, H# o' d2 |9 q5 a3 K: x" w- }: m* F   ex.printStackTrace();2 Z9 W! Z: ~* M- W* q4 O7 X9 h% O
   System.exit(0);$ a$ ?2 G, j5 J* V% G
  }
8 j# f! R( s" ?0 A( B }* H6 D6 X3 H# m9 ^, A
public String[][] getMatrix() {( K8 O% n  ^- s7 h6 F; K8 y9 o: m
  return matrix;3 a# `- J9 C+ {* e9 q
}: y' o& ?4 k( V
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-7 10:10 , Processed in 0.028122 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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