设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7651|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
2 O' V7 |1 o, x7 C' _* `2 Qimport java.io.BufferedReader;
; L* Z! K+ i( A2 g. cimport java.io.FileInputStream;
' e5 e  R" x( p# u2 ?9 rimport java.io.FileNotFoundException;" r5 [: |2 e0 q1 R3 M- y
import java.io.IOException;1 J4 M8 A5 Y" P* A3 K& L. @0 W! F
import java.io.InputStreamReader;
. a  I3 E4 S6 m2 E0 k: T- C0 simport java.io.UnsupportedEncodingException;
& y9 H/ z" ?" G$ `+ ^7 a8 ^6 Iimport java.util.StringTokenizer;
/ N& K6 ^# t" p6 i$ ^public class TXTReader {6 L  I" t6 D5 x2 @
protected String matrix[][];
0 |/ _: ?  n" _, q; X7 T& J protected int xSize;
. W/ D. V3 k9 O, t% l# u# ~) O2 C protected int ySize;
4 M8 w2 x! Q' E& O; C public TXTReader(String sugarFile) {* L  P- p; r* K/ @2 n( _
  java.io.InputStream stream = null;
, K' V6 z7 W1 r, o7 _) A  try {
+ Q/ d. J, a6 v5 f$ U. x* E5 {   stream = new FileInputStream(sugarFile);5 [3 q3 R/ }5 K5 V
  } catch (FileNotFoundException e) {
# J* T! u2 \- C) ~, k   e.printStackTrace();
9 e# _* y7 u7 t' k; l5 }* `; q  }3 K, N2 J2 L$ a, g# o4 s
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
/ V) ?+ H; \& s' t  init(in);9 Z- P& ]- Z, U/ x2 O! |
}
. F1 U) i( |% l private void init(BufferedReader in) {
) o, g! f8 Z  [9 _  try {
/ g6 Z" i7 i" k   String str = in.readLine();
2 y$ x+ a' o, P5 j6 a* u5 a. ~& }   if (!str.equals("b2")) {
6 s1 F, x7 j* K, [    throw new UnsupportedEncodingException(
. Y. K) |: u) _- W+ o1 i7 t      "File is not in TXT ascii format");2 M7 s! \. G( z' b- p' f* {
   }
9 g' A6 l$ L0 Z( B( h' Q+ _   str = in.readLine();1 A5 o* }, ]% ~, u
   String tem[] = str.split("[\\t\\s]+");
5 q) p' F( C, ]6 j  f   xSize = Integer.valueOf(tem[0]).intValue();
7 I1 `0 y9 p- y   ySize = Integer.valueOf(tem[1]).intValue();2 X7 w- d0 e/ x0 \
   matrix = new String[xSize][ySize];
3 _9 m  p' \% `: C5 D$ B. ~   int i = 0;
! [5 O: @+ X( `- I2 P; w   str = "";0 w: ^2 x' O2 A* l- b
   String line = in.readLine();' i$ ]1 [( q1 e/ t) J1 Z/ d
   while (line != null) {
! Q; ?  F+ `" }* a! `3 o/ ?    String temp[] = line.split("[\\t\\s]+");
: n" ~  I/ `: k* ?    line = in.readLine();
9 l1 S# Q( s# k; g# w+ `    for (int j = 0; j < ySize; j++) {
& B% [3 z1 ~: M; s( E     matrix[i][j] = temp[j];3 H  E; {- J/ K* b* ^
    }4 c4 ]$ G$ j% e% x% [' A% n
    i++;  y- h2 p  B1 k
   }" b# u8 {( o  C$ e
   in.close();
+ H% Z& M- A" m  } catch (IOException ex) {
+ Z" ~3 ]* T+ m' ~4 M* B" U: J   System.out.println("Error Reading file");8 ]6 G, N$ U; }2 b' J+ |, n" k; P8 g# ^
   ex.printStackTrace();1 Z  U/ V; m0 ~7 f) r: Q/ _
   System.exit(0);7 d2 @8 o. t5 p$ t  N6 C
  }
" K1 x# k. y0 i" a  c }1 M; K& K& F' c! ?( `4 k0 n
public String[][] getMatrix() {
7 a. J6 W! _5 m7 {% Q& m) q* a& B. \  return matrix;
1 V/ Y; e  M3 T- p( A9 U. @# w }: t2 o. y6 l9 \! G* z
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-4-4 09:16 , Processed in 0.020676 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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