设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9925|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
0 r& m! x% E$ ]7 kimport java.io.BufferedReader;8 e. W: f$ a) H( K" M
import java.io.FileInputStream;
, }/ w9 P2 l. z/ ], ~- ximport java.io.FileNotFoundException;$ u/ H0 q: K5 X  `
import java.io.IOException;
* o0 b/ L& [  u; z. iimport java.io.InputStreamReader;
3 z$ P2 y* L7 c5 k9 p- Timport java.io.UnsupportedEncodingException;
9 G' p1 l" G$ a7 Q0 ~+ Gimport java.util.StringTokenizer;
! F: @! B9 H) W2 f. g9 ^public class TXTReader {( I! a3 B4 \- }0 b$ N6 _6 P
protected String matrix[][];; N* M' `5 z: {% {6 S
protected int xSize;/ t  v$ c& I* x- ~! W# M" n4 o
protected int ySize;* j$ j! o. I# E  w
public TXTReader(String sugarFile) {2 U/ q  J6 L+ z! }
  java.io.InputStream stream = null;
3 z2 K9 k1 n: r+ o  try {
+ ^/ h& ~# {, Z   stream = new FileInputStream(sugarFile);
3 H- i7 t( R, w1 s- J/ M  } catch (FileNotFoundException e) {
- f4 t/ r2 H0 O/ p; V   e.printStackTrace();. S! l2 B5 U7 Q5 e6 l' h9 W4 t
  }/ f" ?$ ^0 _7 y" J& O) H
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
( o; M3 W! y9 {7 ]/ L5 t/ @) L4 _  v  init(in);3 f+ c" |) j7 n6 H. \" Z
}: q: ^) s* H, }$ F7 ]% I
private void init(BufferedReader in) {7 h& i# L: T$ i* D/ x& z
  try {
5 B/ d/ \8 A& S) k$ L7 K9 ~   String str = in.readLine();5 F& A5 h! H0 H, D! G
   if (!str.equals("b2")) {/ ?; D, \4 a# U+ m
    throw new UnsupportedEncodingException(0 D; M* R( s9 P5 i% _9 f( Q2 l
      "File is not in TXT ascii format");7 f8 w8 w9 ^+ h' L
   }) U2 J, _( [, i1 Y$ A1 u% ^
   str = in.readLine();
; C2 e4 N* E/ y1 S" [+ q   String tem[] = str.split("[\\t\\s]+");, a( Q( r, |  f9 M/ ~2 j8 T
   xSize = Integer.valueOf(tem[0]).intValue();
& s6 {  A" I; }& E1 W   ySize = Integer.valueOf(tem[1]).intValue();
! w4 [+ X" n4 x/ N; E/ }$ Q   matrix = new String[xSize][ySize];
% b1 _! s* d8 ~* Q- ?; E* i   int i = 0;
9 F3 D- G- ^% Y% N4 B   str = "";
- h1 v" \! l  U, f8 M4 @2 M   String line = in.readLine();, _8 ?! [! T& N6 M) g6 y; W8 S) I6 F
   while (line != null) {3 Y0 q- `3 x, f3 r7 y; j1 y" j4 f
    String temp[] = line.split("[\\t\\s]+");4 b- T0 \* y7 ~! e# K: e
    line = in.readLine();
6 W' O: c. N3 H# m' |: K% q    for (int j = 0; j < ySize; j++) {
. q. p! Y/ c+ Y     matrix[i][j] = temp[j];
% |5 [% ^3 k+ j2 f    }
& r  d4 c- M2 u, T' n% x    i++;" h/ @9 g) s+ [  u( T+ n  {3 L
   }, S( U- F: J  ^, D1 i( P& I
   in.close();
3 m0 y; z0 S/ ^4 {4 Q# g6 X# ]  } catch (IOException ex) {
* h: i4 J7 M; N  C* Y   System.out.println("Error Reading file");
9 L+ `, q# W7 {+ u  q+ y# B   ex.printStackTrace();7 U/ n9 @2 a3 ]2 _- U3 s( T3 E
   System.exit(0);" Z) G3 N( c+ v
  }- U) T& I! q& }* E
}
3 C2 T! E* h% m  e" [# C" M public String[][] getMatrix() {
  V$ l- g7 }# S$ |3 g* ^  return matrix;3 n7 u- }$ I; ^
}
: e3 t: u2 u3 Y7 @}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-11 10:17 , Processed in 0.018643 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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