设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10328|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
% e$ T" L7 W" O& g* Himport java.io.BufferedReader;
- D( Z' v9 H0 T% L7 F. aimport java.io.FileInputStream;
( t7 I- w( H" y' ~0 v* Aimport java.io.FileNotFoundException;: q5 h! e( F; f6 B) W. L1 o+ O
import java.io.IOException;9 d3 @# z$ V3 W5 j
import java.io.InputStreamReader;
' g# e" L, e/ p: Z8 _. k  Aimport java.io.UnsupportedEncodingException;& M* a1 Q9 V( ?: }
import java.util.StringTokenizer;
/ ]( |, N- f2 n2 D4 l; t  Ypublic class TXTReader {
0 F8 i% B7 w8 W3 D; z0 E protected String matrix[][];9 v) I) I6 A9 ?2 F+ Z
protected int xSize;$ a, b( N& s; j+ E9 H
protected int ySize;3 Z/ b5 X1 e. ^( I0 g
public TXTReader(String sugarFile) {* _; u; h( d# v/ V6 L' [( A2 s2 A
  java.io.InputStream stream = null;) G) F( t- b1 V
  try {$ Z* K8 ?5 o. D% F: j1 y% E
   stream = new FileInputStream(sugarFile);, n  g5 O7 I- I0 y) i6 u. K0 L/ f
  } catch (FileNotFoundException e) {
; f( v3 \: [/ w* L% f   e.printStackTrace();& F# H* Z9 y+ A2 j2 e' M4 x6 U
  }
8 m$ A% K& `8 L9 o4 X! y2 c  BufferedReader in = new BufferedReader(new InputStreamReader(stream));' _  W& f/ E# M% @  h' ]) k  [: `
  init(in);
. E- X& n8 ^) b6 r0 H8 c) E, J# d9 h }
: h7 Y! j; H7 g7 g& U  e5 @5 S8 x' j private void init(BufferedReader in) {6 n6 t, J! n2 V- e& @0 e" F
  try {
: t$ Z) F* _& j: w7 F# F   String str = in.readLine();
/ f0 I. ^8 _# N; ~. `   if (!str.equals("b2")) {6 v/ T5 j  P6 T3 o
    throw new UnsupportedEncodingException(! w5 d- S4 m% u+ f: ]
      "File is not in TXT ascii format");$ C* U% p" w  \+ t
   }$ g2 P1 Z$ ]8 E5 q9 S
   str = in.readLine();
# }* x* z1 U1 R/ z  V) Y8 c( ~. J% [   String tem[] = str.split("[\\t\\s]+");
# {8 F8 Z+ c  q1 J- Z7 [   xSize = Integer.valueOf(tem[0]).intValue();$ {/ h6 F# r, P4 d( d! p# X  i: V
   ySize = Integer.valueOf(tem[1]).intValue();( L& W* |1 |4 n4 J
   matrix = new String[xSize][ySize];
7 v2 U7 I& [  G. R( X   int i = 0;
: s4 K- I' y, K: p4 n5 B" g& O$ G   str = "";
7 \! P) p% G: |, v   String line = in.readLine();
$ I( Q( {# d& G& N0 p   while (line != null) {9 |+ A" X9 r* v
    String temp[] = line.split("[\\t\\s]+");
) s% Y  }- I8 R9 A7 E4 z- [    line = in.readLine();
9 @6 G: H& }/ w6 r# g5 v3 h    for (int j = 0; j < ySize; j++) {
- G% n! ]$ K% U. y     matrix[i][j] = temp[j];
2 r2 J+ h, ]3 ~    }
- e3 U  v$ |& X' W* [    i++;. J  W* S  {- b4 R( E( r  v
   }1 i; R; G. N- _- A0 d$ h
   in.close();
- {, w5 K7 i1 {+ L* V3 x  } catch (IOException ex) {
4 B' f; ^8 |4 Q. N3 K/ d   System.out.println("Error Reading file");; F& g* F8 D1 y3 v% n. O* ]# S
   ex.printStackTrace();
: i* f' a0 {$ A' h+ E  \   System.exit(0);
+ l0 @; B3 u1 T. z  }. S1 R& u2 l8 {5 O
}
. Y9 Y& F3 T  x- r" p3 a; C1 E public String[][] getMatrix() {9 p7 M0 x6 u+ G6 v
  return matrix;
, ~" g. d* ^3 A0 J/ n: H8 R% y$ l }- J; M) h' P  n1 }
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-4 09:00 , Processed in 0.015285 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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