设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8741|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
9 z) c' Y- m' Rimport java.io.BufferedReader;
0 z: W/ z% q$ j! himport java.io.FileInputStream;
% n7 N/ F- m% {/ ^( f) jimport java.io.FileNotFoundException;
" s, Q) i/ B/ d3 jimport java.io.IOException;( S1 B) C2 D1 K. d4 I/ A
import java.io.InputStreamReader;
/ i7 ~* m( i) R7 k, S7 rimport java.io.UnsupportedEncodingException;  }. \( ~9 y/ `) B- `  r. Q
import java.util.StringTokenizer;
1 g6 b2 v1 U* O) v% ~public class TXTReader {
* q2 y- {5 i7 h protected String matrix[][];
9 l* U3 g" O9 k& \. C1 d" s, B. u protected int xSize;
7 }% t( E; T$ z  \ protected int ySize;
! U' E# Y- @: g# j3 Q! d' d$ C public TXTReader(String sugarFile) {4 b" W4 x$ {) n* Y8 K0 f
  java.io.InputStream stream = null;
; m' [- V& H3 S/ D( C5 k3 t, s  try {& X6 ^4 L0 J/ F
   stream = new FileInputStream(sugarFile);
: A" o  x7 P  G& u4 p* \7 D  } catch (FileNotFoundException e) {
6 Z+ R0 I& C/ ~3 Y' V* _   e.printStackTrace();* D+ U- a6 \0 N7 e
  }
4 k! P4 |2 b( e" u. c5 g$ A( v% l( d  BufferedReader in = new BufferedReader(new InputStreamReader(stream));$ m% Q2 ^& L' ?3 L
  init(in);3 {1 t6 k* U* v
}
" @+ j3 H" H% x1 B; m# u private void init(BufferedReader in) {
7 y) Y- E8 O9 d; o" P; W  try {$ X, t. h; _( ?
   String str = in.readLine();+ {3 z* E- V  i
   if (!str.equals("b2")) {6 Z5 Z8 D; p, |& F# o
    throw new UnsupportedEncodingException(
9 V/ g& {) T- T- n" ]- i+ w, E      "File is not in TXT ascii format");
. |+ H- ~+ T/ w! G( |   }
/ t- B/ t2 L1 ], [5 U. t2 A( `( j- a   str = in.readLine();" {8 d9 r! A$ y! F
   String tem[] = str.split("[\\t\\s]+");0 j( z* U4 _9 P
   xSize = Integer.valueOf(tem[0]).intValue();% Q, ^5 T, M: x/ V
   ySize = Integer.valueOf(tem[1]).intValue();
. n% t8 V6 L5 e: ?   matrix = new String[xSize][ySize];
* T, N. a- D  G# F: O   int i = 0;' b" N5 N) i/ Y; o
   str = "";: ^& R* h# ]% I% t, A2 r
   String line = in.readLine();, L. x. j0 f, W3 m" S: j
   while (line != null) {4 g& w/ M7 G$ H8 ?5 h8 P: [: N
    String temp[] = line.split("[\\t\\s]+");
1 T) i$ l, C3 m0 ?3 t* |1 s# h    line = in.readLine();7 R$ Y; c$ \2 r0 K, {, }
    for (int j = 0; j < ySize; j++) {
) I& l5 B8 a# H     matrix[i][j] = temp[j];
/ w& v( R: E: \    }1 H( O2 {( H" U' D! j
    i++;
) e' `; [, E! q. c3 W3 c   }
: G; e6 D5 T: b   in.close();
, g* Y' |6 g7 D, a  } catch (IOException ex) {3 m! x& J: W" b- l1 B4 F4 ^! v
   System.out.println("Error Reading file");
( @9 n! z% D4 J; ^1 Q- s- o   ex.printStackTrace();% W- L- U, _1 n) u% ?# s: Z( ]/ k" V
   System.exit(0);3 o3 Q* R7 ~1 E# p; @- Q( G' X
  }, A. r6 N; ?$ Y8 V/ c4 f, Q
}$ R5 ]2 D) W  j: s
public String[][] getMatrix() {  r3 o: ]) E' ^; s9 \# Q
  return matrix;
  w/ W+ z* q1 @) Y/ m }
& s; Z3 f8 o- {! P}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-10 16:53 , Processed in 0.016516 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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