设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10739|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
- N( a, x* t5 A5 z( T8 |import java.io.BufferedReader;/ Q9 f, O: b6 N: p
import java.io.FileInputStream;
* ~& i8 h5 I  o+ `. i% r+ S/ {8 Simport java.io.FileNotFoundException;5 V3 _0 n: s* w' u1 o* V
import java.io.IOException;8 l0 r; \3 X6 j+ u3 ~: P
import java.io.InputStreamReader;
: [5 I8 D7 ^3 a: f. Nimport java.io.UnsupportedEncodingException;
; _* P" H2 t% s4 T  z# s6 @- f- mimport java.util.StringTokenizer;- d3 t+ B( A, l1 I2 N! P7 U- H
public class TXTReader {
1 X7 |" h" g2 v2 s; P protected String matrix[][];% L0 ]6 T+ ]# a7 j& o4 q
protected int xSize;. R# l1 x1 k( K
protected int ySize;& {$ I& t4 \. h) ~
public TXTReader(String sugarFile) {
- c# C6 z$ J( Y; B8 e  k  java.io.InputStream stream = null;
- T$ l0 ^' O+ e" Y& q) `1 @  try {
) S4 K  d# F" x" U6 c3 |   stream = new FileInputStream(sugarFile);5 h" P+ n9 o  A) N' _2 `( h! y5 w
  } catch (FileNotFoundException e) {
$ d6 O) ?' E! @2 I8 H   e.printStackTrace();
2 j! O0 c8 ^+ f2 s  }4 H$ z' r0 }0 G) {, a' T" u
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
2 a  R+ h( Y0 `0 U) Q7 S  init(in);
7 v& i- T& O/ Q. Y# B$ U% \/ a }
5 U5 t" M( d" Q6 Z6 E. ] private void init(BufferedReader in) {
/ o* }1 N( v3 G0 z$ A& M% N6 F  try {, f; M" }! i3 `
   String str = in.readLine();' P; D$ f& A# b& Q
   if (!str.equals("b2")) {
* o+ |: X# J+ n1 B    throw new UnsupportedEncodingException(" H0 n# u5 o" o; K! c5 F* [
      "File is not in TXT ascii format");
8 a. {4 @; \% `' A) L, C4 o7 m0 ?: w0 I   }6 y" O/ n3 o3 r( N
   str = in.readLine();
+ q9 o5 M4 _, ~8 y) X1 t; m# ?   String tem[] = str.split("[\\t\\s]+");
8 E  y3 O) T2 ]3 W* ]- J  n7 g, s   xSize = Integer.valueOf(tem[0]).intValue();
1 j  D" Q7 |0 K- P   ySize = Integer.valueOf(tem[1]).intValue();
5 [! _( o% B! p  P/ O; Q   matrix = new String[xSize][ySize];
9 i' s8 _' J% v( k+ D- N: w) ]   int i = 0;' j) A5 r0 k, a
   str = "";
3 {) C8 M7 p) v: d# V   String line = in.readLine();
, ^, N* O' ?, y5 {6 ~   while (line != null) {+ b( @4 ]4 d! b) Z, R
    String temp[] = line.split("[\\t\\s]+");
0 y, V* m+ e# D0 t6 g" ?    line = in.readLine();9 E6 D3 R! ]$ g- O
    for (int j = 0; j < ySize; j++) {1 ^2 x. Z% h$ |# V2 F+ B8 x' Y
     matrix[i][j] = temp[j];# A; t' O6 f9 z4 i$ @
    }( d  K5 }: w' Z/ r9 N9 h5 ^
    i++;
# X( o: b4 ^( ?, [0 _- `   }
; L# B) @: e) ]   in.close();
2 e& s. A/ q# Z$ z! L  } catch (IOException ex) {
. y4 ^, w$ y, p# O# C9 N   System.out.println("Error Reading file");
) W: l! O0 W3 r: S2 S0 d) C/ L   ex.printStackTrace();
3 z- O0 l% m4 S! `: ?   System.exit(0);
, a5 A9 O) m* E+ p1 \7 Y  P  }* g  A' D# ]1 s4 Y$ I7 ]& P
}
9 O( w- `! P- I3 h; O7 F8 X* @7 U public String[][] getMatrix() {. H5 A6 ]7 m) p5 ]1 l
  return matrix;
# W1 i. m) k9 T4 d2 v5 T }
9 i  t  y# m1 o* z}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-24 07:14 , Processed in 0.015247 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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