设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8592|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;+ `/ s% R1 p! a  o/ r
import java.io.BufferedReader;* y: w- ~) j6 k+ V0 _) K
import java.io.FileInputStream;( g/ r: k: f7 F5 A
import java.io.FileNotFoundException;! {" A  T3 F- z. u+ m& z
import java.io.IOException;- l/ Q4 g7 L6 D) i* A8 F
import java.io.InputStreamReader;
7 U. H. [& q) d( ^9 yimport java.io.UnsupportedEncodingException;8 z9 e9 v+ ]% @+ J7 |! c. U
import java.util.StringTokenizer;
1 P/ \2 Z) q% j9 ~$ @public class TXTReader {
$ a, K8 X1 L$ G# W+ X1 |" @- O/ a protected String matrix[][];
  I& Y$ B# A$ O8 d" J protected int xSize;; R% X# z8 d* O% t2 L% x( g+ n
protected int ySize;
4 i* a- a- d! W& K$ y7 I7 d public TXTReader(String sugarFile) {1 P1 x! y- F1 b3 w- h* j0 V& I
  java.io.InputStream stream = null;+ f" Z$ u6 `7 S$ j* Q2 v
  try {
: A& b! r/ K9 T: a   stream = new FileInputStream(sugarFile);* d# y+ @* y3 O1 h' K) f
  } catch (FileNotFoundException e) {' `/ [2 R' T1 p( |0 @0 t
   e.printStackTrace();: j' z1 R  J) d$ b
  }
7 Y; i9 X- q, W3 [+ s/ U  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
( o% m5 h' B( H" U2 \  init(in);8 e$ B! t2 t' Y, G8 D2 V, f3 j
}  q4 Q! |+ k8 v4 ]& Z
private void init(BufferedReader in) {$ f& \! n8 a. f+ i% g( V
  try {
8 ?# X# W; i, ?) G6 Y   String str = in.readLine();( c9 r  {: m6 O) `( e
   if (!str.equals("b2")) {
+ v/ D! T( Z9 T; D' ]2 x    throw new UnsupportedEncodingException(
6 M: E" L& ]6 b. \* I4 p5 Z      "File is not in TXT ascii format");$ A+ q  h7 n0 G- Q# [- r: ?9 m$ d, T
   }
1 ?9 R5 a8 F* @0 y   str = in.readLine();
& Q7 n8 Y) [9 y) m   String tem[] = str.split("[\\t\\s]+");0 J5 j  A+ Z  K' t3 p$ Y; V8 d
   xSize = Integer.valueOf(tem[0]).intValue();
2 Z; J$ x& f; Q2 o; ?   ySize = Integer.valueOf(tem[1]).intValue();
# Y" V3 W' a- _; D: w; G. D5 O   matrix = new String[xSize][ySize];
$ C# b* l5 w% p& l) b   int i = 0;
" o, O5 e7 P2 x4 |  G   str = "";
5 E7 t3 Z0 Z" f( P3 h   String line = in.readLine();6 y2 w8 T7 [7 z% H  S5 U2 X: h% e4 U6 s
   while (line != null) {
' K' w5 ^  l3 e5 |) N0 ?    String temp[] = line.split("[\\t\\s]+");
7 P0 G. v$ N- a' U9 Z+ _    line = in.readLine();
: a: e4 e* J, J6 X- d+ _2 @& H8 ]    for (int j = 0; j < ySize; j++) {  R) O. P. s) m: m4 P4 ~
     matrix[i][j] = temp[j];
) @# K% Q% N* o$ N/ ^    }
/ o* f" k. ]/ r# y0 Q) C2 ~    i++;$ H( o8 [; x9 H/ A1 d9 v. W
   }
& e' j& q) L* j7 u" K   in.close();3 J$ x% W2 s# D. Y8 N
  } catch (IOException ex) {
# K, ?/ e( `3 P" i" c: F; m   System.out.println("Error Reading file");& x8 l2 P( c+ z/ M6 H8 Q. U$ i% }
   ex.printStackTrace();
. b: ?. s5 ?% K1 g0 N9 Q% Y   System.exit(0);# e# W/ F; I4 K. a8 ^1 B- U
  }
( @2 `3 U' X1 G- Z }
2 y5 R7 h; q1 M1 S public String[][] getMatrix() {
% R8 U1 O' B7 d5 S  return matrix;
/ F" t8 j0 H, ?. j3 C! r# [ }
" Q  h9 }- w# X7 r5 u# }  s1 _}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-2 00:20 , Processed in 0.017369 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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