设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6268|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;! b0 w1 w+ S% G( w* ]- z' U
import java.io.BufferedReader;9 _( P1 a, g. P3 O+ Q& m/ F7 z1 r
import java.io.FileInputStream;( c! n8 d" P9 ^( i# f
import java.io.FileNotFoundException;: N" v4 H1 ]  c) m. ~
import java.io.IOException;
/ |; y4 h7 z3 O& [. i1 O4 pimport java.io.InputStreamReader;  G+ Y. U" R7 ]. Q9 j
import java.io.UnsupportedEncodingException;
9 ]% S" [7 B, q: O& Simport java.util.StringTokenizer;- G4 i8 ^( x& z9 I' U% D) P
public class TXTReader {: ^7 N0 b) \3 D9 `4 s
protected String matrix[][];. R) J: X% A) P* c! Z( R2 \+ h
protected int xSize;2 b1 l6 H' }! f) M
protected int ySize;$ c( ~, k3 b& i! v
public TXTReader(String sugarFile) {$ l/ H1 M, \0 K# d
  java.io.InputStream stream = null;
$ P- ?6 A, u2 `& ^% h0 e  try {
2 w" w6 J' e; P: l) a: D/ p   stream = new FileInputStream(sugarFile);# P' R4 {$ u! i) @5 P& I
  } catch (FileNotFoundException e) {8 f+ ?( z0 V8 P1 u  G* ~
   e.printStackTrace();
# I6 g1 w6 N& R! _  }
) i& N9 `- Y* M0 s  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
( E' @. x2 y2 l9 z2 l5 [- |9 r- D  init(in);3 c* Y. i( a8 j/ Y
}
! o0 X) O- {) ~2 ~; Z1 e6 e' D private void init(BufferedReader in) {
) R, \: m  n; s5 g# |  try {
& ~, V' _/ d/ ]4 l' F7 v   String str = in.readLine();
1 L  p! A9 S, N/ C' h  y2 f) B" x   if (!str.equals("b2")) {
% v% R! x1 k1 n% S  w    throw new UnsupportedEncodingException(" ^8 c. n3 y! u6 V( N7 C" H
      "File is not in TXT ascii format");! O) R7 q+ [' }. v0 M# c
   }
4 o/ U2 p2 J: g   str = in.readLine();; q9 s8 _9 m3 a: p
   String tem[] = str.split("[\\t\\s]+");# U; Z" `2 ?& y3 q
   xSize = Integer.valueOf(tem[0]).intValue();
3 |3 }; _$ l, h) |1 }   ySize = Integer.valueOf(tem[1]).intValue();
: Z5 u" u+ ?& \) s   matrix = new String[xSize][ySize];+ d, D' e! \" R- L$ F
   int i = 0;
* s$ S' L( ]" J# v   str = "";6 }4 `. J7 V: m2 e- `& Q
   String line = in.readLine();& n3 K0 q+ d; c; _1 |0 H
   while (line != null) {
5 O$ I- @% a3 f% C* h    String temp[] = line.split("[\\t\\s]+");0 m" ^# A8 ]2 e! W7 D
    line = in.readLine();
7 L( |: W0 F2 j# d3 n1 E" B- f% m    for (int j = 0; j < ySize; j++) {
% I5 ?' F7 B, M+ \) ]     matrix[i][j] = temp[j];) u. ]/ h' `# g
    }* u- q/ h' J2 `) i
    i++;/ R* s  ^( e  X0 N! r4 N. B$ }
   }
0 }  }& d  q" h3 g9 g/ x- i   in.close();
( M7 e( i5 a9 G& ]: e  } catch (IOException ex) {
( x3 v3 o+ v6 Y, G0 H   System.out.println("Error Reading file");
; a% [* j5 x* R* l% p1 t! Y   ex.printStackTrace();
' {# @+ r. ~; P6 X8 z; l   System.exit(0);
+ K7 K0 g0 u- D" R1 w" X/ I  }
' X3 e# ~" @+ B. S: }1 X5 `% t( I }/ ?9 f8 a, ~9 B+ ~, w# [7 a
public String[][] getMatrix() {% r0 k: k, j$ f4 ~8 F0 L
  return matrix;
, d! ^1 Y. X) L' ?; l }1 m) V' N# v7 W6 k" U- c/ p
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-21 06:08 , Processed in 0.013201 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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