设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7710|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
& G5 I* E8 r( u) wimport java.io.BufferedReader;
" F  ], ]" }0 g. ~! x0 Kimport java.io.FileInputStream;
' i4 v* A2 h5 _) G( ^import java.io.FileNotFoundException;
+ l' d1 b3 @, cimport java.io.IOException;
- N8 b% V; g) y( }( C, p* X5 ~& ~import java.io.InputStreamReader;
6 p: B5 _. Y4 A8 Nimport java.io.UnsupportedEncodingException;
# L4 Z- B1 x2 b9 k" _& Q4 `# p( d. `$ O9 ]import java.util.StringTokenizer;- b1 z2 d# ]" \, t9 v* w# j9 H
public class TXTReader {
9 _! M- y1 y) Q5 M1 x) m$ W protected String matrix[][];
, @6 z/ y. A- M( X& F" I protected int xSize;; Z! B  a6 l# b6 Y2 |
protected int ySize;
2 z+ {) |( t9 N0 o8 A public TXTReader(String sugarFile) {
! z- @2 i: N  M) g4 I; z; i  java.io.InputStream stream = null;
5 T; D; W' I, Q  try {% i3 i0 y1 l2 u2 p1 s" V
   stream = new FileInputStream(sugarFile);
  V5 C- c- V$ o4 f$ Y0 g9 e9 h0 i  ^  } catch (FileNotFoundException e) {( z" b# C* r$ `0 [! d
   e.printStackTrace();7 X7 c% X) d& P5 Z4 \( U2 `
  }
4 _9 Q8 ?/ F0 e* P; g3 v" Z  BufferedReader in = new BufferedReader(new InputStreamReader(stream));( H  B/ \& d# a2 ?' _
  init(in);
; @! M# T5 ]: x- w: I }! j( D) v% C& {9 J. e& L- |" m
private void init(BufferedReader in) {- x4 Y, d" [0 w6 G* ~
  try {# z; S6 S- Y' D* \/ A9 Q8 c
   String str = in.readLine();
8 c5 [- g& k$ |5 v9 z2 z2 {1 M   if (!str.equals("b2")) {# q3 m2 e& C; \+ C7 |9 p# `1 W. s2 c( J
    throw new UnsupportedEncodingException(# o) a- T9 V  ~- j- `/ E2 G
      "File is not in TXT ascii format");" N- s- z3 f2 b6 m
   }
/ ?$ X7 M+ P- {" ]! b   str = in.readLine();
% f9 f' k: ^) R! K   String tem[] = str.split("[\\t\\s]+");
3 W( Z( ?/ x7 Q8 V! U4 }7 L$ S   xSize = Integer.valueOf(tem[0]).intValue();( q) Y3 L6 ?3 c  m
   ySize = Integer.valueOf(tem[1]).intValue();/ ]: @; \' `' G% L
   matrix = new String[xSize][ySize];: L; V" x; l  ~8 ~# r3 `1 q! {2 w
   int i = 0;
3 s; b5 ]9 ^; c   str = "";
" O0 ^9 X9 Y7 H* }4 X& P   String line = in.readLine();
! ?. Z/ z; e5 e' `+ r: P1 X2 N   while (line != null) {
8 _3 d8 o5 l8 s# o5 e- K& ~    String temp[] = line.split("[\\t\\s]+");
6 T6 Y2 Y9 a4 k4 f" D2 ?    line = in.readLine();
3 C; V1 U* ]% s0 o    for (int j = 0; j < ySize; j++) {
" ~& E& v. l- O# H  {0 {$ d' Y  t     matrix[i][j] = temp[j];& K: i" J9 N3 y9 d* Z& ?6 H3 d
    }2 \; Q5 z$ A% F4 K
    i++;1 M# @( l; S4 b1 g1 l" W& D
   }- L) `% `- }9 W5 l: ]: q
   in.close();
8 y& H: @& Z8 L+ x, S( w  } catch (IOException ex) {& |6 N1 g+ c1 U9 q: t+ `
   System.out.println("Error Reading file");+ M5 S# Z/ Y% V" D) R
   ex.printStackTrace();
/ m2 t. l& W& H# S   System.exit(0);
( F+ }& A! r, }  }
1 V9 F$ f) \# l$ u' M! l }7 G. N$ @+ t  D: Q3 T. f
public String[][] getMatrix() {
+ E3 W/ Q2 g/ g  return matrix;2 F' ?! B! U, l
}
. K- `' C! Y1 r% r, o# L! K; d}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-4-8 23:02 , Processed in 0.013421 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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