设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9550|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
; Q7 x8 X6 x$ f5 @7 X4 n+ Timport java.io.BufferedReader;
. M  {# m7 h# O( timport java.io.FileInputStream;- }# U5 \* V4 a* W3 [
import java.io.FileNotFoundException;
7 W: C: u* u8 U  b  Rimport java.io.IOException;
4 ]* e" d$ R; E5 ]import java.io.InputStreamReader;
+ L" W! Z1 C0 ?' `import java.io.UnsupportedEncodingException;' a7 [' ?3 `6 o& H4 h/ m- `9 E
import java.util.StringTokenizer;% _+ d, u5 Q  Q- }! d6 s' f
public class TXTReader {
. W0 l$ ]" E1 h$ L( X1 ] protected String matrix[][];& D$ Z  {% [" N) _8 y" h2 @# f
protected int xSize;
: Y0 K: v. X$ O- f9 _+ q; ] protected int ySize;' J% ^1 d5 u& I* a6 h
public TXTReader(String sugarFile) {% R, q4 g' @7 h1 N$ k0 @
  java.io.InputStream stream = null;# e8 `5 R: \8 w! a' ?0 m
  try {# T* p. E0 ^: L/ X7 B' p9 v9 h. }
   stream = new FileInputStream(sugarFile);
( j1 y# @  F1 p5 ~7 {. M  } catch (FileNotFoundException e) {! o: a0 m: \) ?! k5 F9 A( E9 T
   e.printStackTrace();
. g& [- O1 C6 Z/ h! C6 C  }3 ]0 q( g0 w5 J3 t; f" P
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));" |. u# _1 q" p2 m( m1 q
  init(in);# P6 M- M4 e0 r+ i2 O6 k" H
}9 l. Y9 E6 `4 g1 q) `
private void init(BufferedReader in) {/ l# Y; W6 `$ Z4 z8 {
  try {
( ~# U% j: x. X  \( h. D" @! I   String str = in.readLine();* t! B4 k% n( f) j3 j
   if (!str.equals("b2")) {# N) R0 d; q) E: f- U, t
    throw new UnsupportedEncodingException(1 d# O. O( q1 o
      "File is not in TXT ascii format");  k9 a3 ?5 s; @4 q, K
   }
0 \8 ^8 b- x# k5 s  e( K   str = in.readLine();
0 o# w* O5 r' v4 g8 K0 x   String tem[] = str.split("[\\t\\s]+");( M' Q3 C: n: q8 a4 g5 _
   xSize = Integer.valueOf(tem[0]).intValue();2 e$ P' _, @) e: \! c
   ySize = Integer.valueOf(tem[1]).intValue();! f4 w& D$ _$ i8 \" S) i
   matrix = new String[xSize][ySize];
( d  @$ S6 G4 S: H7 X. U" i   int i = 0;
% G  S8 L- Y" Z7 c# y8 T# d( f0 p; W   str = "";
4 c# l' J# u$ L8 Q+ @) j- E   String line = in.readLine();# ~% _& u: [3 A8 c8 j
   while (line != null) {
. R' V) i( n. }  ~    String temp[] = line.split("[\\t\\s]+");5 u4 Y7 Z8 R7 W6 m7 T$ c3 E' ?: T9 z
    line = in.readLine();# h2 }4 }- M; k/ f) K- N+ O- Q" n- F
    for (int j = 0; j < ySize; j++) {# u. v9 m1 J) [
     matrix[i][j] = temp[j];
7 W9 g6 W: {3 Y/ R# j- ?& R    }2 [. F7 y1 h8 b' r: Z- U/ C4 M
    i++;
; K3 t# }7 o6 Q; z3 k) H9 L. g8 ^   }
" l' |' }0 V! Y3 u, N3 m% p+ q   in.close();
; H% V$ Q  u3 k. D- y3 S  } catch (IOException ex) {
9 D: @; g5 {( |   System.out.println("Error Reading file");
/ P- i, y9 h0 r   ex.printStackTrace();- ?* Z% H7 s9 I3 p8 M) H( r- x% V
   System.exit(0);' _7 A8 N+ r9 Z; ]" @" P
  }4 E4 e7 n* E& [" d" a
}
" A0 K' ^4 f4 n. L: p public String[][] getMatrix() {
8 u# @1 t4 W5 Z) A  ?  return matrix;' ^: g5 C" Y$ L; w
}
- V) C1 j( s* G" v  E- F: k) p}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-24 07:44 , Processed in 0.014841 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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