设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10107|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
% a# ?9 X# i. p6 l8 @import java.io.BufferedReader;
( \5 g- [+ }2 h& ]import java.io.FileInputStream;5 p3 M+ u- ?4 L$ G: K1 a
import java.io.FileNotFoundException;# s& u1 x) \& t  }% _9 I# n
import java.io.IOException;  r& z/ q* P, n' w$ B- L/ \3 L
import java.io.InputStreamReader;  e" X7 H7 b% t7 L
import java.io.UnsupportedEncodingException;3 |% u/ i$ d* ^4 j
import java.util.StringTokenizer;4 @) e' R3 u$ w) F  _* c+ e3 [
public class TXTReader {
. [/ l& l& i% w; W protected String matrix[][];
  ~7 m1 E* C  V$ d# n! R protected int xSize;
2 b3 o  Y% R/ F7 l: q protected int ySize;* W# d% ]& t3 h* o9 I8 }5 p6 A
public TXTReader(String sugarFile) {3 W9 G2 ~3 s5 r: b( |$ R
  java.io.InputStream stream = null;. Q/ W3 @2 S0 b" ^. m9 B
  try {
1 y+ e8 B; X0 w% r; N  \   stream = new FileInputStream(sugarFile);7 V6 l& H6 J, n) V8 b9 ~, ^
  } catch (FileNotFoundException e) {) S. o+ `$ ^' z9 j' f
   e.printStackTrace();
5 |' }" J. E; P& C# c- @! u  }
4 k* y/ U2 M- u# G+ ~  I, K  @  BufferedReader in = new BufferedReader(new InputStreamReader(stream));' X  U9 A/ L/ U) [+ [5 {
  init(in);
, N% v  S% m$ s" U2 z }1 k8 }, `* v5 k0 `. a9 c
private void init(BufferedReader in) {4 f( w3 D: D: j$ Q  V) Q
  try {7 M- w* O1 P" R2 C9 _& l6 |* k" [
   String str = in.readLine();
, v6 c9 i- e% }3 k( N2 b: ?& a   if (!str.equals("b2")) {
( D8 m% V, C* P6 U8 v    throw new UnsupportedEncodingException(
2 t7 Q- J' r6 j% S" G2 V9 L4 d      "File is not in TXT ascii format");2 E; b8 k' H# ~
   }9 g# k) ?" W& {/ m, L
   str = in.readLine();7 {, @+ e, r3 o: ?- W
   String tem[] = str.split("[\\t\\s]+");( V% K+ h/ E+ C7 g
   xSize = Integer.valueOf(tem[0]).intValue();1 P. v% P8 ~4 s1 z: ?# {+ x! I
   ySize = Integer.valueOf(tem[1]).intValue();/ ^; F; i9 {7 N% ?& i
   matrix = new String[xSize][ySize];8 a, i' R8 Q( O7 }5 m* f
   int i = 0;
  i7 n  V4 r3 a: W( B* X  O   str = "";/ r* j- O+ p2 R" b2 Z- r- k! y
   String line = in.readLine();
- |1 P, Z7 w+ ?8 _$ P, b   while (line != null) {! T1 h; c% c& ?* i
    String temp[] = line.split("[\\t\\s]+");
( |* o" @- U+ z! `    line = in.readLine();  P- p: b) C0 j1 _; q6 ~
    for (int j = 0; j < ySize; j++) {
5 H) ^: f$ w/ t! j4 b4 R' }2 T     matrix[i][j] = temp[j];/ S1 B' b# W6 B- Q
    }+ h' z* {/ T+ U# T+ Z# L9 m5 L
    i++;. F3 G' K9 k1 z& J2 S
   }4 b: R# m! |% L' v: w
   in.close();# z- w6 f2 Q2 f$ a2 H7 J
  } catch (IOException ex) {
1 [' p5 U( ^( B8 m4 t% x7 J   System.out.println("Error Reading file");
+ r* _+ x  V( D+ I1 W. R( W' c; _   ex.printStackTrace();/ g$ N2 `* g# T+ R
   System.exit(0);: ~1 H/ [9 w" S3 Z/ P5 t7 g* e- t
  }
; E: e2 ~" k( I& Q }
8 Q8 E) A1 m* \2 n1 p" h. z public String[][] getMatrix() {
0 G. j* i! E4 a  [  return matrix;
# ?. W/ E" S: S' C6 j0 i }( b3 F2 s# e5 ^) G  [
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-22 22:30 , Processed in 0.022711 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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