设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8417|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;6 o; I) Q9 F! T
import java.io.BufferedReader;
0 N1 Q' e! v4 Nimport java.io.FileInputStream;* o; p# g$ P5 ]( }  [( H2 |* g
import java.io.FileNotFoundException;9 b$ t" h6 Q! b$ |* Q
import java.io.IOException;' [% x" P" B/ Z$ N( K
import java.io.InputStreamReader;
- \, x/ b7 W$ {0 i. a8 a  ^import java.io.UnsupportedEncodingException;4 q! I* A  R( ?) @+ C8 I/ a# C4 L& d
import java.util.StringTokenizer;' z; {3 K0 L1 b! r; H* g* |3 s
public class TXTReader {
2 H# E% r- r6 u" U4 R protected String matrix[][];
3 H1 S1 M6 W8 K' H# r protected int xSize;
$ J3 H/ ~0 q7 V. [- Z$ j protected int ySize;
6 T6 `/ V) m2 ?! A9 R* d/ [' M public TXTReader(String sugarFile) {
* R% c+ B* ~$ h$ P0 n  java.io.InputStream stream = null;) c5 P+ A) Q- i' Q6 O, I5 t( V
  try {% h: J/ E) O' r6 J, v) V# t' n
   stream = new FileInputStream(sugarFile);
8 b, r) A: K7 B) x# `  } catch (FileNotFoundException e) {: G/ X0 d6 D4 w
   e.printStackTrace();
- H4 v/ M" u# D0 }8 }  }0 `% H) h0 _# a4 m& p
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
' a, ?. L3 Z  q. a# [  init(in);
9 R8 @! F1 ]9 R# a$ u  l }+ f+ J& t4 b9 f% {
private void init(BufferedReader in) {
/ e0 K, J3 [0 {9 P; O  try {
0 M$ y9 M& n# N  [! v& p   String str = in.readLine();
! n$ J) e/ l& V- i7 T   if (!str.equals("b2")) {
% W7 ]. @1 J8 {3 Z+ `7 O    throw new UnsupportedEncodingException(8 l& l  q9 ]# e
      "File is not in TXT ascii format");
, P- D8 `- G7 P* x( B) |, c   }
" d: H! R- d; b9 F   str = in.readLine();& {3 F0 C8 [' H; b3 ]: J
   String tem[] = str.split("[\\t\\s]+");+ u5 m8 t8 j- E0 \
   xSize = Integer.valueOf(tem[0]).intValue();% L1 n% i$ j8 w0 o  U* |
   ySize = Integer.valueOf(tem[1]).intValue();
  `- G6 }" T5 h; s5 B! E. y   matrix = new String[xSize][ySize];
/ a! }/ W$ ?( t" M! v   int i = 0;
& A* H/ `, T6 f6 }1 k   str = "";
% G+ {/ e5 K" p3 H7 \* N   String line = in.readLine();5 n1 V, N* ~; O* `
   while (line != null) {8 c( Z7 p9 h" C3 Q
    String temp[] = line.split("[\\t\\s]+");
) q  k, T7 K6 W  L) U3 a! e; F    line = in.readLine();
1 J1 U% I' }% F0 q+ X8 \3 z    for (int j = 0; j < ySize; j++) {7 Z! `9 J& e2 u: ?1 t4 w  w$ a
     matrix[i][j] = temp[j];
" U8 ?* }0 b+ |# r& w1 A    }0 X" f# Y3 R0 g" ]) f6 {
    i++;
9 F5 S& H# I7 A& x: Q, P  _" I5 {   }
1 @: W* K2 z6 F  |" u0 B4 `8 q# t   in.close();
, \, P' A! n0 W$ Q/ }) ~: y  } catch (IOException ex) {; u6 ?  z0 @0 M
   System.out.println("Error Reading file");5 Y- M) J" V8 V6 G
   ex.printStackTrace();& A6 j: x3 c5 Y1 [) I
   System.exit(0);
% p- Y  Q% w) m( X0 _  F' G  }
3 z8 p# ?3 x+ M1 w* j }6 s0 O6 _- R' k
public String[][] getMatrix() {
- d' h8 g0 }. N8 n  return matrix;
4 O0 |. b9 M& j& C }
6 H* d8 ~: L( h3 t9 A, \  j}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-20 20:01 , Processed in 0.014752 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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