设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7662|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;% f5 g- {- s8 u( O1 S
import java.io.BufferedReader;
0 T2 x1 Q! V* R4 {5 T" k/ R( zimport java.io.FileInputStream;
- p5 q( u: x4 \4 {import java.io.FileNotFoundException;
& C( U% L+ ^, V7 s! Z0 V4 C8 iimport java.io.IOException;
( C  }, {8 x# `  L1 s- _/ uimport java.io.InputStreamReader;
6 z" a" A( i: s+ ]% J7 w, g9 J2 e0 e* eimport java.io.UnsupportedEncodingException;9 P1 w$ R' W7 c. J  j# ~) C
import java.util.StringTokenizer;. E9 V, U( v& A4 o: U
public class TXTReader {4 M, ?1 M9 S% `2 \4 {5 c, H, J
protected String matrix[][];
2 y7 |+ u4 `! l protected int xSize;0 Y5 G% h4 Z5 [7 v/ k' I
protected int ySize;8 K1 J" ^- r+ a' T" i& r3 R/ H: {# q
public TXTReader(String sugarFile) {
1 @2 m! X6 C! @8 \$ C$ f  java.io.InputStream stream = null;
* [7 V# u* [$ C6 [, S8 _- U  ^  try {% G6 D5 `3 E2 [/ z8 W6 y
   stream = new FileInputStream(sugarFile);
$ r9 D; V" g) H, w) F7 F  } catch (FileNotFoundException e) {
2 A+ l. j/ c8 o& m( n5 E   e.printStackTrace();  A/ k' h4 J) e# Q
  }
+ A: a& [  H! ~  Q+ n  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
- T4 e- K8 k  ?, ]4 D0 Y9 J3 Y  init(in);
, ?; G/ D  {" s: t  v }
& b; M" Y& S9 z0 W) ^ private void init(BufferedReader in) {
8 M) e( A( d$ V5 ?' E  try {
) M  i0 k4 W1 k   String str = in.readLine();
7 v6 V9 }5 k0 ^1 s4 N$ }, Z$ U! G   if (!str.equals("b2")) {' F6 v' D! y) `5 w
    throw new UnsupportedEncodingException(1 G: i, ?, g- }! @
      "File is not in TXT ascii format");
8 s) z6 ~8 D1 r* p, N   }$ }; p# ^( {! P% Q( D+ W: B' A
   str = in.readLine();
( h' k6 }+ s$ y. m6 I. t- `   String tem[] = str.split("[\\t\\s]+");, L! O1 ?- t( n9 Z  P
   xSize = Integer.valueOf(tem[0]).intValue();" T& M' @$ r; R. P6 S- b4 y5 s
   ySize = Integer.valueOf(tem[1]).intValue();
: m( w' S5 Y9 Z1 c   matrix = new String[xSize][ySize];& d6 h, R5 J% e! P% f! s2 \
   int i = 0;
, ~; W2 `  c) m9 q7 A% g+ D   str = "";
; o7 X- p( u0 r( x5 s2 j$ T   String line = in.readLine();
0 ^6 D% j* A& Q# M' ^4 L1 [1 s   while (line != null) {
; F% f" z7 m( F; u! ]- K    String temp[] = line.split("[\\t\\s]+");
6 M( J4 |' s& G    line = in.readLine();! v9 h/ N1 {- H0 j
    for (int j = 0; j < ySize; j++) {" E7 T7 f$ X/ m3 ~0 I( w# k: B. X
     matrix[i][j] = temp[j];
, e: a% p: C9 |' e0 o1 M    }  w+ Q: N9 c" u, K/ r# H
    i++;8 E( T* H9 F( T* p( V0 G
   }. X1 O1 K# {8 w$ n1 |" }1 Q& Y
   in.close();
( M3 x" w$ m0 a  } catch (IOException ex) {
4 O* ^  g7 x& c8 n( R   System.out.println("Error Reading file");' d) M( l9 ]+ @- P
   ex.printStackTrace();
5 M2 n7 M" C/ x0 A8 P# C+ Q7 U   System.exit(0);
1 v% c, y$ w8 y, O+ E  }
' U0 [* J2 n" g" U }
0 E* ]/ k5 p! A) m& a6 Z* O public String[][] getMatrix() {$ z/ k& r, F  B" q5 f5 @' v
  return matrix;+ {- ?+ K; [9 m  U# a! x
}+ r* M) s" M4 X
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-4-5 06:49 , Processed in 0.018021 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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