设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9894|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;2 A" W6 u& S% p6 s; C9 l
import java.io.BufferedReader;8 M+ |0 Q/ m# o  M5 Q1 i
import java.io.FileInputStream;- Q; E4 \" x! h. L
import java.io.FileNotFoundException;
* o  w- o! B3 \1 [8 w/ }' Z2 Qimport java.io.IOException;) y% x2 P9 p# \+ Z0 _  U2 h% r3 k( `
import java.io.InputStreamReader;
! A+ N7 z1 q( X+ Limport java.io.UnsupportedEncodingException;
5 w6 x/ c$ J, {9 t/ m* k2 \; x9 }import java.util.StringTokenizer;
  q4 O' B2 J/ M/ w. K1 e7 P2 Z/ }0 Tpublic class TXTReader {
5 V# d# c0 k) J+ O! Y protected String matrix[][];# c0 L  T: H2 `. K( p2 C
protected int xSize;9 m* T$ R6 Q* f' h) K
protected int ySize;$ ^4 P: u& `5 x. K
public TXTReader(String sugarFile) {
3 K. B, P& l) N5 B7 u  java.io.InputStream stream = null;
5 W# {0 f, B& h( h, C, X  try {
) w4 D! V' I7 f$ I   stream = new FileInputStream(sugarFile);) O" l+ h! x: s7 |" t: p3 ]/ {5 b
  } catch (FileNotFoundException e) {
. U! G" s3 @& g' \5 _   e.printStackTrace();
8 H6 @1 Y/ B. _; r) t8 o  }- l+ T. @& n/ L' \
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
+ }- q+ o0 G0 U+ u. E  init(in);
" G& E; j) w5 m2 d' L# \ }
8 E" \9 I8 U# ]$ _" `/ Z private void init(BufferedReader in) {
" l8 R* V$ B1 F/ }+ p% z$ s  try {1 x+ |3 F+ E0 Y. a
   String str = in.readLine();
* R; g* ^3 g- Y   if (!str.equals("b2")) {$ r' P) t4 k, w; @7 j- i* V  _
    throw new UnsupportedEncodingException(
" d1 N& h3 v8 O& }* p" Q9 H1 ~      "File is not in TXT ascii format");
- e3 o. a+ g/ K/ C   }# Q4 k( {0 U' j9 g: v  K. F
   str = in.readLine();
& E* z( t  c) x; f3 o3 r   String tem[] = str.split("[\\t\\s]+");
# O( H1 P& b8 \% x6 s/ \   xSize = Integer.valueOf(tem[0]).intValue();5 T- ^  c9 N$ C0 z- ^' j
   ySize = Integer.valueOf(tem[1]).intValue();; m& ?; B" T* b2 H" [5 p
   matrix = new String[xSize][ySize];, g! W( g: ^( u- Y
   int i = 0;% ~8 F& O' R9 ^, f2 @. z
   str = "";) `& T, G3 v" B; r4 e5 y! Q
   String line = in.readLine();* D3 P8 _2 f0 [; \& j6 z+ A, V
   while (line != null) {0 E! J0 f5 H% _8 [# V- p& T* h
    String temp[] = line.split("[\\t\\s]+");7 M4 z! s2 d# t1 |" d1 |5 s. X
    line = in.readLine();
# g- `# v5 K0 |/ d+ l5 ]1 M    for (int j = 0; j < ySize; j++) {
" k$ r! S8 c4 t8 M3 D+ n2 n     matrix[i][j] = temp[j];: d6 y" N, p4 ?. p3 c
    }
; h  F* O/ C3 h) m% J    i++;
& h: N  d3 ?6 z6 ?3 U* s/ N   }  Y+ L; n3 \0 u
   in.close();
# y3 P- c/ A7 U, o: _) Y9 }( n2 s  } catch (IOException ex) {
% R& K+ D3 n9 ?. x# N   System.out.println("Error Reading file");
0 B% I( @8 S$ z% k2 H0 V   ex.printStackTrace();4 @2 G8 q, e* Y
   System.exit(0);
( L* j4 f' J8 Q  }
# U' P9 y3 _3 \3 x/ h- b }5 p* D1 W8 Q- u# R& x
public String[][] getMatrix() {5 i8 J9 F( N+ m# h
  return matrix;% q6 ?, I- r. {5 z2 i& V0 F
}( h7 _6 M9 F5 l! @
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-9 18:48 , Processed in 0.014961 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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