设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7158|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;5 l4 h  O0 a6 t6 B0 c0 `. Z
import java.io.BufferedReader;
# W4 L2 m+ o9 uimport java.io.FileInputStream;
/ i. I; ?0 \8 s7 Simport java.io.FileNotFoundException;
9 @8 {+ I# {( @' P3 O9 Z( P$ i# ?import java.io.IOException;2 Z9 U- S6 U2 V  y7 |4 b) I* E
import java.io.InputStreamReader;& c2 J" B' u3 Z2 ]: Y
import java.io.UnsupportedEncodingException;4 S; {( {/ N0 w  ^  K) c
import java.util.StringTokenizer;
) k# B2 z1 l, Mpublic class TXTReader {9 K# f0 D! Z3 t0 J- p- T" n
protected String matrix[][];
9 D9 S. [$ r  [9 ` protected int xSize;
( W4 c; ?; I" Y) D9 ~, ~, H protected int ySize;6 o6 b+ p. a2 m! p* k4 P8 H
public TXTReader(String sugarFile) {
% y( |/ ]6 v2 S9 z4 @  java.io.InputStream stream = null;
9 a! E( z  v. ~  try {, c8 w/ w) _9 _" M0 y& j
   stream = new FileInputStream(sugarFile);1 \& U8 p5 [5 G3 l
  } catch (FileNotFoundException e) {
3 O9 X! s; i9 d/ B7 l. E5 \   e.printStackTrace();0 B( |7 H; j9 k& o
  }  ?1 _9 G1 {5 E6 o
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));9 ^, S, i3 O$ {  B/ o8 e
  init(in);/ O2 @/ V" `. [: M6 d: u: u) F% ^
}( _. G9 M6 f, N9 H3 p
private void init(BufferedReader in) {$ b# K; O% t3 W4 t0 b. T
  try {
# z5 U; y% e# ?6 [! u" L% C   String str = in.readLine();
/ R& L3 `$ w6 p   if (!str.equals("b2")) {
7 \8 U2 N3 p, ^2 p9 n% I    throw new UnsupportedEncodingException(
5 y. L  E7 p  Z' v. f      "File is not in TXT ascii format");* O, R4 |  I+ x- x
   }6 Q. f+ W! f' q+ @5 k# f
   str = in.readLine();
2 k& X9 ]0 d: a& s% g5 A* Z   String tem[] = str.split("[\\t\\s]+");, A0 w+ v1 n, f0 N& o. c/ p6 y7 l
   xSize = Integer.valueOf(tem[0]).intValue();
7 V1 `7 v. I# v7 b   ySize = Integer.valueOf(tem[1]).intValue();! v# u, b0 R& f6 t" [
   matrix = new String[xSize][ySize];
; ]( \% N; g! t3 Y" G   int i = 0;
% ]: M0 E* k/ ]0 G- X   str = "";- g, J$ f* `0 N0 D
   String line = in.readLine();8 j) K& N8 _1 B+ w9 v
   while (line != null) {
( _' Y& L! V7 a4 D! r    String temp[] = line.split("[\\t\\s]+");5 X6 s& t% O  s! [
    line = in.readLine();
1 i# S* ^8 B4 X* [2 a9 ]7 P. Q    for (int j = 0; j < ySize; j++) {
  t- v- ~, a: O     matrix[i][j] = temp[j];9 i; @* a* V: O* B
    }
2 J% J2 {9 b" V    i++;: I( r3 {. k  j& B6 k0 t* U* T
   }
. n. P( p& s6 A8 T/ ?# g   in.close();
- R6 v; _; v. J; x. ]1 d) t# s  } catch (IOException ex) {- |0 H: r& S% R8 N- h: x
   System.out.println("Error Reading file");
2 x4 E3 K5 }# S% |+ m8 M4 D   ex.printStackTrace();
% }$ @7 F% O$ r( i% z   System.exit(0);
8 R+ e( X% R3 K( [' p  }* X3 @8 P: R7 k" D
}- |% }9 ]* {) b" r9 u
public String[][] getMatrix() {$ I& P, U( B# d+ ~5 b
  return matrix;
; I! A, W& i; M3 E9 z1 X, [) |' o }. G5 z' ?3 d! M# o. }2 K
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-24 14:05 , Processed in 0.022906 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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