设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9406|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;. R7 g% C5 W+ s5 n" I
import java.io.BufferedReader;4 C: W1 T6 Y  Y. i
import java.io.FileInputStream;
6 O1 P/ j2 W- c" X" \import java.io.FileNotFoundException;% h: B" _1 W; ~, r
import java.io.IOException;
! L- L+ G8 R/ o4 W) _import java.io.InputStreamReader;
5 {* V4 q* R/ u: Pimport java.io.UnsupportedEncodingException;2 B! j; d. G$ c' H7 `3 S
import java.util.StringTokenizer;" S- K- ^/ T8 h
public class TXTReader {
$ m/ L/ n% ~- U+ E protected String matrix[][];
7 I6 L7 `  c+ z# H  Y3 @( d protected int xSize;/ B  M& n) m) `) }8 O0 Y; W/ H
protected int ySize;
' h" f( K( w1 {0 |' z public TXTReader(String sugarFile) {
' F+ Z8 E7 I7 H  K  java.io.InputStream stream = null;) t/ ~$ B, {0 e5 l7 G
  try {) m# ~( v! `7 R3 p% t' F) o  s6 b
   stream = new FileInputStream(sugarFile);
( U# p" ^* ~: s8 J  } catch (FileNotFoundException e) {: Z" z0 _$ _( U- X
   e.printStackTrace();0 e$ Y( N4 |4 g$ b; K
  }' U5 j" L& F9 @& o6 [; y; r* R
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));9 z) ^- f) `7 z$ V
  init(in);& i% X! \6 t3 A% n4 C5 e
}
/ b4 H  O& _% w% i2 b private void init(BufferedReader in) {* W) ~2 A% a4 |) w9 B8 Y
  try {
, f5 [. J2 ^+ i, y' w   String str = in.readLine();
, g& m0 P. e3 G* M$ S   if (!str.equals("b2")) {
3 M/ l" W. Q7 u/ C" H    throw new UnsupportedEncodingException(
2 ?/ P4 k4 Q! k: g, H      "File is not in TXT ascii format");# A! O( h: I: Y: {. ~& M
   }; r/ S7 {* D! U6 z
   str = in.readLine();
+ X1 D0 V( `6 B: T: A   String tem[] = str.split("[\\t\\s]+");3 A. C1 ]; `, E+ t6 n. |
   xSize = Integer.valueOf(tem[0]).intValue();
! Z! ~8 k0 n9 j& z* m9 O' ?5 e. a   ySize = Integer.valueOf(tem[1]).intValue();
- u# ]& t% j0 j6 k2 m   matrix = new String[xSize][ySize];
$ I4 s5 ?7 d/ N. ?   int i = 0;
8 c! ~# O3 @  g( r, s: i   str = "";
; O2 m' H  P: G+ e   String line = in.readLine();& o+ Q$ p: o5 j/ y- l" v0 E5 I
   while (line != null) {
# J1 G  e2 @& O3 C% k9 }    String temp[] = line.split("[\\t\\s]+");2 r% v! d7 N% w2 z) f& N  M. P! G
    line = in.readLine();
- d7 R  @9 a" h+ ?( @    for (int j = 0; j < ySize; j++) {
% }, K. p* I% m3 P: A' ]     matrix[i][j] = temp[j];) J5 G% Z$ w' L$ b$ d9 n/ c1 f
    }. Y" R" j. v# p" B9 q# l
    i++;- ^0 z! q, D! m" p) a4 U$ Y
   }7 b9 p4 R$ `( H1 c
   in.close();
& h. _, A. ^, d: C$ M* K9 _' Y/ @3 {  } catch (IOException ex) {
1 L+ y0 m& _  T" [2 C# E   System.out.println("Error Reading file");
$ |2 o$ `5 v4 A( m8 U2 J, S   ex.printStackTrace();
  |: Q: P1 P" @  B$ V/ n   System.exit(0);5 [( g2 q1 x! C& o/ G
  }
3 S0 X( v) J; `+ o  v }
3 T; `# }8 |( F0 v# R( y8 R% K# {9 Y public String[][] getMatrix() {
2 U6 `7 B- }& S: V& Q8 x  return matrix;
- h0 R8 C0 U& P5 z }
6 I3 |0 l+ M. H}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-18 10:54 , Processed in 0.014402 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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