设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8387|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;. Q- ]' U7 C; h# D
import java.io.BufferedReader;7 b/ ^: ]! U8 ^+ k% v; |% B
import java.io.FileInputStream;2 N, `  u6 o% _1 H
import java.io.FileNotFoundException;
, t1 j1 s& m. bimport java.io.IOException;& a$ e2 Z8 ?. _# A# O' @4 U4 B8 u
import java.io.InputStreamReader;
( p4 l' V2 z: Bimport java.io.UnsupportedEncodingException;: b; h1 i7 m1 F
import java.util.StringTokenizer;( Z6 s- n% b& L. F/ u! I
public class TXTReader {
0 T: j8 B& D1 Y0 W, a protected String matrix[][];
) V$ i1 K* v6 _9 D protected int xSize;+ c  h7 U% j* S5 \1 @3 [1 k
protected int ySize;: x; z7 A6 i( {6 V2 p' u$ [# X
public TXTReader(String sugarFile) {
* V9 |1 O4 l, R, }  java.io.InputStream stream = null;# g# L/ V7 D' k9 K! b3 c
  try {6 e0 r8 M, R# h& l) r' ]
   stream = new FileInputStream(sugarFile);
) D) o5 g) ]1 r/ o  ?' F" i# [  } catch (FileNotFoundException e) {* M2 X: r' o# `- g
   e.printStackTrace();/ u3 ?) O3 v3 r' f$ k
  }5 O# N% |8 \! c. Y1 g9 D
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));; o" o6 s, l0 V- Q& d( G  s- K
  init(in);
7 Z8 h+ s; t$ K4 k8 Y5 a2 K }6 E5 e5 F# k1 X5 [$ j3 `& U
private void init(BufferedReader in) {, q0 y/ U; T6 [9 M% i
  try {
9 p" ?! L6 {: B/ r+ B! I   String str = in.readLine();  y1 M; {/ h! r; E) w4 ?
   if (!str.equals("b2")) {! V- J1 B+ y; ?$ v
    throw new UnsupportedEncodingException(0 a9 p$ P" {% i8 k! k
      "File is not in TXT ascii format");
1 H7 F  R8 ?( l: d* e9 y7 @   }& s* i$ q0 M' i) f1 I
   str = in.readLine();
: J. E" j; J% y% J5 U% M- Z   String tem[] = str.split("[\\t\\s]+");" }) K! h% M0 T- |- l! Q) w
   xSize = Integer.valueOf(tem[0]).intValue();' i+ N4 S% h2 B. ^: _
   ySize = Integer.valueOf(tem[1]).intValue();' `- J# b# r5 r" K' g
   matrix = new String[xSize][ySize];
5 ~+ D# n* N: a   int i = 0;
. i- g4 Q1 ]% O$ F& z) o   str = "";% F6 v# G* S) L! v
   String line = in.readLine();
# y3 j$ y# ~) w2 i   while (line != null) {
( j# X! A2 j  k+ \+ A5 t9 g    String temp[] = line.split("[\\t\\s]+");
) B+ E+ v/ Q: Y, b    line = in.readLine();
" m1 Z2 R% }  P9 b. W    for (int j = 0; j < ySize; j++) {
2 G4 A) u) E# k7 M     matrix[i][j] = temp[j];
0 U5 S# `) f% r3 P' I    }
2 \2 D5 Y; R# C, T    i++;
" G2 x2 [$ m& I( y  E0 p   }, Y/ U3 X% V1 `. h3 O, g5 \; Y) }
   in.close();, e1 e$ R" {# l5 w8 |' X
  } catch (IOException ex) {
9 ^  Q* y) S0 J4 c' H2 X1 S   System.out.println("Error Reading file");& N& v9 O6 w# S6 {* S. \# G
   ex.printStackTrace();
, e9 T+ b) Y6 S   System.exit(0);4 [1 {/ Z) I# K+ v9 U
  }
' O( A" u# l& K2 B: @ }
8 P1 x7 Y2 q% |7 j2 g public String[][] getMatrix() {
& M" P& G1 {# A1 Q! b  return matrix;7 r/ N, r3 X6 y2 D
}- Q8 o. b: m  }
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-19 06:40 , Processed in 0.017537 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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