设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5675|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;* C2 D+ f; E) w( q! O
import java.io.BufferedReader;
( D. D, A) u, ^/ l' Q3 Mimport java.io.FileInputStream;' p; Y. n, ^: A4 @1 g+ a2 v
import java.io.FileNotFoundException;) ?( s! a" ?6 E. F0 v
import java.io.IOException;; C; p: e: k% e: [
import java.io.InputStreamReader;
6 l; h9 W- L5 Timport java.io.UnsupportedEncodingException;
  I; q: b- K5 D; [import java.util.StringTokenizer;
* I' P) `# L* ~' N; s6 `. Mpublic class TXTReader {
* R+ y9 N, N- d protected String matrix[][];0 j9 H2 I. ^* V" O5 k# l. d* N
protected int xSize;" @/ w5 d9 ]& A- d) }3 t
protected int ySize;
0 ?1 U( T3 U: S9 T public TXTReader(String sugarFile) {
( G8 i$ H5 R* a6 Q* ?% n/ P4 \) }  java.io.InputStream stream = null;
3 [) C2 h, p* ^  try {& x: F6 t2 S' `
   stream = new FileInputStream(sugarFile);
# o% w) K# F# ~0 W6 n% f& v  } catch (FileNotFoundException e) {6 p$ o6 c! b2 G/ K
   e.printStackTrace();$ Y& E% B+ t+ l( c3 K( V
  }$ x* H3 Z0 K& V: e( b/ v; U
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
8 D6 G; f/ n" R  init(in);# A5 h  G- [  b6 ]
}4 u3 Q9 @$ w  T% y) |" q2 y  Y
private void init(BufferedReader in) {4 T( x& k, |1 N
  try {( a" `8 o1 [) C% g
   String str = in.readLine();4 z$ i( g; |7 K- g" I' [
   if (!str.equals("b2")) {
6 \+ H" q1 ?# M+ w    throw new UnsupportedEncodingException(9 A  i/ V! Z5 n+ x
      "File is not in TXT ascii format");& P5 e: Y* e2 a4 O1 Q# ^- ^
   }
& j4 E; l7 E: u/ X3 D+ J   str = in.readLine();9 l& P' e' O* J( E
   String tem[] = str.split("[\\t\\s]+");7 A; o# p4 Y. H5 B; N9 L4 O
   xSize = Integer.valueOf(tem[0]).intValue();
# A- h+ U; |4 y$ ?, \* ]" U   ySize = Integer.valueOf(tem[1]).intValue();
3 u4 ]" L6 L& Y   matrix = new String[xSize][ySize];$ C$ G: g% o# y1 A/ S- w: K
   int i = 0;9 d5 b! x4 P0 u
   str = "";
: o- h& d9 a- P2 M1 u2 G   String line = in.readLine();
" R4 m/ J, I! `   while (line != null) {( W5 l8 K  d  o& q3 {& D8 G0 ~# v
    String temp[] = line.split("[\\t\\s]+");- F& k$ `$ {1 E
    line = in.readLine();. O' W% {* ?4 t7 j0 x9 a4 L
    for (int j = 0; j < ySize; j++) {
9 ]" I4 `' p( {- @3 Z- Y% e$ z     matrix[i][j] = temp[j];
$ {+ g/ M( `! o% ]% j$ ?    }
0 n5 S1 _2 q  e* J* J+ @    i++;2 q% M" w9 p6 o) I
   }+ `2 [9 ?6 N8 O+ e
   in.close();
9 t9 |9 [/ O9 c6 r1 I+ v  } catch (IOException ex) {/ {& q$ m; n  m
   System.out.println("Error Reading file");
% B3 m; b# W7 I% K9 Z% P( p   ex.printStackTrace();
4 M- P: P' t  m1 E" F! I   System.exit(0);, ^7 ]- q: y, v$ u# d6 N% V
  }
$ z0 I: k  C4 O }$ p; G5 ^& `, p' r( X! d$ J
public String[][] getMatrix() {
' b2 F  ?3 @9 N& |) D, a  return matrix;
$ D& G, U- \$ B }
4 u# e$ X0 ~; N, e. w# R}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-19 01:36 , Processed in 0.014507 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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