设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9249|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;# i3 N6 z" g% }) m
import java.io.BufferedReader;7 r  d- N7 s0 p! J
import java.io.FileInputStream;9 f& H) }" V4 S6 r
import java.io.FileNotFoundException;
# ^  I# p9 X' u4 g, F1 b  limport java.io.IOException;* ]" Y7 }3 e% v4 w, q% \
import java.io.InputStreamReader;
( x# L" o  y1 U( J( Ximport java.io.UnsupportedEncodingException;1 N; k. G+ F# G8 e) ]  c
import java.util.StringTokenizer;9 w; l- t9 y! c0 _: a6 A$ M7 }
public class TXTReader {
- J  K+ {& r# s8 N' F1 E% @ protected String matrix[][];
2 l: W1 I% y: t" Z/ h protected int xSize;
6 k' `3 B8 y# N, E4 c0 E7 J3 | protected int ySize;
1 |: W' \3 J! O8 U: O public TXTReader(String sugarFile) {* {  b( l  O- W. L3 _( T- [
  java.io.InputStream stream = null;0 k& G2 P( K6 r8 J' E
  try {
" C8 y8 [! K9 E   stream = new FileInputStream(sugarFile);# R% t, |4 o" o6 b. d
  } catch (FileNotFoundException e) {
! e' \; @. Z5 _3 a   e.printStackTrace();$ Y- ~% Q, y- v7 f8 f" @$ Y
  }$ ~( ?3 ]/ J# b  {# R1 h; ]0 _/ A
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));$ i7 v& i% J, O) [, t$ @& Z
  init(in);
, q# v' r. `( G$ ^ }
! V! T! n' _7 Y/ U/ B3 H private void init(BufferedReader in) {
. _: \0 S7 Y4 I$ k$ y/ w' s  try {
  q3 l5 P1 n9 G6 u% f   String str = in.readLine();
' _3 l5 Q+ ?0 y1 x! I# W   if (!str.equals("b2")) {
7 \' \. Q; C' g, ~' H    throw new UnsupportedEncodingException(: l! \4 R" m& O! q/ N
      "File is not in TXT ascii format");3 Q7 R5 R$ @1 F8 F% |. X$ _- n( J
   }) m/ A. o, p% u
   str = in.readLine();; s$ @- i7 F+ ~" o3 i
   String tem[] = str.split("[\\t\\s]+");* X  V8 r2 Y! ]* M
   xSize = Integer.valueOf(tem[0]).intValue();7 t  r& f5 [; ?# w, }- e
   ySize = Integer.valueOf(tem[1]).intValue();
5 M! ]; k$ F* M& H/ p) L   matrix = new String[xSize][ySize];" s6 y0 U2 m! e# \! S% h
   int i = 0;
/ f1 S7 V9 d  k1 k   str = "";3 f5 l3 J- }5 D, _" `5 f  O
   String line = in.readLine();
" y1 v& f# f$ L4 a   while (line != null) {* a: K+ B; k% M" W( ?* x
    String temp[] = line.split("[\\t\\s]+");) v9 N; l' s: O6 r# A; ]+ W1 p, q
    line = in.readLine();0 r" v# ]# w9 v2 c
    for (int j = 0; j < ySize; j++) {- \6 K7 t7 }( f6 D4 u0 J* U6 X; W
     matrix[i][j] = temp[j];* K' K. J8 v6 {. {) o( B  U: N
    }
/ H5 {& f/ ~7 Q2 A4 K  w& i    i++;
0 z9 S0 z- ?5 Y8 C% Y# @/ }* A   }
. O2 z$ r4 u' v5 m$ A# \0 A( A0 {   in.close();
& k. P5 a' M7 d$ n  } catch (IOException ex) {
# k1 K9 ]/ T" O% m   System.out.println("Error Reading file");
/ c( K* s' x; y0 S   ex.printStackTrace();0 e: ^  z  V5 g" N& U
   System.exit(0);0 Y- h+ \, p( ~/ y( D6 O
  }- E. I, _" n+ U8 m, x/ a% I
}  w) `# f( ^( L# E5 J
public String[][] getMatrix() {
9 [/ Z3 ~8 m6 O( q  return matrix;& U4 L) Z8 v* K- D, l
}+ H9 H' A5 @; w% n
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-10 23:14 , Processed in 0.013310 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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