设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6719|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
# Z. d( F1 g9 [6 simport java.io.BufferedReader;# P& d0 ]6 W% e+ Z
import java.io.FileInputStream;
9 ]- v. E* B+ nimport java.io.FileNotFoundException;* L1 }- f- u* ^; |% l+ a: u
import java.io.IOException;
' ]" |' _1 u" limport java.io.InputStreamReader;
# P# K" k6 ^+ e+ T3 himport java.io.UnsupportedEncodingException;; S/ z, n% k: d
import java.util.StringTokenizer;4 y+ }+ k1 `' ^& ~  K; @' f
public class TXTReader {
  v2 Y7 \& d: A( f# d; J protected String matrix[][];
; C" A. x$ X2 C; I/ p protected int xSize;
+ W/ p7 a: j4 t0 o* H; J' h protected int ySize;
  [$ a2 l3 \$ C* g public TXTReader(String sugarFile) {0 p7 u: a) @1 b" o) E, K* U
  java.io.InputStream stream = null;' p: W: e/ G! w! |; x( v
  try {$ N6 X8 e4 T% {0 s4 |. [/ F7 e- f
   stream = new FileInputStream(sugarFile);
$ g" B* N& }+ P9 m  } catch (FileNotFoundException e) {% E2 Z/ B8 ~1 L  g- E4 |9 Y
   e.printStackTrace();
$ v+ b7 Q6 _' a3 u9 K1 b+ D  }  S' l8 C, r  E5 m, I% y
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));9 N$ |' g& u7 ?0 H9 E5 v* L& O
  init(in);
" Q0 D% P8 d6 m }* u( A; o8 L2 j
private void init(BufferedReader in) {7 w8 |( u/ a: W  c4 ?
  try {$ Z! Q7 ~, H7 c7 Y& ?
   String str = in.readLine();
& U0 X4 R; O; b5 i8 m. `  N7 G+ q   if (!str.equals("b2")) {" B2 f& [, C2 c3 D- {! a
    throw new UnsupportedEncodingException(
  {* i( B% f% [5 x      "File is not in TXT ascii format");
7 U  T& W! Y6 H% X2 D   }
" ^0 n' N9 Q/ x- P; c   str = in.readLine();, s# o( y# a  N1 C$ d5 j
   String tem[] = str.split("[\\t\\s]+");, @& T6 e' n8 X
   xSize = Integer.valueOf(tem[0]).intValue();
; h  `# z0 ^( j( H) r* A5 _0 {   ySize = Integer.valueOf(tem[1]).intValue();
' k" U) [) p9 ]: ?   matrix = new String[xSize][ySize];2 M5 a( |9 ]4 H' q) f2 s4 g
   int i = 0;
- I2 M/ \. Y) x$ d4 K; d$ G+ B9 T   str = "";  U7 q" B1 f' r" l- I' w( f
   String line = in.readLine();8 l0 C& a* h( V# [$ O
   while (line != null) {
( R$ r- L* E2 G2 @    String temp[] = line.split("[\\t\\s]+");7 C$ j" K" A$ X. f' F' f
    line = in.readLine();
/ P3 G9 G0 y9 x9 K- H. \    for (int j = 0; j < ySize; j++) {. F! J* E* C( F- y5 N
     matrix[i][j] = temp[j];- B9 W) G4 W. s5 @% B7 R3 [
    }
8 u8 U3 o" ]- D5 R' ~& p    i++;7 y: L. b2 F: u, e! c4 s
   }2 x! B' d: t9 W6 `
   in.close();
' o0 r+ ?# n/ Y2 R4 C  } catch (IOException ex) {- G; E. v6 J; G: h
   System.out.println("Error Reading file");2 t6 H8 E4 n: R/ ^. K; |) W2 m. A7 j
   ex.printStackTrace();
+ G  o/ s1 D! J! \% k5 P: c7 r# z3 n   System.exit(0);  p' @/ A) x) v, `, d
  }
% L$ N( c- g' r# u7 c }
( f  ~' v  L8 G$ j8 x. I8 W" [ public String[][] getMatrix() {
2 M7 ^+ F* {% T5 L  h3 @: g  return matrix;
) }: w5 v+ {1 P, q8 g- u. z- ^ }: U" Y9 K1 k1 c- }! c/ m2 h
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-29 08:57 , Processed in 0.014371 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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