设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8960|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;* U  e. \# U) P6 {3 c" M
import java.io.BufferedReader;/ G. D2 p; d2 l
import java.io.FileInputStream;* j& I4 U" t+ Y& T& t! a' `
import java.io.FileNotFoundException;
# }, Z: `! r6 u+ E; I: l0 r4 T, Fimport java.io.IOException;1 ~$ L6 X, ~/ e1 u# c
import java.io.InputStreamReader;
% a# P5 l6 G9 d3 Wimport java.io.UnsupportedEncodingException;4 Z. q  [3 P- e  `
import java.util.StringTokenizer;
! B* L( H! R4 hpublic class TXTReader {; v4 ?$ N6 U1 Y, }7 r
protected String matrix[][];
& W4 i, t4 C/ {* l& w$ {8 _8 H protected int xSize;: g3 ^; Y( x4 ]8 g; \
protected int ySize;
9 S$ f! M. h3 o2 f# S9 R: _ public TXTReader(String sugarFile) {# E2 ?+ d1 B' j, j3 {
  java.io.InputStream stream = null;) U. f4 T# t) s. h
  try {
* w2 }# z  r: J   stream = new FileInputStream(sugarFile);7 i4 X# ^5 R4 v. ?! w7 B$ W
  } catch (FileNotFoundException e) {
% H& y0 E- c& {) I; x   e.printStackTrace();9 X+ T" ?: I# A0 K
  }
: P8 B: ^$ A& a; }. A' |6 i  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
4 [" K# K4 h9 e  b  C4 m* p! t" J  init(in);
2 |& @. v$ \% j( D }* w4 R7 w/ W0 y
private void init(BufferedReader in) {
. ]+ A5 ~- W; Y' m; Y' Q- n  try {) o( l; j' d* W2 q  D% P
   String str = in.readLine();& w' M5 z$ J- H1 d* S! i
   if (!str.equals("b2")) {$ I0 r1 L0 m/ u9 s
    throw new UnsupportedEncodingException(
8 f& O8 R5 j1 m4 X. c2 Q! M      "File is not in TXT ascii format");
! Y$ e- ^7 `  s  R" O   }: V' M! p4 A; \4 h# p
   str = in.readLine();
/ U$ r- C2 |; A   String tem[] = str.split("[\\t\\s]+");6 l' T- \- l1 G# t3 m' U, R) S
   xSize = Integer.valueOf(tem[0]).intValue();
% u. ]$ z( I6 u/ P' C   ySize = Integer.valueOf(tem[1]).intValue();' M# D1 g* g! s$ t2 P
   matrix = new String[xSize][ySize];2 G2 A. o) g/ w* b( c6 Y5 @
   int i = 0;
; d' \  o5 S- Z1 n* D4 ^   str = "";7 o( M* x* Q+ Z8 S
   String line = in.readLine();) q7 t9 R( G0 E$ G% ~
   while (line != null) {% S+ n3 l! p1 r" E- `! z
    String temp[] = line.split("[\\t\\s]+");5 z+ \, O# N6 y/ S
    line = in.readLine();
% `3 \% T8 g0 \4 ^* K& {. S    for (int j = 0; j < ySize; j++) {
1 [; T8 A! a! U! @     matrix[i][j] = temp[j];8 W, u3 u% Q& U2 O. ?
    }# N1 b9 P3 h9 u; \" n# i
    i++;* J) c% }' `' k0 r9 h: p9 I$ |( `
   }
& `, R# A/ w  U   in.close();
3 g6 X4 ?8 S1 N4 l$ W5 ^  B) x  } catch (IOException ex) {% J; U& o  X( {0 F+ Z
   System.out.println("Error Reading file");
" B' `- @3 t5 T: G3 X/ B   ex.printStackTrace();
5 T" z/ M+ q5 B4 V   System.exit(0);
! B8 J% ]% o8 \  p3 j  }
6 w3 }+ k9 h) ? }
( S& C% ], i. G7 C; U public String[][] getMatrix() {
' E2 B/ G: R7 }6 z/ e  return matrix;
' o: Q1 [$ i8 _) S7 C }
+ Y' {# M/ \( r2 ~}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-23 17:34 , Processed in 0.017609 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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