设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6054|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;: w" |, g$ Z  ~6 t) J! p
import java.io.BufferedReader;
0 S+ J: r: |4 N9 W6 ximport java.io.FileInputStream;/ n+ B8 i$ @' {, |/ C* e
import java.io.FileNotFoundException;
! e. S5 D5 d& E  P% uimport java.io.IOException;
% U1 [4 Q$ `9 B/ X9 `/ B2 Uimport java.io.InputStreamReader;$ x' W& b; f) p$ X# f" p8 k
import java.io.UnsupportedEncodingException;
2 t  j( S% w  f% l! K7 Kimport java.util.StringTokenizer;
8 X3 r2 w& n; @; Npublic class TXTReader {
8 Q/ c. }) D' a/ ~ protected String matrix[][];
+ G1 E! H$ Q4 j5 |) D! N protected int xSize;
; h  D* r3 _) n, Y protected int ySize;% I2 K. F2 j' _7 K0 m& y
public TXTReader(String sugarFile) {
$ I1 \4 V2 m# r  java.io.InputStream stream = null;
% q, U7 W8 E( M  try {
- Z, K  @) ]/ |2 x   stream = new FileInputStream(sugarFile);. u4 v# N5 g3 n' D
  } catch (FileNotFoundException e) {
! B4 u. U( P) ~6 P' t   e.printStackTrace();
7 ?- D* e2 V1 k& I" z9 s2 C  }5 v, a7 A+ }( w: C
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));1 i. n, P3 M: ~
  init(in);
! w9 x) }3 ]. S1 g  Q" `: _ }
$ r! Q$ j# _! M private void init(BufferedReader in) {
7 m' _5 _6 d5 E8 B7 {9 g  try {
& E2 T- u$ x- \( o   String str = in.readLine();
1 _2 k3 ^+ r$ D- r* j* b- n3 _   if (!str.equals("b2")) {0 q; h" h4 q) R! t
    throw new UnsupportedEncodingException(
% [% p8 b* S7 a( X      "File is not in TXT ascii format");
3 R. O0 t' z( V& a! P$ y  z6 C$ b   }
( j6 X. w! t: Z0 z* a   str = in.readLine();
/ [2 c6 G: ?+ o: ]3 k   String tem[] = str.split("[\\t\\s]+");# L5 N- `1 l2 o
   xSize = Integer.valueOf(tem[0]).intValue();
  O' g! I* }2 N   ySize = Integer.valueOf(tem[1]).intValue();
  r, O" j0 l& R  E% A: ?: h   matrix = new String[xSize][ySize];( X6 q7 z9 o. s; G
   int i = 0;
$ L5 D  Q9 K: ?+ l3 F   str = "";4 E% {( d' t8 \6 v8 q# B! S6 A) F9 Z
   String line = in.readLine();' \6 R" t6 p. U# W4 C7 {5 a, K7 H
   while (line != null) {
+ ^* S: q; }$ w. P. L2 i  |    String temp[] = line.split("[\\t\\s]+");
4 [8 O9 k, I, h0 A( V% m& z3 }/ @8 y    line = in.readLine();& ]) |+ z0 B/ D4 N1 j
    for (int j = 0; j < ySize; j++) {
( a# Y, l8 r$ R$ g     matrix[i][j] = temp[j];
7 N" k/ h4 V, K    }
8 f: ~' Z- R6 x2 K5 N5 p    i++;5 ~) E1 {  Z- }5 [1 A
   }! j& {/ V5 p, C5 ^  A
   in.close();& l* Y$ l  F  Z/ F
  } catch (IOException ex) {
  m. i% _) {' h+ o: I. `) w: g# J4 i3 Q   System.out.println("Error Reading file");
8 ^+ \& Y( g0 I   ex.printStackTrace();
1 a0 D; m5 t! y- Y   System.exit(0);
. w! d8 V. m2 W4 \/ _0 b& Q8 k  }
# Z" Z% n/ Z- i. ^, O0 Y* o4 k }( r% l5 J, T, t  u) U! I
public String[][] getMatrix() {
; _; T' n9 d  d( `  return matrix;. O$ s! `5 ^7 Q" R8 |
}
2 R. J$ _* P6 U5 s' A}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-9 00:47 , Processed in 0.020033 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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