设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7622|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;) U% v; `9 v6 w% E, w
import java.io.BufferedReader;
! I$ P3 ~; k/ D% z3 d5 {import java.io.FileInputStream;
# K; G3 A/ k3 o! timport java.io.FileNotFoundException;
2 x3 s. P& L% C! V: h# T: Qimport java.io.IOException;. ~3 t: c6 N2 @1 ]! m' ?) R
import java.io.InputStreamReader;
3 g+ L5 ]* s2 h( I) j- Uimport java.io.UnsupportedEncodingException;" x! x0 v( Q6 F
import java.util.StringTokenizer;+ w7 V3 c  }; R% t* W
public class TXTReader {; ?3 y/ O- a3 X$ @2 M
protected String matrix[][];9 `& P: l+ N8 c" K
protected int xSize;. G% {& q4 b& n
protected int ySize;
5 }8 D9 B4 C; q, f4 R, a5 D9 h' B public TXTReader(String sugarFile) {5 N. K7 W1 ~: g; }  K- h1 h0 C4 d
  java.io.InputStream stream = null;
1 i! l7 a1 x! V& U& K  try {% z9 A/ C6 b) o3 H) P
   stream = new FileInputStream(sugarFile);
7 p! g& W6 r' P# F' F5 j  } catch (FileNotFoundException e) {1 j" V6 L: F$ ?2 m
   e.printStackTrace();% _3 {6 R# U9 T! w( |( h
  }
4 E: J4 Q9 Z# a  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
" S: e8 s: ^; m$ q: d  init(in);
/ t: m. V& K. @2 K: g }
8 I3 ]" d5 j- g, {6 d, m private void init(BufferedReader in) {& f# w2 v' p3 g- T, _
  try {
" O& C' ]: F9 B. ^8 Q   String str = in.readLine();
; V( l/ U. G2 y, P   if (!str.equals("b2")) {
# _4 H0 h4 n7 M5 G  o: ]    throw new UnsupportedEncodingException(
( ?, H" J8 l0 c, n1 t& V      "File is not in TXT ascii format");" q& \7 T4 U. x6 k$ X' t; U
   }: i4 a) f7 N0 m! L! k. F
   str = in.readLine();* L" ?5 X+ T  U, H; D
   String tem[] = str.split("[\\t\\s]+");
! B# g6 M, j" j* h3 r' e4 }   xSize = Integer.valueOf(tem[0]).intValue();5 _" |$ S' E( u0 \
   ySize = Integer.valueOf(tem[1]).intValue();
+ l) `4 `9 }% Z9 }* @  b7 V; p   matrix = new String[xSize][ySize];/ j  L4 _7 Y% R% P
   int i = 0;3 B; ?, `6 }/ q2 C/ y/ r" v& b
   str = "";% i$ W( d: \) n" Q4 n
   String line = in.readLine();
) U- v! Z5 W2 M3 x9 o6 n# {) O   while (line != null) {5 M, C% U9 |# V0 ]1 o
    String temp[] = line.split("[\\t\\s]+");
; b) W* Z- v6 n: \. z7 T6 b" l" j" K, t    line = in.readLine();& B/ |0 ?) \1 Z/ t& g
    for (int j = 0; j < ySize; j++) {2 l% T$ ]: F' O3 g
     matrix[i][j] = temp[j];, J- P8 z8 c2 F& w" ^
    }* Z! |( v, i' C* |! F( e2 J. q  C
    i++;
+ R- ~; V$ h7 x" O   }
8 |/ z# f! y6 T, k8 C5 U   in.close();
5 }7 J# q7 O2 O8 C0 t; D  } catch (IOException ex) {
% ~9 \  d+ I8 i. z# H   System.out.println("Error Reading file");
, a: b- l# P+ j* v$ y- n( l   ex.printStackTrace();: d1 V- c. Q+ F
   System.exit(0);
8 a7 g) K: g3 ^  }! E# V# i3 r$ S' {& v
}( `+ j# y2 Q; S0 o% a
public String[][] getMatrix() {
! N7 Z4 t/ ^: ~: l1 F; }& K) R3 Z  return matrix;
+ _' I" ^8 U0 q+ S- m  T( {9 z }
9 ?" D. Q) G( r1 g, R}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-4-2 07:30 , Processed in 0.018557 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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