设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10413|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;+ f# w+ x: i) {! L4 ~& r
import java.io.BufferedReader;9 S8 W( s# [$ i! h& ~* @. B  h
import java.io.FileInputStream;) N* |2 k+ D' v: q" X- e" g: z) \
import java.io.FileNotFoundException;
- W4 g! A" w* d1 M* U3 oimport java.io.IOException;2 J. a! F# r" N4 E5 E
import java.io.InputStreamReader;) z6 B1 @  h7 L1 N4 S5 W
import java.io.UnsupportedEncodingException;
6 H% X9 C# U! i1 }5 _8 zimport java.util.StringTokenizer;0 q- a) S( y9 x. o3 T
public class TXTReader {
6 y8 K  U) O$ C0 J protected String matrix[][];
7 N* v- i& u  @) [( J3 s8 _ protected int xSize;
% l! I; Z( @. z) q* D1 s4 ? protected int ySize;1 p7 Y- }# G5 Q% M; U* F2 F; Y" h: q( U
public TXTReader(String sugarFile) {
( C2 a+ ?2 G; W: l" l  java.io.InputStream stream = null;
6 E; U  q! S# N0 z% i  try {
- U: q" Q' `+ c  C   stream = new FileInputStream(sugarFile);3 P2 e# ]; h9 V* z. j5 i
  } catch (FileNotFoundException e) {
2 ~- N# F- a, Z( C   e.printStackTrace();
) t( h4 w" _) S; U) o  }! s! }7 Y2 X$ z7 G9 m
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));! K" ~: @& W' V, K% C: ^  W
  init(in);
( m' n* Z0 X/ c }* h% s) t! K" ^5 Y
private void init(BufferedReader in) {- @$ |# [" D* i
  try {
) }6 K) G9 x9 n6 S6 a5 S   String str = in.readLine();
, @2 W3 P) ^1 Y9 W8 _   if (!str.equals("b2")) {$ P. k+ E' k8 Y3 i6 T7 I  [
    throw new UnsupportedEncodingException(: F% [5 m( e3 \1 m' |/ \+ E3 x) x
      "File is not in TXT ascii format");8 o/ n5 x% E. g; X
   }
+ ~8 j9 h: p8 m' J; z, r/ b' ]   str = in.readLine();
/ A. u# G; D6 H$ d- w" p   String tem[] = str.split("[\\t\\s]+");# k* L) v5 r* ~( K+ M
   xSize = Integer.valueOf(tem[0]).intValue();
) b! x6 [3 a2 M% |; X: N: w   ySize = Integer.valueOf(tem[1]).intValue();
0 d- c% {( l# @7 H. W; z# r5 x- k   matrix = new String[xSize][ySize];
2 Z! _; b$ r9 _2 H' l7 _3 ]   int i = 0;
" p2 l" H2 t! {1 T* y1 ^   str = "";
( t$ ]; e4 k2 a" Q1 Z$ u4 l   String line = in.readLine();8 o  L$ m4 {% C, s! T
   while (line != null) {
2 N' c- L3 D( t1 B; [$ N% X+ z! p    String temp[] = line.split("[\\t\\s]+");1 C4 m2 x4 l; k4 j
    line = in.readLine();* s8 @1 T% R5 r2 \+ b
    for (int j = 0; j < ySize; j++) {7 c+ H  p; v$ C2 h/ l
     matrix[i][j] = temp[j];9 Q! v% R* {* D* V
    }
; ]+ U* d/ m* g3 [0 x. t2 Z    i++;. I" v3 l( X7 a
   }
7 D$ {  z+ F9 M: X7 f   in.close();  ^8 t8 t8 ]  h( v* w) {1 @
  } catch (IOException ex) {
& w: }% E5 f) J. F   System.out.println("Error Reading file");
; e, y' |, |, m   ex.printStackTrace();' L; g$ {+ O' S2 n- H, s
   System.exit(0);2 i5 v% f% y5 |% w* I9 H+ t
  }  y( N4 p9 |$ \, z
}, c1 H9 l. h5 c7 ?! o2 h
public String[][] getMatrix() {# q: K8 N- O6 O& f) e/ T
  return matrix;9 ]$ j9 N  v3 q
}; [6 o; i( b( x9 n. U  Y0 Q
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-9 11:01 , Processed in 0.019951 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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