设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10253|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
/ T1 {, v" v4 U; o' ~import java.io.BufferedReader;
+ \, c) n6 B* w# _$ [& @4 ~import java.io.FileInputStream;3 ?4 q/ X* c; b
import java.io.FileNotFoundException;, [3 k5 V( @# g- E; H6 Z
import java.io.IOException;
: c$ R! j$ Z8 Z- `* Oimport java.io.InputStreamReader;( \0 l4 y9 u7 A+ H0 Z% O
import java.io.UnsupportedEncodingException;* |# ~2 |) L3 R# ?$ ]
import java.util.StringTokenizer;5 K7 @  m" U( q) g- x% Y8 D; {
public class TXTReader {
0 F0 S. g. \. P protected String matrix[][];
) {- q2 X* q# C1 [$ C2 B9 \ protected int xSize;6 L4 X) `0 Z: ~( W
protected int ySize;( O" e$ u0 L% `! T
public TXTReader(String sugarFile) {! ^6 d6 z0 H# q* ?0 W) n( X
  java.io.InputStream stream = null;0 `; o  ]  O5 N( t8 b
  try {' z2 D( c1 X  f$ K! u8 h/ ~2 w
   stream = new FileInputStream(sugarFile);( E+ E( H$ g+ ^1 f( t3 @" q2 F# E
  } catch (FileNotFoundException e) {
, D( Z7 t- I1 g   e.printStackTrace();
3 W6 D) O7 e/ S+ g2 q- D8 l  }/ @5 s6 U1 i# J. P: K
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));$ p) o0 w, [1 C5 s$ a1 ]/ E' T
  init(in);% W: m, U' Q1 Q; ]# @0 G
}, ~% f% |' T3 d- m4 u0 }; Y% _1 J$ \
private void init(BufferedReader in) {
) ]. m' x; H- b  try {$ ]3 d4 h7 M& C6 c$ K% r4 m" B% I+ Q0 f
   String str = in.readLine();
: N/ a& s$ p. D" u! ^   if (!str.equals("b2")) {
3 k/ g3 h6 i' i5 L5 l    throw new UnsupportedEncodingException(
# S% Q9 X3 |3 m# A, b" y      "File is not in TXT ascii format");
9 H7 j8 Z& }* j) N+ {   }2 Z8 ^7 x( G4 |1 D/ e
   str = in.readLine();
' N8 u4 n, {& X/ W4 U   String tem[] = str.split("[\\t\\s]+");/ `) S1 V+ i/ C. M9 S  y8 \
   xSize = Integer.valueOf(tem[0]).intValue();
) ]4 S/ [& a6 R5 N4 E( b$ O- V7 e" I+ d! g   ySize = Integer.valueOf(tem[1]).intValue();
  i  }9 L! v+ C* Q   matrix = new String[xSize][ySize];
+ w& y+ _9 f' K. D& }' G1 E, A$ F   int i = 0;  U3 {: h2 T) G: H# S
   str = "";
5 S$ K; i9 {6 B: N& V   String line = in.readLine();" L* @) u6 L7 S+ ]
   while (line != null) {
7 A% c/ ^- E" d. B1 V    String temp[] = line.split("[\\t\\s]+");1 m! x. b9 Y- n3 N$ h; P
    line = in.readLine();
4 M2 {( \7 S4 B$ k" d    for (int j = 0; j < ySize; j++) {
- m) D$ }; r7 W* J1 K* h1 P& U     matrix[i][j] = temp[j];
% O9 N% D" p8 X+ j6 m* S    }
! M7 J0 k# Z  ?. u! O* m( k    i++;$ {! X8 F! S/ c, g7 g4 [0 `/ b
   }
; ~3 ]9 g! a: i: z+ ~   in.close();
" M3 P3 q. o2 B3 [, `7 P. A2 l  } catch (IOException ex) {  e; ^( h, s  F% s) C$ L2 Y2 l4 R7 B
   System.out.println("Error Reading file");
$ e% |7 d, ~, p5 v* z. f, f; m9 P   ex.printStackTrace();
2 a7 [  X9 R3 Z# C   System.exit(0);
9 n7 |& v  V2 I' _4 f" h3 d/ w  }
  P8 m. R9 o& ~& f# i2 W }
+ X1 ]9 Q5 X& _3 y. V- A: \5 W' | public String[][] getMatrix() {
* ~# t. a& S( W/ ]  return matrix;
! l/ v' K7 C6 I% H }# s: U, z9 f# Q  Z" N
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-31 17:14 , Processed in 0.015996 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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