设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10225|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
* k0 I! v+ L1 i  s$ e" `  gimport java.io.BufferedReader;8 z2 x1 ]! m/ j; c: Q& W2 F
import java.io.FileInputStream;; Y% s7 u; s3 F; P" X$ Y* z
import java.io.FileNotFoundException;! M9 s, M2 ]. c8 d* x
import java.io.IOException;# n: f% A3 i0 w  \+ K# j6 o+ F
import java.io.InputStreamReader;
% c) k$ h+ o2 h# o" h+ Vimport java.io.UnsupportedEncodingException;
6 f" o+ K; a% w" w% e$ uimport java.util.StringTokenizer;! V) I  {, P- @4 ?3 X
public class TXTReader {
2 \. b, Q* k- B/ U7 q8 ?+ X3 g protected String matrix[][];" J" y7 y9 ~' F% G2 J
protected int xSize;5 w9 F9 N$ E3 u8 f# O% f. k: s
protected int ySize;
8 v4 A4 K2 `* Y4 d( L public TXTReader(String sugarFile) {
6 b9 S. Z! s" c9 o5 t* y# |  java.io.InputStream stream = null;' B( h4 A4 b0 M" @! y. F
  try {; L/ x( ~9 I& o& }0 l* P1 N
   stream = new FileInputStream(sugarFile);
. v0 H. |! T5 A6 O" y  } catch (FileNotFoundException e) {
! K* y: i) o% n" E5 L& T   e.printStackTrace();
" g. I$ \) `6 d+ J+ j( M  }
/ S+ P0 L+ j$ I  A; U6 z  BufferedReader in = new BufferedReader(new InputStreamReader(stream));9 k8 F0 w! S( U" R
  init(in);0 B5 V2 n* l' Y/ D" r. _" D# n
}" o5 ~. j5 w; S
private void init(BufferedReader in) {& J; Y# e  N, }& k6 [
  try {+ I5 K$ D4 W6 l
   String str = in.readLine();
* x. i3 N" [/ Q9 o* B   if (!str.equals("b2")) {
) o' [( `0 k) W( S0 G* I; u    throw new UnsupportedEncodingException(2 V: j" A! Y/ T+ B. o
      "File is not in TXT ascii format");
" s4 h" a, |' z4 U0 S8 Q" ]( q/ a   }/ V9 ~6 S& O# Y" m+ X
   str = in.readLine();6 \# |3 W2 ^* `/ q' C8 I' I+ k
   String tem[] = str.split("[\\t\\s]+");
- ?# J. o% q" f9 l# y   xSize = Integer.valueOf(tem[0]).intValue();* S0 h1 Q# N3 |9 Z5 t
   ySize = Integer.valueOf(tem[1]).intValue();& U' J9 ?0 _* G5 t
   matrix = new String[xSize][ySize];. `& d) `. e/ M7 F
   int i = 0;
7 {+ U8 v) Z# Y# `! N. k   str = "";
7 k  A. c" v7 W2 D  W# D' o   String line = in.readLine();
+ F6 h% N3 j8 F  `' F/ w   while (line != null) {
5 R" R4 x: X5 [" ]1 R8 K, g+ {    String temp[] = line.split("[\\t\\s]+");$ ^) l' _" J, L
    line = in.readLine();
; A' l* S0 ?$ F! r1 N; R    for (int j = 0; j < ySize; j++) {* `2 L7 D9 d* Z. z
     matrix[i][j] = temp[j];6 z& k( X+ j+ Z7 n8 A
    }. ^' j' O" W/ z! ^
    i++;
3 ]9 _  q! D$ d, c   }
0 {! R' r. Z2 A   in.close();
: S, n6 l8 H2 O1 k0 q  @( l: d  } catch (IOException ex) {/ }1 \9 L9 a7 O. t; \  j* g
   System.out.println("Error Reading file");
2 Q" a" Q$ o; E: L, b   ex.printStackTrace();
) s* i- V0 v5 H   System.exit(0);- ?5 I! \0 _+ s8 j' P
  }
" j3 W& |( }: Q# f }
) ?  c; V- F% y, _8 f  C, r6 e* B public String[][] getMatrix() {
+ T7 A1 G. e% Z+ R  U  return matrix;
& k! t  y  d9 i  F+ _% C/ D }! E% x5 ]& j' q9 s* @+ y+ a
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-29 19:38 , Processed in 0.016058 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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