设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8467|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;- t; T+ L2 {" h3 f4 q$ A
import java.io.BufferedReader;- `# |$ Y- ^; n$ F( t* g
import java.io.FileInputStream;3 R. v9 B4 A- ^' E( _
import java.io.FileNotFoundException;
% W0 P# Z* t& {6 W/ v5 Iimport java.io.IOException;; b3 y& W1 p( o  g) b( @. p% D1 g
import java.io.InputStreamReader;
- N% M; t% M* p% J3 c. k" I/ I' oimport java.io.UnsupportedEncodingException;
: x9 v* k9 c! W# eimport java.util.StringTokenizer;
! o( f  u5 a8 B5 k4 P! R6 q: Ipublic class TXTReader {
" j4 J. O7 R! d/ B$ G( T protected String matrix[][];
7 s1 O( {, ^4 u$ a. b+ j% f protected int xSize;
3 J- \, o* I+ g  K/ w0 O: h5 q! c* g+ W& c protected int ySize;5 ~! `- s% R5 m9 x( d5 W* a4 D
public TXTReader(String sugarFile) {
; S- d2 K) o/ i: Y) R  java.io.InputStream stream = null;
1 p: z/ x0 h4 n& |6 R4 m% k  try {) K, r0 E% \, G% |9 j+ k% t# k, S
   stream = new FileInputStream(sugarFile);: }2 |( H$ d/ `3 J
  } catch (FileNotFoundException e) {0 Z  g) k( b3 T+ h7 {0 U( L
   e.printStackTrace();6 m) P% N" z1 i: O- N& b9 i3 s
  }, I2 V7 J" K! @4 O/ f
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));5 T, z; p# L7 s! T' ~4 I
  init(in);- R; Q+ [( f5 d4 z
}( `% K# N1 T. \, k7 m: q
private void init(BufferedReader in) {0 {$ n. q) q' x& @" v9 ?! U
  try {8 i1 [: Q! @6 O/ ^
   String str = in.readLine();
! r. I3 J5 P& m6 R- Q( d: I6 C* q   if (!str.equals("b2")) {
0 U% e( E1 Z. F+ C( P    throw new UnsupportedEncodingException(
+ i; [% k$ W: E5 m1 E      "File is not in TXT ascii format");1 H* R+ c4 Q# s$ o4 {; x
   }
, G9 }$ @' [' b6 R   str = in.readLine();
1 C! p  l# _- F; F9 k   String tem[] = str.split("[\\t\\s]+");" M8 ^% w0 j6 d
   xSize = Integer.valueOf(tem[0]).intValue();
# W+ C2 U1 j6 [1 ~   ySize = Integer.valueOf(tem[1]).intValue();2 y+ W8 f- V* f5 h* [
   matrix = new String[xSize][ySize];
- C7 R) N9 K' \, A   int i = 0;
3 D3 I  X5 ^. w, ?9 g- M   str = "";
1 j; q. r0 p! R   String line = in.readLine();( X& o( @* `. y/ }
   while (line != null) {( {' j* P$ J8 S) G9 k
    String temp[] = line.split("[\\t\\s]+");9 u, A8 i6 H5 H! w
    line = in.readLine();; q1 O' r$ T+ j1 c; I" M
    for (int j = 0; j < ySize; j++) {* m9 {- G4 M* `  L( ?0 `
     matrix[i][j] = temp[j];
# y9 s) g0 e% x1 n( |% m( ]6 E    }
2 u# P9 ^" A3 c    i++;
0 |. V8 w" J  F& t% C   }
  C8 g8 y9 Q4 N0 Q   in.close();
( P( d% O) E' f! Z; n- c  } catch (IOException ex) {2 B+ X) ^( G% Q$ \4 k* k
   System.out.println("Error Reading file");
, g! u' o9 {: T+ m% |/ Z. h   ex.printStackTrace();
6 ~* `6 f$ k) T/ D# k   System.exit(0);
, p7 _7 O4 @# H0 c. h% F  }
* H# }& T6 |6 g; ]: N6 L }
, p: m/ z  r" k4 D% h" x public String[][] getMatrix() {3 m) b' E# K: j
  return matrix;7 |, A, r  e9 P( U% n# p
}. B4 b1 l- ?8 Y$ |7 K8 f- ?0 z8 C
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-24 01:52 , Processed in 0.017963 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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