设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10034|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
4 X$ y7 A+ I5 k" W! gimport java.io.BufferedReader;/ W6 W+ G5 M% Y. u4 a+ U1 b
import java.io.FileInputStream;; ~3 ^4 T3 \7 ], Q+ H
import java.io.FileNotFoundException;5 \8 g' ~. g+ j/ e' a( t% Y$ F* N
import java.io.IOException;
9 Q5 D; n" J, Pimport java.io.InputStreamReader;
" k1 ]2 k% U( K5 simport java.io.UnsupportedEncodingException;
+ k* V; {# v- D& m9 y9 C8 v& q1 Rimport java.util.StringTokenizer;( N1 K* H: G! A% I# u: t
public class TXTReader {
0 l$ k5 g/ H2 A) _! ^ protected String matrix[][];
& b6 s7 s. Y* @. f7 Y protected int xSize;& O# k2 _! T4 r- |+ ~! T7 W
protected int ySize;
+ ]6 a* w. @, P: b$ f0 n public TXTReader(String sugarFile) {, }: x3 J- M8 t
  java.io.InputStream stream = null;, D  [+ d( _- g- h) ~9 Y# P
  try {
# q! a& j( l$ l( t; D   stream = new FileInputStream(sugarFile);
; d8 s4 m) a7 [. Q: W+ w  } catch (FileNotFoundException e) {% ?& d3 d5 i* l; z
   e.printStackTrace();! a0 _# b! i: ^5 a8 T
  }
! H; O/ V, F7 @' C  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
7 [. I' r: L$ ~8 C% [! ~  init(in);# m; @1 ~# u' S' ~
}
" m5 \; M( h' _; W  L* J( D private void init(BufferedReader in) {
! h' M; C8 u2 p: O  p( E& s  U  try {0 e' N" A& X8 @+ m
   String str = in.readLine();
7 \2 Q- E# @3 `) w1 _; \   if (!str.equals("b2")) {
- _; D' x8 C9 U# I/ G+ P    throw new UnsupportedEncodingException(
+ e8 s7 d  Z/ X6 v4 h( ~/ o4 `3 L      "File is not in TXT ascii format");% l& {, x# Q5 {
   }
2 W4 }8 ~5 U% a' b! K$ s   str = in.readLine();# C3 Z' Z# H$ t$ z4 G, p
   String tem[] = str.split("[\\t\\s]+");
+ U/ }0 \' m* h   xSize = Integer.valueOf(tem[0]).intValue();
, l3 v8 J  }9 S( p   ySize = Integer.valueOf(tem[1]).intValue();
$ p* H4 J4 [7 o2 v( c   matrix = new String[xSize][ySize];
# S" i- ?: H0 [9 c   int i = 0;
* I! f; v6 P8 ^+ J& z% e   str = "";
0 [. A6 c4 B, h% w   String line = in.readLine();
% l% x7 f3 H3 p1 T   while (line != null) {8 H# s. d+ s1 ]- g( k- X9 x
    String temp[] = line.split("[\\t\\s]+");
2 x# U. h6 j! l    line = in.readLine();
$ d8 }; O0 T, J& j7 a5 J; M! f- B* ^    for (int j = 0; j < ySize; j++) {
) e5 R9 z3 s2 i5 @- X# X4 n     matrix[i][j] = temp[j];
6 U) t% M3 j8 \! y& E/ j    }7 S( q7 I8 S  ~% r  ~" j- O& ]
    i++;9 Q9 |% n, _$ y2 A8 X
   }
# x' y* m; V) y9 Q/ n0 U: a" i   in.close();
' h4 k1 ^+ M& o6 t; J( {  } catch (IOException ex) {
( E; f# |* B/ P  C$ k' F4 b   System.out.println("Error Reading file");
8 b2 z0 C$ \" n- G1 N- q7 Z) U  R   ex.printStackTrace();
0 U9 k0 p! B- z1 W+ X   System.exit(0);
" D3 r. L; u. D# \  p6 T! T* C- d  }( `2 F; W( M5 ?
}/ e5 l! t# K$ e) k% X' p
public String[][] getMatrix() {
) i# b2 X( a9 D6 G  return matrix;
3 v. B4 M$ o! R3 n: r }  J0 k+ h3 z: v
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-18 01:24 , Processed in 0.015374 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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