设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9819|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
$ x6 @5 H; p/ Q' c, K2 L3 z$ pimport java.io.BufferedReader;
# G% Q. P2 ~3 o$ ~, K  _import java.io.FileInputStream;
) ^  ]/ ~0 F& s8 ]6 w1 Rimport java.io.FileNotFoundException;
! v- K3 A/ W- q. X. {import java.io.IOException;
8 q& R+ X: X" b' cimport java.io.InputStreamReader;% b0 m  u, d: w6 B* O& n) x
import java.io.UnsupportedEncodingException;
0 _1 C+ Q. Z9 F8 _' Fimport java.util.StringTokenizer;
! U! {) G/ ]& l' f6 Lpublic class TXTReader {
5 g3 ?* {1 w8 p protected String matrix[][];! R6 P! X3 n6 Z: W" X
protected int xSize;) H7 s6 q# U' T0 J' G0 o; r
protected int ySize;1 n9 x* J+ T& ?- e+ }9 S; b7 m
public TXTReader(String sugarFile) {9 P# m3 w1 E/ x! ]# T
  java.io.InputStream stream = null;3 r2 T/ j1 [- e( w1 G& H) H& k: `
  try {+ H/ X* x( O" R. p; v  m
   stream = new FileInputStream(sugarFile);
7 U0 ]2 `' _. y4 D) Z9 w" R6 a  } catch (FileNotFoundException e) {
' F/ X( M+ m4 ]   e.printStackTrace();: }( ?- t. b6 P
  }. f8 i0 N% G8 r: }( P# Z$ d! a" w! d! Z
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));4 p7 N( W, q! |- {% t- F
  init(in);3 V# j: Y& x9 i$ u
}
2 n: U$ ]' ^0 L8 \& | private void init(BufferedReader in) {
6 o' u) k) t& ]* Z$ L$ Z  try {
" E9 W* u9 _" [8 U8 f   String str = in.readLine();
% X3 m8 f4 d; [   if (!str.equals("b2")) {
( D5 f' U9 R" }4 f% y/ d* V- }    throw new UnsupportedEncodingException(  j/ t# R% Y+ V- n3 F+ \7 K: k
      "File is not in TXT ascii format");- R5 G7 v0 |8 Z6 z" L- W3 ?+ D
   }2 R6 C' h3 b/ I& W
   str = in.readLine();4 s1 X) {, G7 `
   String tem[] = str.split("[\\t\\s]+");
+ }2 j' d, r2 I  L7 p& v   xSize = Integer.valueOf(tem[0]).intValue();
& Q, s- Q( Q( F/ c7 w( V   ySize = Integer.valueOf(tem[1]).intValue();( n, S; ?9 K- x1 P5 ^( d; m+ W
   matrix = new String[xSize][ySize];
6 x! h9 z& v* A+ j3 C   int i = 0;2 b8 t  z& J9 b: v1 O5 _
   str = "";
9 f  g# N/ w$ B: B2 [9 {- ~/ y; X   String line = in.readLine();  ^% E9 t; w. s4 t
   while (line != null) {
, o7 U% A) F! J$ E$ t$ a    String temp[] = line.split("[\\t\\s]+");
- v5 ~7 ~# M5 ^    line = in.readLine();
/ w! F; B. ]4 F7 F1 l* B    for (int j = 0; j < ySize; j++) {
* [) T' q3 s1 ?     matrix[i][j] = temp[j];/ h  h+ p9 Z# E9 T
    }
6 W- S, p( L2 w8 @: W    i++;
& j6 g% B, ]: ^) @/ |   }0 W; T" ]: Y  [% t/ G
   in.close();
5 `" Y1 O. r  B8 P# Y! R! X. }1 Y  } catch (IOException ex) {
; M) g- }2 c% h- }4 [- p   System.out.println("Error Reading file");+ V3 r! |3 g9 i& F, j9 m
   ex.printStackTrace();
; K3 M  {. b9 C3 n% J- g7 z) I   System.exit(0);5 i' U$ x/ _. C# }0 v
  }; t  q/ ^6 }! G
}
* w+ ?5 o$ n0 [0 d/ i public String[][] getMatrix() {7 O: C2 }' H* o$ X; Y* ?
  return matrix;7 [3 D+ F: s8 ]7 |/ u" N% L
}5 B  [8 y% O9 g! f
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-5 18:25 , Processed in 0.017324 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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