设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9889|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;$ o9 I4 F% Z) |( v$ E, ~8 x
import java.io.BufferedReader;
! D* t7 X) L1 A9 Jimport java.io.FileInputStream;
2 z" ?9 t  k9 K$ A1 z: i" i+ E# Uimport java.io.FileNotFoundException;8 t$ W+ l: A6 ]* ^0 T9 r
import java.io.IOException;
: G) W! C$ C3 x  Q  K8 Jimport java.io.InputStreamReader;8 W& R/ r' ], G
import java.io.UnsupportedEncodingException;
0 \8 P* |+ A  q9 _import java.util.StringTokenizer;# u: M9 z" M! r! ?6 D  S. |; T/ w
public class TXTReader {
- j0 i6 V9 b) L6 j# g' I! z protected String matrix[][];# H2 h4 @, v$ r- c. u9 z
protected int xSize;' B/ E% w% Y1 l% F  a  \
protected int ySize;
6 k* X* Y* A) L9 j public TXTReader(String sugarFile) {
+ }4 w7 Y3 u9 Y$ `% B( m9 |  java.io.InputStream stream = null;
$ F- P/ f' K0 |/ m  try {* ]" Y2 Y" L4 ^
   stream = new FileInputStream(sugarFile);: N" B/ P9 R7 n6 X" g, E; M
  } catch (FileNotFoundException e) {) W. N' E5 E: U& ~; ~- g# G; [) T
   e.printStackTrace();
4 a- C) l2 p1 ~+ {% I( @  }$ Y. H. g4 [! T/ a2 A2 ^
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));2 o7 P! T! S& Y
  init(in);
! U% N! `( U7 O' z }: N$ d8 }- ]2 {  T2 c
private void init(BufferedReader in) {- D  E: a" l+ C& j) r+ a' E
  try {0 |- O/ }1 K8 D; z9 m  n# q
   String str = in.readLine();
" H8 f* v+ s5 j) Z- _   if (!str.equals("b2")) {# m7 G4 a5 [: _4 y% f+ J# U
    throw new UnsupportedEncodingException(9 M& e7 `! m$ `" A- A6 M$ x2 s
      "File is not in TXT ascii format");4 E# ^- D! }+ p: ~4 Z2 K
   }& [& u" c2 n6 \. I* d
   str = in.readLine();/ u# n3 L& i, z) Z
   String tem[] = str.split("[\\t\\s]+");8 g( l( a6 n& p+ P
   xSize = Integer.valueOf(tem[0]).intValue();
6 x" E# `: d) L8 P/ h/ M   ySize = Integer.valueOf(tem[1]).intValue();6 \( Y+ m) S: B( V7 D
   matrix = new String[xSize][ySize];
/ _7 h- }$ s' f: V) q5 U   int i = 0;5 z: J6 ]5 O8 }8 U  w
   str = "";, j$ B3 F. l1 A; F: u3 F
   String line = in.readLine();+ ]8 w4 V5 R& ^' q
   while (line != null) {
$ c  j0 N) i, L    String temp[] = line.split("[\\t\\s]+");
! P6 n6 \# U+ e3 Z& B    line = in.readLine();
- U: a$ l( @) _. D7 W; I# P    for (int j = 0; j < ySize; j++) {7 X! U- I! B: B) t
     matrix[i][j] = temp[j];0 L+ u: @- s. F1 E) b8 ^& h4 W
    }) b3 i+ p' X8 \7 f% K
    i++;
$ [, \( E1 o7 X' Q  c) z9 Z   }7 B( o7 N+ e2 W% A
   in.close();
9 [, T% ]; C6 j3 @6 ]. F  } catch (IOException ex) {
+ M. O  u  {* `$ z, c% v# T9 O   System.out.println("Error Reading file");
  w" O+ `/ e9 d9 d$ O   ex.printStackTrace();( Z' r( K1 b2 O1 o
   System.exit(0);3 U& Y! M, [. Z& z3 Y
  }
! H  x% X/ @4 [" t  r2 ?8 e; E }7 j3 z& c0 o. V
public String[][] getMatrix() {5 H( q' a! N. O
  return matrix;3 ^3 ~' w& u: t6 M6 z
}
- C% f% B' ]2 k2 V6 b}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-9 11:45 , Processed in 0.013784 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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