设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6847|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
: X# P, U% Q9 pimport java.io.BufferedReader;
+ R! J! `. `+ {import java.io.FileInputStream;1 }' P1 _0 o  D4 f: z; i+ |, n
import java.io.FileNotFoundException;4 }, _) M3 z2 ^' \7 q
import java.io.IOException;
- G% r- R) \2 Mimport java.io.InputStreamReader;
/ l- g8 K, J: J! ^import java.io.UnsupportedEncodingException;
+ U9 Q; X) @1 e; `- C7 m$ K( `import java.util.StringTokenizer;( z' S7 Y! [5 Y. j$ W9 i7 Z
public class TXTReader {
2 e4 X* W: Z* |3 m. W protected String matrix[][];
  X. i- l! l  a5 B- e protected int xSize;& p4 V- [% e: S6 X" y4 L
protected int ySize;- `9 h0 {( v' F) L
public TXTReader(String sugarFile) {+ w% r  C! F4 F
  java.io.InputStream stream = null;
0 c2 K! K) r, [; q- u% C  try {, K' Q5 h* l$ }* |" t, x. s- n  G
   stream = new FileInputStream(sugarFile);% I1 j5 g$ v4 w* Q
  } catch (FileNotFoundException e) {
" _! R% g8 G* t   e.printStackTrace();, s; y/ N3 @$ T
  }
5 c7 y8 w8 s, Z/ K  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
8 c2 j# a: e7 ~/ z1 |  init(in);5 l2 k! i/ w& H5 T( E6 J; f% h
}
- _' }% w: E+ d' ~( \, v private void init(BufferedReader in) {7 L1 J) @9 ^* Z$ ?
  try {
6 x+ d5 {% T, |7 q8 M   String str = in.readLine();2 X( w, z+ b2 n! M
   if (!str.equals("b2")) {, E  y$ @, q* ~; i- a
    throw new UnsupportedEncodingException(
' i4 H- Y6 e3 S$ Z' p/ O" l8 a6 V+ v      "File is not in TXT ascii format");
* d# W) @( f* y  _7 Y/ S( a   }
9 C3 R5 {. l, ]; I$ E   str = in.readLine();
% u& y  O+ s4 T   String tem[] = str.split("[\\t\\s]+");
; R  a1 S1 _5 t2 ^0 O   xSize = Integer.valueOf(tem[0]).intValue();5 f& ?  k2 e6 G1 G' I, K
   ySize = Integer.valueOf(tem[1]).intValue();0 t: C9 N# W: ~& S/ @3 o- ]; h( u
   matrix = new String[xSize][ySize];: ?2 A8 I  P- v/ s9 X' V' X  b7 n4 W
   int i = 0;; b6 m  n; D9 ~! `
   str = "";1 x0 t) d2 x1 y$ g& p: N
   String line = in.readLine();
% g. h( ^* a2 `. V   while (line != null) {! U& z4 \2 j8 M  [: w! e* g
    String temp[] = line.split("[\\t\\s]+");+ g& n* i* H2 u. o- h
    line = in.readLine();8 L' A  c; n2 S. k! y! L
    for (int j = 0; j < ySize; j++) {% L& i' z4 h- s$ J8 a
     matrix[i][j] = temp[j];
. }/ b5 m; I8 K    }
, b* Y! B" o0 x- v1 L) `* D) B6 J    i++;1 G; C' q; M) i8 V8 `* H
   }
3 R7 o2 W% T2 |* ~6 L( e   in.close();
* V' h3 `. D8 ^+ ~  } catch (IOException ex) {' Q" y, E  l. p1 g9 D3 I
   System.out.println("Error Reading file");
  T% H' `8 @& u/ n   ex.printStackTrace();
6 a8 K8 |8 B) G3 `9 G) ]! z) b6 e   System.exit(0);
( Q4 i- k. d# S  }
& }8 K# U* `& k" p& K$ O }
, R# r- B+ V! S5 j% A4 ?7 ?% } public String[][] getMatrix() {
0 T* }1 P9 P8 o  return matrix;% @2 }8 k& S" g6 j# d; C
}
5 v$ u' u1 ~4 z. z}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-7 09:08 , Processed in 0.016826 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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