设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9626|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
) U' u' j9 q4 O  _import java.io.BufferedReader;
* y0 Q* M3 o+ gimport java.io.FileInputStream;
% ]4 B6 C- @1 R' o( S+ rimport java.io.FileNotFoundException;
; W( m5 D$ M) t" {import java.io.IOException;7 V7 O* f5 {5 k( T) u2 u
import java.io.InputStreamReader;
; u2 i+ c8 t# X. ^7 l# }# Fimport java.io.UnsupportedEncodingException;
8 m0 V" Q% o6 n9 Z& o% nimport java.util.StringTokenizer;8 o+ B- x. z+ L8 l1 E+ f# j
public class TXTReader {
' u0 V$ s3 b4 w) m protected String matrix[][];
: ^7 g) g% H/ d, n: C protected int xSize;/ `4 @& k7 J1 P( t
protected int ySize;
+ i9 w5 [: g! K; n0 ^& j. r6 ` public TXTReader(String sugarFile) {
; u$ l+ [6 u/ W9 N' I0 {; q  F  java.io.InputStream stream = null;
2 F5 f7 l8 \. g2 u: P1 O2 Y9 {0 `  try {
/ U! s( W& X! N, _' i. v   stream = new FileInputStream(sugarFile);% n: x8 e7 j; M& c) v9 {9 n
  } catch (FileNotFoundException e) {
& m& I* O9 m2 j/ o' B   e.printStackTrace();
5 }& |1 }1 T* e$ A  W  }
, Y( Q! R- n& d0 U- O  E" Y" O  BufferedReader in = new BufferedReader(new InputStreamReader(stream));) l  E; h# |1 S* l/ {6 i1 F
  init(in);
; X+ t3 R' \/ V9 t5 X }6 M; a! P# s5 q0 r8 R
private void init(BufferedReader in) {
; o0 A/ u2 j9 v+ d. O  try {
; N8 T7 ]% K0 G" N* v/ d   String str = in.readLine();( Q6 ^* [' j& ?6 t! m
   if (!str.equals("b2")) {
; L# b3 u! O+ P7 i- P    throw new UnsupportedEncodingException(
9 ?0 ~( v$ p- Y& U+ Y! B8 T      "File is not in TXT ascii format");
8 {8 s4 {. I; y( J( e2 _( y   }* [% w8 A, Y' i9 h, S8 a) s$ d
   str = in.readLine();4 I5 Y4 G' h/ j
   String tem[] = str.split("[\\t\\s]+");
- E9 \. S1 y, N" J7 V) x   xSize = Integer.valueOf(tem[0]).intValue();3 ^, B3 y* r2 w
   ySize = Integer.valueOf(tem[1]).intValue();7 L, K, z* }* q
   matrix = new String[xSize][ySize];8 q0 w( J" ^' A, h7 U
   int i = 0;
' a4 t# j8 V% _5 \   str = "";' ]: T: W9 c* t' D7 s
   String line = in.readLine();  o! l$ C1 @4 U  i7 _7 E( a$ L( D4 t
   while (line != null) {
5 U: }: k+ J0 k* X' L! z    String temp[] = line.split("[\\t\\s]+");
$ e( A9 B  f: R: Q    line = in.readLine();
% e& C/ a; H4 O6 Y/ R    for (int j = 0; j < ySize; j++) {
1 @0 D/ @) x# c+ Q& \5 e" {     matrix[i][j] = temp[j];
8 _, B9 ~/ }% _/ n* \  T" u    }+ E3 q# A9 {! `' v; _% o
    i++;7 T( l) f; ^: y
   }
! \, `) e" S. }* c+ H& Z   in.close();% M( N* J* O# m/ v5 K' ^- N
  } catch (IOException ex) {
% [, ]* K6 R7 {# H5 v# |   System.out.println("Error Reading file");
6 }* p9 C4 a+ x  T6 I* W( U   ex.printStackTrace();2 _$ b0 X0 O/ O+ P& J9 Z: M  d
   System.exit(0);4 k- e8 A) D! s; [, N
  }7 f+ V3 j. E) K# w! ]; ?+ S& l) s
}
1 ?7 A) p9 K4 W% T5 R public String[][] getMatrix() {- Q# h0 v2 O- Q0 b* g
  return matrix;0 O* I; l- }# h, I* q' m2 m
}
# x% z9 U) c  L  V}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-28 07:17 , Processed in 0.013838 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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