设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6417|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
1 I/ `- N7 t" V& Z) Limport java.io.BufferedReader;: T4 }# @, p. l2 `  T
import java.io.FileInputStream;
/ g. ^" r5 L, J1 ~import java.io.FileNotFoundException;7 \. E5 d* Z- _* p% g9 }* c+ i
import java.io.IOException;( A$ R" {7 {% B1 q1 K; Z% k
import java.io.InputStreamReader;
6 j1 F- ?& m% B; y, d; Wimport java.io.UnsupportedEncodingException;9 L- x5 {/ I3 O& n$ l
import java.util.StringTokenizer;- D1 l% @% }% Q$ [) ?, J
public class TXTReader {6 Y: n% U4 W) v
protected String matrix[][];
7 w4 \$ N9 a6 [  k- }2 ?4 @ protected int xSize;$ t& Z) b6 j# b" i4 V+ S( t, u
protected int ySize;
% F2 Q/ V  M5 w5 Q+ x3 V, o7 R1 a public TXTReader(String sugarFile) {! A' y" c" s+ k( S8 C
  java.io.InputStream stream = null;
8 g& D; ?9 Q% s# H. A  try {
; ]# N, A. y8 q; e. w. g% \   stream = new FileInputStream(sugarFile);+ N0 C6 y) @4 j: x: t5 A4 B" b% R
  } catch (FileNotFoundException e) {1 X/ K1 J9 D) _* l) L7 ^: u! f
   e.printStackTrace();6 t: i" a7 v! V' q1 W: Z  E
  }
1 s6 a6 D' h8 q7 {  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
% d( W7 N8 R5 U6 e  init(in);
. I; A$ J5 \' b }6 W. I; F- ]' q4 }
private void init(BufferedReader in) {
* G2 I. d. Q8 H+ m& O0 ^  try {
  s! |* q, i; j1 I4 D/ v   String str = in.readLine();
( d8 v3 B# C' Y2 s. }, V$ v. S   if (!str.equals("b2")) {
* L, m% X4 o) J' b; |9 g    throw new UnsupportedEncodingException(( h, K3 t. @4 {/ ^
      "File is not in TXT ascii format");( c2 [5 g: U' {+ _0 h2 C
   }
3 I+ R3 H% s# C1 n0 A, g3 G   str = in.readLine();
' N; q% k' f& q2 Z! g1 J5 w# W1 z: }   String tem[] = str.split("[\\t\\s]+");
- S. ?* l$ ?8 V  t6 ^   xSize = Integer.valueOf(tem[0]).intValue();
/ o* q) ?$ z$ }0 l* e- b% y   ySize = Integer.valueOf(tem[1]).intValue();
  c1 n5 {; d1 Z, _0 b! D! d) Q   matrix = new String[xSize][ySize];: w- t  h  u+ s. V' R
   int i = 0;+ ?! `# b6 w) d; c4 I9 U& y1 H7 t
   str = "";
% g$ ^2 M% h3 z4 J+ R3 m$ ?   String line = in.readLine();) M' M& f" @  F( T/ d9 i( M
   while (line != null) {
  ^; _# I- I2 e2 k! v    String temp[] = line.split("[\\t\\s]+");
1 e! V% b7 W# u2 Z6 W    line = in.readLine();
. s2 W: B. {' R4 V/ |9 k    for (int j = 0; j < ySize; j++) {' _" @! z$ \1 u6 |( B; h
     matrix[i][j] = temp[j];& |6 S2 A- i! \1 k* [$ Z$ q! z
    }
, _" G( t5 p. \, K4 }4 @    i++;
, V+ j. T" m! L   }$ C& O7 ]' R* V  L8 f: k* d% T! R
   in.close();; \9 y4 y# m1 w
  } catch (IOException ex) {# d. D, |7 R' Y" d' t
   System.out.println("Error Reading file");
) q" h$ ~8 ~% `/ p, U. r! S3 S: |   ex.printStackTrace();2 n% }- S" v0 y
   System.exit(0);
1 s& n0 f! M6 [. D! i/ m  }. x1 e4 z7 T; F0 R3 g
}7 `" L0 t- I+ D, }1 F+ M
public String[][] getMatrix() {$ [/ g( o3 S2 l# J. B( K
  return matrix;
7 w5 ~* V. j' Z% c0 W9 { }/ |& ?$ A5 k& [6 Q5 f6 B' p! K/ j
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-9 02:00 , Processed in 0.014672 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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