设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6943|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
1 e8 w4 D% {6 q$ `* b# t; aimport java.io.BufferedReader;
5 c& A1 [) P; {+ x2 `6 yimport java.io.FileInputStream;, z  ~( j0 G# s8 H* I7 i2 I
import java.io.FileNotFoundException;/ v  c" J4 g& I6 {1 u5 H
import java.io.IOException;
& n$ m5 D1 d8 ^import java.io.InputStreamReader;: }$ H7 V) v# e: U7 G
import java.io.UnsupportedEncodingException;
0 Y7 A1 z1 a5 k1 u5 @( Aimport java.util.StringTokenizer;
+ o; ]7 I7 Z/ k$ |# H& kpublic class TXTReader {9 A  d  D. a$ ^, o0 U
protected String matrix[][];* J- [- N8 s* J% E2 w  B% @5 x
protected int xSize;) e2 c" L8 @4 }* q/ l# y
protected int ySize;3 _$ o! u6 H% I# B+ r
public TXTReader(String sugarFile) {
' o9 B. i2 p$ o1 ?  |7 e, E. _. U  java.io.InputStream stream = null;
! ?, i) c% Q$ }* g6 z6 k  try {+ U7 E7 ?' E& Y7 Y7 o' v1 w2 q
   stream = new FileInputStream(sugarFile);1 c4 l6 g* e+ n. R- m
  } catch (FileNotFoundException e) {
3 A4 h9 p* @4 e6 f) ]" G' x( B   e.printStackTrace();3 _/ K, x, F2 ]
  }
* ~# s8 c- z# q. }/ T7 p% U' J  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
' `6 g) q" h) g  j2 n% o  S  init(in);. I' s6 Z  x: C1 E" M
}
* h* u8 F0 L( D$ {+ k3 q- b private void init(BufferedReader in) {
& W% c7 ~# K& z3 D, S4 R  try {; H5 E2 ]* x" K# k
   String str = in.readLine();' w) r8 |& ?1 I1 }6 {
   if (!str.equals("b2")) {& W/ o  Y9 l. X. x
    throw new UnsupportedEncodingException(4 M2 j, ?" K9 l) B8 ~& P( u6 w0 T1 p
      "File is not in TXT ascii format");
& E$ b; j# {& y  c! ~   }! D1 N) L# O; x. F  L
   str = in.readLine();, A8 s7 `! e$ C2 B
   String tem[] = str.split("[\\t\\s]+");9 m5 h* l) z- o( e4 n
   xSize = Integer.valueOf(tem[0]).intValue();6 p4 s/ z9 d6 T) j+ x$ A6 b  S+ J: @
   ySize = Integer.valueOf(tem[1]).intValue();
* Z/ t& N8 ~  \2 B   matrix = new String[xSize][ySize];
7 A  m- w2 y) K& ]; W3 y   int i = 0;
, W" w1 @- s0 L' _/ V( @   str = "";3 A* C- D) I, }: [. _! @
   String line = in.readLine();$ Y, V! d) D6 {5 |% Q
   while (line != null) {
* S4 {  X  S) i1 B    String temp[] = line.split("[\\t\\s]+");
1 F  q& C% F( i. A; A3 O    line = in.readLine();
9 ]$ ~( O$ X/ K) U( }    for (int j = 0; j < ySize; j++) {, I" A+ m2 `% T4 P* y% J' C
     matrix[i][j] = temp[j];
& F) H4 f6 B& G, m$ M    }; f/ a4 q6 u3 N% t
    i++;/ E. A- b+ Y: J! V
   }7 t, ~) H  `5 N; T+ u7 ?" m
   in.close();
0 C8 W3 p9 J6 l, w: T  } catch (IOException ex) {
$ u" I1 |5 E+ n* \   System.out.println("Error Reading file");
2 T2 b. [& j+ k: i   ex.printStackTrace();
; u9 y  i! r( n# L   System.exit(0);, {7 N8 M) v5 n$ J9 P# U$ v2 n7 j" C
  }* |% b7 @; ~9 i
}
' [/ K  J# g4 u public String[][] getMatrix() {2 l- x8 ~, p! o$ c8 i+ W
  return matrix;9 K, o  S. C$ c, C  l5 v/ v
}
& t" r: @% m. C! T+ G! J}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-11 18:30 , Processed in 0.015783 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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