设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6919|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;- I0 C7 T- H. ?: u
import java.io.BufferedReader;9 z3 z& a0 ?. ~5 O
import java.io.FileInputStream;: m: N7 ^9 l% h+ W, e0 D8 m7 D! s
import java.io.FileNotFoundException;
# A. B% Q) G4 F; J( Eimport java.io.IOException;5 w" V  A2 }- o
import java.io.InputStreamReader;9 _& Y0 @. W# [
import java.io.UnsupportedEncodingException;
! w0 J0 r! k! Z1 @import java.util.StringTokenizer;
% R3 |2 E2 [# F5 m& j' ipublic class TXTReader {6 p. n; |( h& L" {6 l, b- V' a+ Q
protected String matrix[][];
  C$ k' s. [/ j protected int xSize;6 R) F% @& K! f* v8 x
protected int ySize;9 o. W  ?. n1 O, L% V5 w
public TXTReader(String sugarFile) {3 h1 Y4 C5 a+ j" t2 |* {- d  A5 J% m5 g
  java.io.InputStream stream = null;
5 l3 b3 y: [2 f2 v  try {
0 `' i+ f1 V1 y/ |! q8 g   stream = new FileInputStream(sugarFile);
: e# e/ R2 i! ^! J& [  } catch (FileNotFoundException e) {
9 N, F* F( m7 ~1 X# @2 O8 P   e.printStackTrace();6 ]  e+ ]; N5 j% R( h
  }+ y9 j+ R5 V3 o8 m4 R3 F
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
6 ^! N" Q- X4 k/ X  init(in);
& |+ i. \/ K- U% g- y }
* R3 d; O& T% _% I: u- Z2 Q private void init(BufferedReader in) {
7 H1 A1 I6 e. ~  l- {4 W2 T& t# N' @, Z4 I  try {
2 N' h% E1 j; p3 J. V, x! {1 u. a" }   String str = in.readLine();- f& f( h- ^8 G5 B
   if (!str.equals("b2")) {
. V! j0 ]' X" }3 q# q, M9 d" {    throw new UnsupportedEncodingException(
6 ~1 @8 s# \+ z: O      "File is not in TXT ascii format");
0 h( q% s# m, G( `; U, X% O( Q   }) i( \1 S+ T! |: y/ H" Y
   str = in.readLine();
( _5 D- ~' n, s4 L; |0 t   String tem[] = str.split("[\\t\\s]+");5 J. R1 ]  w' M; t! \) d" G. W
   xSize = Integer.valueOf(tem[0]).intValue();. [+ A1 h, A0 ^# @
   ySize = Integer.valueOf(tem[1]).intValue();
: q$ Y6 a: p* V- N6 g9 q   matrix = new String[xSize][ySize];1 z& v0 ]0 e, b: Y  M1 Y# Q
   int i = 0;1 K' k5 G. O2 d; i5 D4 J. U: E5 k$ L
   str = "";
8 A6 O0 U! Q5 [1 |2 R   String line = in.readLine();7 q; \5 P* H6 A0 z
   while (line != null) {: W. z. ?6 G- r% ~: P) @1 K
    String temp[] = line.split("[\\t\\s]+");
& f' o$ \7 {3 a: `" ^: P$ ?    line = in.readLine();+ E3 w" f; w9 K, c  g0 S5 d3 j& i
    for (int j = 0; j < ySize; j++) {5 S9 n% @+ ^2 h: X- E$ ~
     matrix[i][j] = temp[j];
+ t; ~; d; H5 D( h( f    }
- Q) l' O3 P% l- N" R    i++;
" ~: j' [/ q$ S) L* {: y   }. t7 m7 A5 w/ ]$ k
   in.close();
5 g! A4 l! h6 D/ E3 W  d% ?  } catch (IOException ex) {
* h+ f7 ?5 [% C$ {5 ]  g4 D7 j   System.out.println("Error Reading file");4 f- e2 @- H5 j! j4 U
   ex.printStackTrace();
6 l) v: V  D; Z3 v& A0 H1 w$ O# G   System.exit(0);
# _6 A& k2 ]2 b/ y; L6 ~  }0 s3 m; V# v4 s( a
}5 `  j: I% e2 |4 M  N  X) b
public String[][] getMatrix() {
6 m7 E! S) T' B9 t: B  return matrix;
' M1 R  I6 @+ _ }
5 v8 E% d6 k' _% |9 r8 I% s}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-10 19:56 , Processed in 0.019984 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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