设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8331|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;9 q; P$ e( d! G( ^; u
import java.io.BufferedReader;* G$ }$ s4 u+ K
import java.io.FileInputStream;- S8 H8 l; O; U/ `
import java.io.FileNotFoundException;3 F* [: P8 b8 F1 m4 s
import java.io.IOException;# n6 v" V8 V. b
import java.io.InputStreamReader;' S+ ?: f9 {3 F. t$ N& E) g" P
import java.io.UnsupportedEncodingException;  N4 j$ Q( A+ S8 N4 a5 p* }
import java.util.StringTokenizer;
0 [) `0 ^5 W7 |5 Kpublic class TXTReader {
1 i, W5 T# E/ G9 j protected String matrix[][];+ a- Z) S' B. G1 V
protected int xSize;
$ J, o# U3 ~5 l( I- c, D2 f0 O protected int ySize;
8 t& Y7 }+ c8 g* A3 d# q, _3 \5 E" P public TXTReader(String sugarFile) {( w7 o4 l3 P3 i2 {% R- j: f0 ]
  java.io.InputStream stream = null;
, _$ A4 y# X. @- ]8 ~: ^  try {
( Q+ u9 W2 l3 ~6 D   stream = new FileInputStream(sugarFile);
0 D3 f: v- U$ l; P' q! |3 _+ o  } catch (FileNotFoundException e) {
9 Q  c6 Q" k3 ^: m3 C2 v6 X1 C2 Z   e.printStackTrace();
# e4 N( m$ c7 ?% a: b  }+ s- |( |  n# f2 ]
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
+ T8 }2 R( K. z$ I  init(in);; K. |( _+ U9 e
}5 @, Q! M3 f7 O+ i
private void init(BufferedReader in) {
% m  ?) T' a0 {2 g5 ~  try {0 Q: h1 q# H' M8 Y9 ]& M
   String str = in.readLine();# l# z$ |/ m; d$ _6 r3 _
   if (!str.equals("b2")) {
. J6 r, _$ C& C% a- U7 Q1 H    throw new UnsupportedEncodingException(/ [' h5 C$ ]$ ?) I+ q" f- A% Z
      "File is not in TXT ascii format");
' ^- n# h$ r" c4 r. H1 |   }4 e& |$ X7 d  ~
   str = in.readLine();
8 }2 R! U2 f& W7 _   String tem[] = str.split("[\\t\\s]+");
  G1 Q3 X1 G; ^! b   xSize = Integer.valueOf(tem[0]).intValue();
/ u4 m3 @. I5 e9 y/ e6 ]   ySize = Integer.valueOf(tem[1]).intValue();
* b8 P% n2 [% G' v7 |   matrix = new String[xSize][ySize];
. U* x2 b. F2 `   int i = 0;
# C* Y1 }* H  u. `) `   str = "";4 ^: I4 B$ A& H7 l8 V2 L3 N
   String line = in.readLine();
; _7 ^  [8 a. r1 L   while (line != null) {0 r1 b& E# ]# V: P4 |3 V
    String temp[] = line.split("[\\t\\s]+");
; S8 q$ W7 e( H- v* V" z0 w7 ^, g    line = in.readLine();
2 R4 l' Y  v6 _4 f    for (int j = 0; j < ySize; j++) {3 t  w4 G7 D! y( r8 _* R% a: }
     matrix[i][j] = temp[j];
% U& t4 Z' z/ O9 |6 Z; Z. S% k; S    }- u. `/ k! @% _9 k8 J
    i++;( Q& u8 ]& h$ `4 k7 f# G
   }
/ e  ]0 i5 s5 R/ L   in.close();
* J, y5 i: A' o" H" ]$ V8 C6 M5 w  } catch (IOException ex) {% u; u7 D; k9 s) [2 M3 n
   System.out.println("Error Reading file");
% B5 t* {, q  E+ g1 e$ i   ex.printStackTrace();
, c" ?) Q+ X/ u   System.exit(0);
; x; O4 ?+ ^6 b- V9 B6 r  }
% ?% Q! s9 A' `7 e1 D7 d' D }
/ A( R, A/ u) ?, U3 D public String[][] getMatrix() {. m2 }9 k9 j+ q7 s+ [$ y$ `6 q
  return matrix;
  A4 G( g; Q4 B$ B" S) Z* y$ w. u }0 K5 Z4 _  m5 X
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-15 12:59 , Processed in 0.017917 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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