设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9177|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;# f% q' ?( ?2 x; F
import java.io.BufferedReader;+ p4 W7 z8 F$ H
import java.io.FileInputStream;
- _+ \0 P) Q# [) ?' eimport java.io.FileNotFoundException;, L8 c4 |7 {3 e9 c1 m$ P, k
import java.io.IOException;
; C; r, h! o$ W9 i: Mimport java.io.InputStreamReader;' E: @* g( E3 a6 K1 i
import java.io.UnsupportedEncodingException;
; M! T6 o  X4 {0 U% i* D1 C. Fimport java.util.StringTokenizer;
3 v; H- n3 T6 p' x: ^& npublic class TXTReader {
% |/ x; W2 j5 x# V$ V protected String matrix[][];: U4 X* N7 |* Y4 [
protected int xSize;* Z6 c" ?: C, P9 C- Z
protected int ySize;
" b3 Y) G2 G) l. O public TXTReader(String sugarFile) {
* w# `; X; W" S% {# m9 {6 L  java.io.InputStream stream = null;' p$ Y8 I# o. w/ h6 c( i
  try {) |1 ]2 E1 [8 N1 [
   stream = new FileInputStream(sugarFile);
* }; @3 ~9 j6 O1 s1 l  } catch (FileNotFoundException e) {
- Y6 V2 h0 M& p4 w. @   e.printStackTrace();' E6 V3 Y( x# N$ G- d2 A( @
  }# g3 ^1 ^0 u8 t, k7 B9 f
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));1 h6 a& x; s, D( R# M6 G
  init(in);
2 p7 }2 C! {7 t6 v }. w: K7 d9 C2 h0 Q; Z9 B9 S  J2 P
private void init(BufferedReader in) {
. s. e- |- Z3 p. `/ C  try {
4 U% u- ?% f4 e! y7 @: I. J2 A   String str = in.readLine();
" J0 R4 q% {/ i) Y6 P   if (!str.equals("b2")) {
. T" K7 h+ V' z0 N7 q; I. e    throw new UnsupportedEncodingException(
9 l  @% o; I5 n      "File is not in TXT ascii format");
0 g2 U1 Y# n' @! {* ^" [+ x! ?1 q   }
/ n* g, I5 Q4 R8 G5 L   str = in.readLine();
8 R3 V9 h- |0 r( _( B: ]   String tem[] = str.split("[\\t\\s]+");
# h) U9 w7 G5 A  F   xSize = Integer.valueOf(tem[0]).intValue();
! A$ Y; `  P1 T) `) e  v8 U   ySize = Integer.valueOf(tem[1]).intValue();# d8 u( c( v* \6 X; u
   matrix = new String[xSize][ySize];9 Q% G3 O' G, y! ]* m
   int i = 0;7 K( v/ d0 i9 Y( B5 [9 ?  b/ _" T" Y
   str = "";& v  O3 H4 Q6 P5 L
   String line = in.readLine();  H! m0 W( t! d+ P6 M8 T
   while (line != null) {+ a- f- F9 g% w1 a
    String temp[] = line.split("[\\t\\s]+");$ X5 H, g7 V2 L4 r
    line = in.readLine();
0 F+ H  r: d% Z3 H, W4 h! E    for (int j = 0; j < ySize; j++) {7 k' O6 W8 [$ `$ A" A' y
     matrix[i][j] = temp[j];
0 _# x9 Q7 |' G6 T) {    }
6 ^! I, x  a* Z- `# B5 S6 b    i++;
- N- ~6 ?/ a  k. Z   }
& a2 b2 w, P: {/ x* c( _* m   in.close();
% H" j# a' p' `2 @6 R9 C( M  } catch (IOException ex) {% p& \$ n- ^1 v
   System.out.println("Error Reading file");0 }- B4 y4 @0 ]7 j2 g# d
   ex.printStackTrace();
: p6 `3 p+ @! ]& O$ k   System.exit(0);& p( K  a, {  b( i+ R$ F0 v
  }5 Q  w0 t- K) r
}
; U; r% J5 f4 e( X! u6 ] public String[][] getMatrix() {
* s& u/ t& ?0 h! ~$ a  T6 A8 ~; q  return matrix;
1 d1 a: A  Q- N) B }+ F+ m$ a$ ~  R2 ]* ?6 M
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-6 19:38 , Processed in 0.012603 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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