设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10437|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
/ w$ `0 a: u  q/ jimport java.io.BufferedReader;# r  f* |6 m2 m
import java.io.FileInputStream;
' g9 T, [4 Z& d, w; d+ w1 D% P1 timport java.io.FileNotFoundException;
- E0 N: \: t! limport java.io.IOException;. v9 j& g- p  s0 l1 k; c& E
import java.io.InputStreamReader;
9 I. p0 X  N9 ?9 limport java.io.UnsupportedEncodingException;7 f. j) K+ j! {- i# |" n
import java.util.StringTokenizer;
/ ^$ L; |! S; Z! Ipublic class TXTReader {
, n' u5 {2 a. {8 w7 v1 M# C protected String matrix[][];
" I1 j5 S! j7 e; ]* k& s protected int xSize;. o. e& [9 M+ G: ]8 S3 G! y
protected int ySize;4 Q3 I9 ~1 v1 w3 E! q
public TXTReader(String sugarFile) {0 _  B* N. p* E5 K
  java.io.InputStream stream = null;
$ R: h" s3 T+ Q+ k  try {
  d6 [2 H  X! K) k" i   stream = new FileInputStream(sugarFile);9 Y5 k5 d6 |% z- N1 e
  } catch (FileNotFoundException e) {
6 {8 T9 @" ]( M; T8 q   e.printStackTrace();
* ]$ @& q1 @( D# e$ G, p+ i  }% b1 V( A6 Y3 ]# N0 t2 e+ f# `
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
* n+ Q0 j3 S$ c1 ]. ^. q  init(in);' r. A6 O6 a; r1 @0 M; r
}
' ?) B* Y6 l. T' y6 i+ \ private void init(BufferedReader in) {" J' c0 h& }$ R3 x) Z. E; p
  try {# |0 `# ]+ R# n5 E- V7 a% ?! j
   String str = in.readLine();
( d3 j8 x9 I5 n: ^   if (!str.equals("b2")) {8 m4 }% b) x; P3 O; x  L' N
    throw new UnsupportedEncodingException(
. O; a* {1 Y+ M) c* u3 T) q      "File is not in TXT ascii format");
/ m& F3 k% I* D5 S1 u9 \$ ?. u, W4 ]   }/ |+ d* w0 T( |& n
   str = in.readLine();0 A5 f) G9 ?5 P% l! z  ]+ i+ a! x, M4 \
   String tem[] = str.split("[\\t\\s]+");) I$ b- Z% n) |+ G
   xSize = Integer.valueOf(tem[0]).intValue();
; y- g0 P7 r, h( X' ?" C5 F   ySize = Integer.valueOf(tem[1]).intValue();: {; H, `- c$ g
   matrix = new String[xSize][ySize];/ ~7 l! z& [! Y2 m1 h. m: i
   int i = 0;& O, g. R7 t! r7 S1 [
   str = "";
' f  {' M( g  S2 z' K0 K) i   String line = in.readLine();) w/ Q* u# ^* u0 [- j
   while (line != null) {
& ^% X% f$ |' x* L4 Z    String temp[] = line.split("[\\t\\s]+");* e9 ]0 M8 ~) c: \
    line = in.readLine();
+ v( w1 J7 T$ _. z0 H    for (int j = 0; j < ySize; j++) {
5 s: Z0 E; W4 J6 c6 h: e     matrix[i][j] = temp[j];: t$ s) l5 o4 U  n" s" k/ l
    }
8 ?8 F' u. k" n7 V. k    i++;( h* D! T+ o4 `  N+ {
   }
0 {' D0 I% v# f4 T   in.close();
: U5 m+ h- A& f/ S# y9 o  } catch (IOException ex) {
# ^0 X! N; p/ A6 t. |   System.out.println("Error Reading file");
3 o" C, X1 F+ F+ c. r   ex.printStackTrace();
: M6 n% C/ l* h& [  e9 S   System.exit(0);6 X. h# r; I! m* H0 e0 f
  }* Y/ Z* o5 l. n# w
}/ \7 w1 f5 [: \" g2 z
public String[][] getMatrix() {* Z" L, m; m. s, n  M. ~
  return matrix;& ^  a  E# n: j6 V
}% @0 c3 }. e3 I
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-10 10:46 , Processed in 0.020930 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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