设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7124|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
6 j& ~6 m9 I( {& y' k6 [import java.io.BufferedReader;
8 X: E- y3 g: W" r0 gimport java.io.FileInputStream;
  R1 W+ T0 X0 A2 C8 pimport java.io.FileNotFoundException;
, t0 k1 O, K: t/ zimport java.io.IOException;
: A7 |/ S8 P% u, J2 z  R( jimport java.io.InputStreamReader;
' q9 p5 P( I9 {8 B, ]! Z. Nimport java.io.UnsupportedEncodingException;
/ g( k1 U% D" A/ N0 N/ bimport java.util.StringTokenizer;  Z" i& F# w  [/ U
public class TXTReader {
6 M" @' n& o' U$ M$ Y; g" Z/ X+ m protected String matrix[][];; T2 U/ u  s2 G
protected int xSize;, E* u( }: |( w% g  V
protected int ySize;: m# n8 T+ n+ y) \% \5 ~
public TXTReader(String sugarFile) {8 F& ?* _; R: r# A
  java.io.InputStream stream = null;
6 b0 j0 a: R9 u* U; P  try {
8 i- }+ t8 z' g   stream = new FileInputStream(sugarFile);
. u$ d! b( R; D  } catch (FileNotFoundException e) {
( w7 a7 g$ B; c( d! Q   e.printStackTrace();
( C9 M. n; H  @" I. c: R  }/ ~9 n' y; S- Z$ t& x) A9 H
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
: A* O. N; v* H4 U- b  init(in);
+ m5 _* m  N" X$ O* T }
6 h/ P7 D& E9 v" K8 ^ private void init(BufferedReader in) {
8 q. |6 N* I/ F# G0 V( D  try {6 L4 f, x0 M; [7 `' X
   String str = in.readLine();
" l0 A, }2 Z# e6 C3 g   if (!str.equals("b2")) {
1 O' k# j8 [  A+ H! p6 Z+ X    throw new UnsupportedEncodingException($ j, U- _) _) R
      "File is not in TXT ascii format");
! H) F3 b9 C' c% m! J% @   }
0 j- a3 V0 j6 d' Z' {1 t   str = in.readLine();
+ u; \, u" R( ?   String tem[] = str.split("[\\t\\s]+");4 B) y4 X3 C$ p
   xSize = Integer.valueOf(tem[0]).intValue();" _) @  j( [6 t& R
   ySize = Integer.valueOf(tem[1]).intValue();
% U) j1 z! R7 O; n6 S0 R. h7 J   matrix = new String[xSize][ySize];  h. p9 L& }& [& G
   int i = 0;
4 E/ j- V! n8 W- I1 k) c   str = "";
* y; v9 V2 _+ F- O: P, f   String line = in.readLine();* J/ s1 R$ ?8 u3 b7 G% _
   while (line != null) {/ _5 V1 i' f" e, [* ~; H4 M8 H
    String temp[] = line.split("[\\t\\s]+");7 v8 D3 W( p1 x
    line = in.readLine();
9 W- n4 }4 @8 m( K* r    for (int j = 0; j < ySize; j++) {9 `2 c6 Q: \5 D! Q; K* p' l* V" A
     matrix[i][j] = temp[j];* Y# F+ X6 @3 L! |2 ^# G. A
    }
& ~2 i/ U8 Q  J$ w  l" B0 e    i++;* }, r: }$ l6 r" Q9 D/ S/ K; A0 w$ v
   }
; H* R& T: Z% m9 t' b& C   in.close();
. l& z, \* W  I! n  n7 o  } catch (IOException ex) {
) c( C% G) ?" q: d5 a9 R' k- E5 h   System.out.println("Error Reading file");* N/ a' [" q) W* G! [$ ]
   ex.printStackTrace();
( H; l3 K: s& ~, {# c! p   System.exit(0);
% P! t, c7 \) [9 K8 K  }
% p- c4 f2 M( B9 t9 |" _' N }
- B8 v6 N8 p$ o" ~ public String[][] getMatrix() {, X: M) m2 E. {: _4 H8 k( g6 D
  return matrix;$ ?+ Q1 L# a1 N3 k3 D
}
6 R3 d3 Y! `* ^7 {+ N% w}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-22 12:32 , Processed in 0.016326 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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