设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10496|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
  X, @5 i, e4 T/ X. Y5 Cimport java.io.BufferedReader;9 Y' o7 Q2 C- T* F. V1 O
import java.io.FileInputStream;
. N% x1 e& k+ O# d* X! W0 Pimport java.io.FileNotFoundException;5 K5 v# L7 G8 E6 b! {
import java.io.IOException;
& O7 a8 i0 J# p9 n" Oimport java.io.InputStreamReader;: Q3 ]. w7 G: m% z5 W
import java.io.UnsupportedEncodingException;
! d. k: O5 N/ ~! o) B! h; Gimport java.util.StringTokenizer;" u; q% M' E# D0 I+ V
public class TXTReader {$ R" J4 l5 O' u, ?, J9 K% d
protected String matrix[][];# S, z1 u# \% D  c7 ]
protected int xSize;2 Z+ {5 Z  P; M1 w' e& u
protected int ySize;- h4 n# C7 j( d
public TXTReader(String sugarFile) {
& J0 C& L& b- ~  java.io.InputStream stream = null;. K& Y) p1 E- K0 o
  try {
' _3 g! y" {6 x9 c$ v5 t4 n   stream = new FileInputStream(sugarFile);0 F& a; z' Q  U" i" `
  } catch (FileNotFoundException e) {: T: H; ^; |; {" F; b+ ~* \
   e.printStackTrace();
6 U' w4 o" W5 d* L: c8 C) A2 r  }7 S9 I: \" T/ h1 T
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));2 f! n) V, l9 p$ q
  init(in);
2 b, Q; @! N" s  j }
( d" }' k5 w  ~9 N7 `3 R: Z# J private void init(BufferedReader in) {5 h2 E7 h% }# C, }) N7 d5 T
  try {& g! _) s7 g/ v; {
   String str = in.readLine();( b6 i* q3 M4 q. @  j' p
   if (!str.equals("b2")) {, S* m' g/ j7 L, G
    throw new UnsupportedEncodingException(' O* |. g- E% r4 K
      "File is not in TXT ascii format");
* L: W3 G2 U- s+ ]' c   }% i4 ~$ L, ]2 v8 f
   str = in.readLine();/ m1 R) {+ E! J6 Y' W/ p
   String tem[] = str.split("[\\t\\s]+");' J! A& n  f# ~* _+ H
   xSize = Integer.valueOf(tem[0]).intValue();
$ l( j$ b. B; A+ x4 A   ySize = Integer.valueOf(tem[1]).intValue();
) z2 G+ m% ?1 z$ K4 G% {) H   matrix = new String[xSize][ySize];
7 v; @, n7 Z! f   int i = 0;
+ J# V) ^: J& @6 j   str = "";6 c  e+ y3 h& a+ O4 s) {
   String line = in.readLine();/ p9 w4 c3 V5 B
   while (line != null) {
+ F/ N" ?( M) b$ |' E    String temp[] = line.split("[\\t\\s]+");
! [) E' T% Y+ R9 X6 e    line = in.readLine();# o/ R+ J+ S8 }- z+ ?5 n, o
    for (int j = 0; j < ySize; j++) {
1 [0 j( }6 ^3 t     matrix[i][j] = temp[j];& M: W' V1 ]; h2 N5 S# T
    }
9 u5 B* s8 w' s% P4 {4 W5 J    i++;
% ?* H" b) Q& N* W   }
. I( a/ d- i: D% [* v   in.close();
. |2 [6 X/ N! Z( g" ^! H+ H2 C  } catch (IOException ex) {
" L, H0 ?" ?" ]: m& |- a4 t* x   System.out.println("Error Reading file");# b! _$ r0 ]6 i; X
   ex.printStackTrace();2 b! k' a5 ^5 d
   System.exit(0);
6 }, m5 e0 ?8 M  n9 O8 f  }' X, E8 h2 o  n. E- n, J  B
}
! c; _3 c0 m( S( v5 h public String[][] getMatrix() {
- }; w: R3 v" r% H+ ~  H  return matrix;
; [( b5 V% s: x& L- p6 s8 r }
8 F1 V% y+ |* \' o3 T0 X: ^}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-9-13 12:37 , Processed in 0.023004 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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