设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7143|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;, r3 F  N1 I+ M# l
import java.io.BufferedReader;
  v. Q% V/ z) b6 R! Himport java.io.FileInputStream;5 O4 d7 W! I  r1 J, b5 Q2 Q, s/ ~6 M
import java.io.FileNotFoundException;2 n! X: U1 p- M, V/ m' p" J
import java.io.IOException;( n+ Z) X: G- N  ~
import java.io.InputStreamReader;2 @  P5 L3 k/ H2 @8 f  b5 n4 z7 O0 b
import java.io.UnsupportedEncodingException;$ W  M9 V* U3 y3 [7 J' n
import java.util.StringTokenizer;
# ]+ D+ V# V. Z+ U* B  R# J9 Gpublic class TXTReader {
. y8 U0 P3 j2 M, s1 j1 f1 ?* c protected String matrix[][];, |) r. }0 X# O# M* C& r. N2 n9 {2 ]2 v
protected int xSize;2 k& V( B6 Y1 @7 C; J8 Q
protected int ySize;
! j! Q, a4 C0 ]4 x/ y+ N3 U public TXTReader(String sugarFile) {
, {" t# n) M# f" v- s# m  java.io.InputStream stream = null;* w, Y0 S: ]/ z" |8 u& a
  try {% P' a0 Y& ~/ w" r
   stream = new FileInputStream(sugarFile);; N* m/ f% S5 Z0 ]: ^
  } catch (FileNotFoundException e) {/ }4 p  A; S8 h, N
   e.printStackTrace();
# x/ E* k, P0 g' g- y4 \' J3 q  }
% C7 _/ G% M5 p  BufferedReader in = new BufferedReader(new InputStreamReader(stream));0 B2 E7 N8 }! o; S& `% b1 D
  init(in);( g$ {% \; z0 ?0 Q) s) K
}6 K4 g, K3 }* K$ V" a
private void init(BufferedReader in) {8 s" _* T6 L# i8 {7 Y  t7 A
  try {
! U2 d+ w" Z8 W, K% g9 K" G1 F   String str = in.readLine();9 A9 C' `1 T! k' v0 C
   if (!str.equals("b2")) {
! t7 F7 \7 X+ Y5 J* [1 F' U    throw new UnsupportedEncodingException(
% j* l& A( x9 c/ c; \! t      "File is not in TXT ascii format");
3 C* ?: e' i, v8 p$ x* J   }+ }# W) E9 u; C) ?) H' l2 @* D
   str = in.readLine();- |+ Q! q5 i$ w/ M' ~; O/ s
   String tem[] = str.split("[\\t\\s]+");! v, L# k- Q8 ]
   xSize = Integer.valueOf(tem[0]).intValue();. f5 R% m  w4 t2 S7 g( [) H
   ySize = Integer.valueOf(tem[1]).intValue();
) @: F% x& x6 D. V   matrix = new String[xSize][ySize];4 z! H& W- q  ^& J7 }0 o
   int i = 0;
: V2 F1 C8 V7 _' J   str = "";% A* v( Z2 O9 }' f! Q  Y# m
   String line = in.readLine();
6 E/ P  f% }8 B9 Q4 |. N3 l   while (line != null) {7 R8 N" I# I* i1 y
    String temp[] = line.split("[\\t\\s]+");
1 H: f6 A/ v9 z  t* P    line = in.readLine();, O9 c+ m' l& C$ P; [6 h; T; |
    for (int j = 0; j < ySize; j++) {
1 Q6 y* W& I5 L8 Q/ e! G- Q% {     matrix[i][j] = temp[j];
  g* B% b. F3 m5 x    }
8 C" i" \" o6 X. Q$ b    i++;
* l0 n; U& g, W9 F' R   }
$ `3 H+ o- |- V, P8 G) E/ j$ W   in.close();( I- {: K2 S; U5 A2 N$ {
  } catch (IOException ex) {
; x, o4 x4 b6 @( ?4 D+ O9 m% \   System.out.println("Error Reading file");
0 b: s# }2 t( {. R2 a  T   ex.printStackTrace();
: W4 O6 P& j2 U, F0 b: X3 y   System.exit(0);
: D+ _7 Q3 Q& u+ @0 M4 R* c- ]  }) l( N7 t7 |8 N
}. J/ l8 |+ ?1 P  i
public String[][] getMatrix() {- x* O* N. N" N
  return matrix;
3 M0 o; n- b# O }' S+ e- \/ o. z( z7 L% \
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-23 13:47 , Processed in 0.022178 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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