设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6654|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
* v! o9 ]9 x! W# m* k5 Jimport java.io.BufferedReader;& y: k- K) l' A8 a' f8 c3 T0 o
import java.io.FileInputStream;- f" }: h. [/ Q4 Q$ s
import java.io.FileNotFoundException;5 G( R* i- H7 z' A2 d- i
import java.io.IOException;1 I4 r6 F, F3 F, Q5 a* p' {
import java.io.InputStreamReader;
7 v3 D. e4 I% L& iimport java.io.UnsupportedEncodingException;8 h' o* b5 B% z0 C7 w
import java.util.StringTokenizer;+ o+ |3 l. o# b
public class TXTReader {
2 n  v7 ?5 S* a7 N8 g. D1 k protected String matrix[][];
+ f% }, y2 G) Z/ B; |, W+ i: Z protected int xSize;; z) A3 |% s/ Y% }+ ]
protected int ySize;: S( ~5 n7 S0 Q( s6 A( H8 e
public TXTReader(String sugarFile) {9 M% w# K1 `' @
  java.io.InputStream stream = null;
+ ^1 @$ w/ ^" q  try {& s' M* m& Q6 s* [: {7 w3 D/ B4 ~
   stream = new FileInputStream(sugarFile);0 T7 y* B, n' r& E) N- X
  } catch (FileNotFoundException e) {. S/ R: L( f2 U5 c4 {' t. B2 E& f
   e.printStackTrace();1 Y, v' Z! ^0 ~0 v- `
  }
. u& c2 U. j9 m6 z  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
" k: |* l8 s$ j2 v- S+ R- O  init(in);
, M7 t7 R2 R* b9 |& o }
& d. [2 x  V! R# H# A private void init(BufferedReader in) {# S1 m% g8 {3 Y2 ~; c7 J
  try {4 _) q2 p: o8 c) d0 u
   String str = in.readLine();# q+ Z; {3 w6 ~9 M5 e* l  ]% X! j
   if (!str.equals("b2")) {
# K/ l% |% C: S    throw new UnsupportedEncodingException(
, p6 t! y8 K$ W3 J1 w9 e6 w. D      "File is not in TXT ascii format");5 c) {  y0 Y3 @! l9 [9 t
   }3 g3 f7 _0 Z1 U6 F  I% n
   str = in.readLine();
% I, _* @. H: o& Z; l0 `, S   String tem[] = str.split("[\\t\\s]+");0 p' ~0 ~3 s, P* s
   xSize = Integer.valueOf(tem[0]).intValue();; i% o) F  _5 e- }
   ySize = Integer.valueOf(tem[1]).intValue();) x5 c, }0 k7 z: W( D
   matrix = new String[xSize][ySize];
/ Q+ v" K3 g' h# @- }* l: V   int i = 0;
: n4 h. N! Q5 |( x9 i2 V- o7 h( R& W   str = "";
3 B6 B$ K9 f0 [& Q& f2 a8 W# Q   String line = in.readLine();* H( d3 b' x* L6 x
   while (line != null) {
. L/ j) k0 E+ C3 T. }    String temp[] = line.split("[\\t\\s]+");0 g# {# t( x  c; I
    line = in.readLine();1 c" g5 T' N7 N) f& m1 K
    for (int j = 0; j < ySize; j++) {8 z9 y( S) a3 {: h; E( j" o0 `
     matrix[i][j] = temp[j];. c/ |5 a2 ?8 ^- |0 u7 k% `
    }
$ u! H2 p! {; z& @$ [5 t/ {    i++;
2 N9 z* L# `: r" p# y   }
4 O6 {2 k, ]8 B   in.close();) n4 b0 A) k& S5 V
  } catch (IOException ex) {
9 Q0 L7 W/ H( a/ k/ b; |   System.out.println("Error Reading file");
0 E$ z& w& T1 ?( d. s. T3 g( T   ex.printStackTrace();3 \! K; P$ F2 l) g: l- z2 E
   System.exit(0);
& l0 e& P8 N3 t/ @: ]( g/ _  }
8 l* ^- Z' B; @  p0 g }
6 I+ \6 l* d7 n0 `6 A  O' T5 e public String[][] getMatrix() {8 d5 I, v2 N% F/ V5 K' ]9 M
  return matrix;# G4 K+ s0 N& U$ ?8 k
}
  E4 U2 }+ `3 Q9 @* W6 q( V0 G}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-24 20:47 , Processed in 0.027802 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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