设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10613|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
8 @! F1 b  x. O2 ximport java.io.BufferedReader;1 l( h/ O, U% o/ r4 {; @1 V0 l
import java.io.FileInputStream;& B8 `0 D8 \1 r/ ^# ]$ w/ w
import java.io.FileNotFoundException;. Y" j& Z( C& N6 I  p9 I8 ]
import java.io.IOException;
/ X# }$ l. g( f' Q' A; [; cimport java.io.InputStreamReader;3 @1 m9 t& h; B! H, `7 X
import java.io.UnsupportedEncodingException;# ]7 \: J" P6 k* w4 M8 \" X
import java.util.StringTokenizer;' i" B5 t  e3 J; s/ ~) _
public class TXTReader {% O- D0 s( z; P; S- y
protected String matrix[][];
) e6 y$ z. v& M" t protected int xSize;
4 ?* L) D; t" @/ x! X protected int ySize;
# L5 O$ L5 d& b: r! |8 m public TXTReader(String sugarFile) {
5 r* n3 B+ I( a  O2 v  java.io.InputStream stream = null;8 M& R0 ]8 G' y8 p( w
  try {9 H  Z1 ^8 V) e( P+ l: h3 d
   stream = new FileInputStream(sugarFile);- z  z/ x* D7 f- c# M9 n
  } catch (FileNotFoundException e) {
. B) J+ [) M) ?- S! _   e.printStackTrace();+ d) s* [$ J& A% a# {" x
  }1 Q+ v4 X' L; C3 W8 R) F. n/ b
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));4 O& _( Y6 k' U
  init(in);, k3 d! \7 L; k; T! \' c6 ^
}
! F$ h' G: I4 l private void init(BufferedReader in) {7 E% i3 r" B4 k$ o3 V; N8 ~& K
  try {
$ ]/ A5 i3 k) v6 S! p3 Q0 c   String str = in.readLine();: \" V0 a; F' y
   if (!str.equals("b2")) {
1 N# r$ v6 p/ ]) C) O6 C# P    throw new UnsupportedEncodingException(" S0 ]: _" B. e
      "File is not in TXT ascii format");( K. Q) a2 t9 i& @6 ^
   }! J/ A: _4 N# ]
   str = in.readLine();
/ D& W' @4 U2 E7 ~   String tem[] = str.split("[\\t\\s]+");/ z  y  ^0 q9 \
   xSize = Integer.valueOf(tem[0]).intValue();
0 T9 m( d2 }) G   ySize = Integer.valueOf(tem[1]).intValue();
/ F% J0 c) o; g) i( G   matrix = new String[xSize][ySize];6 S$ s- h" l: N
   int i = 0;2 l' }2 }' W* Z1 Q4 [
   str = "";
' G2 C' h2 s1 e2 j5 M/ K! v+ Z   String line = in.readLine();, F5 `) k: \% O
   while (line != null) {( ]5 ~, M; L9 G6 ]" p
    String temp[] = line.split("[\\t\\s]+");0 Z  t: P* h+ P, g
    line = in.readLine();
/ C$ Q, n4 o& t    for (int j = 0; j < ySize; j++) {, b. ^9 g$ X  U
     matrix[i][j] = temp[j];8 P: e9 _% [8 o- F8 z2 o
    }& J' ?/ }  J& ^
    i++;
1 t3 c0 T. n! b2 F   }+ A+ [$ X- `2 E2 |; a# \
   in.close();
; w& B0 R% F+ M8 h1 k; y8 ^  } catch (IOException ex) {: i5 |( U8 s& z% I" Z( {1 \/ O
   System.out.println("Error Reading file");) Q! k1 n/ V- e, p2 ~# B# t
   ex.printStackTrace();
) {+ b9 j4 ~% Z* l6 `   System.exit(0);
# J6 F1 O  D' e; g2 e7 D0 P7 m; D$ @  }. }; A8 z+ [: c
}
% q! ?8 [  l: G' ? public String[][] getMatrix() {- }  L0 d+ R7 T5 Q9 m( S
  return matrix;, M2 P* k; R+ z/ H' \- {
}9 h. V, f0 b' ^$ G
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-19 10:21 , Processed in 0.014427 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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