设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7704|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
' }- @) w9 T( ^import java.io.BufferedReader;
0 n3 H, Z) a1 J/ d8 _import java.io.FileInputStream;1 b0 f( f8 v: n9 T' g7 O$ ]# |7 ^
import java.io.FileNotFoundException;
3 }& u1 z  E8 [5 \/ i' Timport java.io.IOException;
; V* v' }* J! e9 Iimport java.io.InputStreamReader;  b+ t/ ]/ S5 n
import java.io.UnsupportedEncodingException;
# J2 I8 v: J# W5 ~: T% s& iimport java.util.StringTokenizer;# {7 T2 ?9 I- }* p$ _) \
public class TXTReader {4 u4 O* P. l" a, _# z% X! q3 j
protected String matrix[][];6 k9 F: \  U# U' ]4 g2 ^. U
protected int xSize;
  ~2 O% n( G% [# i! t$ ?* v protected int ySize;
7 j2 s8 \+ I' C8 W+ b9 @" j public TXTReader(String sugarFile) {
' y, ?) L2 S1 C" P3 t; K: a' o  java.io.InputStream stream = null;
1 X; u9 G- ]7 H& H% ?  try {) V! O) b! `: D% g6 [
   stream = new FileInputStream(sugarFile);+ O) B* R# M) k( U: p7 o8 {6 ?
  } catch (FileNotFoundException e) {
6 W% q5 y5 d! f   e.printStackTrace();
; ]4 P' K8 v4 I6 M1 z  }
0 E6 k! O- V( f+ d7 b  BufferedReader in = new BufferedReader(new InputStreamReader(stream));4 d. ^! o# Y" g  G* A" I* i1 Y
  init(in);9 I$ l$ X6 ~0 h, J& a
}
& Q# ^0 g3 v/ z1 M" [# h private void init(BufferedReader in) {9 h1 S+ C) S- i+ L, ~
  try {/ N+ {* B* w2 e
   String str = in.readLine();4 f6 t( H& [4 O7 }' F1 Y
   if (!str.equals("b2")) {2 D3 |, b9 X1 A& `0 K' r5 w
    throw new UnsupportedEncodingException(
' m/ C+ ?: m% m/ C4 p& u+ Z5 J: O4 c      "File is not in TXT ascii format");
2 J+ C4 e0 x2 R   }5 J+ u; X7 G! D
   str = in.readLine();
" s! n# |' ?* }   String tem[] = str.split("[\\t\\s]+");
% ]4 q: T$ [3 t4 B7 H   xSize = Integer.valueOf(tem[0]).intValue();
8 p; Y$ y' H& R: b3 k4 O   ySize = Integer.valueOf(tem[1]).intValue();
: v! w" c" E3 g0 p  u; i   matrix = new String[xSize][ySize];
+ g: B5 d$ I4 F  F8 f# P4 r   int i = 0;
6 z& {, Y) n7 Z; g- T( U8 X   str = "";
9 d9 v/ T5 O* `+ p- k   String line = in.readLine();) x2 Q& R& [. e+ i
   while (line != null) {( B2 }  S" A. s- u! h2 |
    String temp[] = line.split("[\\t\\s]+");/ p! }- Z8 ^  I1 x
    line = in.readLine();
" b+ h6 |7 v; t    for (int j = 0; j < ySize; j++) {4 w0 o% v6 r8 H) J/ J* ?
     matrix[i][j] = temp[j];
3 A1 r9 F7 r" U# x3 m0 D5 |9 c    }* r. y% ~4 P3 v7 D, F. W6 s
    i++;7 T0 C. P+ U* Y& M% y
   }! d! Y" c6 \9 K. y( H
   in.close();6 E; l' N8 U& @& V( r
  } catch (IOException ex) {
6 O0 E9 m4 V7 D: E: X2 p   System.out.println("Error Reading file");
, {# Y5 x, F/ }7 P7 x' E* G( i   ex.printStackTrace();4 `) z, i( H0 d, W. g7 t" `
   System.exit(0);1 x- W1 V% X$ F/ g/ G
  }" ]. M% S3 W! P
}* H# n  ~' S8 z8 Z: M$ q
public String[][] getMatrix() {
' M2 i# K1 }0 O) d0 y( C& [3 X  return matrix;! f, e0 h/ ]( X" i$ p/ J: n, c' R
}. u9 a* b/ w5 X9 P$ X! Z% B
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-4-8 14:55 , Processed in 0.016382 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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