设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8833|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
. r/ I9 D9 N! F/ n' K+ l# G# ximport java.io.BufferedReader;
# m4 p9 C) a) e: k! M; A# gimport java.io.FileInputStream;
* j6 e9 G$ ~  fimport java.io.FileNotFoundException;
. i) `: Q7 f5 I( ]2 Timport java.io.IOException;
/ |- k6 [+ M) t5 T/ E' Fimport java.io.InputStreamReader;( k, n) k1 I6 t2 S8 D
import java.io.UnsupportedEncodingException;% q; G8 O4 A. `) P8 A1 |3 {
import java.util.StringTokenizer;
- j) J" S: k+ d% l9 k; q7 apublic class TXTReader {
  y+ J/ L7 k% p protected String matrix[][];/ {: U- g$ u% q$ ^4 f2 @: D  x
protected int xSize;! f# X7 A" a( V: b3 |! P
protected int ySize;
- s9 G0 S1 ]& c& k/ e) X public TXTReader(String sugarFile) {
: C# e3 A" [$ O4 V$ m) I  java.io.InputStream stream = null;
5 ~2 N/ {8 d* B, p  try {5 R+ H/ h5 b: f: n4 N& j4 d3 B
   stream = new FileInputStream(sugarFile);! K* W/ W4 P  S/ j4 q& M! w
  } catch (FileNotFoundException e) {
9 f0 C8 g$ H- I8 {5 `1 k( W   e.printStackTrace();, B* q4 K6 N5 q! q9 w+ Q
  }
4 ?) ?3 c5 @* y$ \2 a  BufferedReader in = new BufferedReader(new InputStreamReader(stream));7 `: B% R! y; l, j/ k
  init(in);/ j1 M! M7 ]" k7 ]5 S
}
" c$ }7 G8 X7 D& d private void init(BufferedReader in) {8 ~! b  o6 V' L, C* V! r
  try {
. D" t. i+ l0 Z1 P  t( \   String str = in.readLine();" E/ L" Y; [4 x3 k% f! y
   if (!str.equals("b2")) {
4 K. w0 O3 ]0 G4 U  s1 v+ L    throw new UnsupportedEncodingException(
. Y  o* o& M/ T* ^* b1 e      "File is not in TXT ascii format");! M3 U9 r* F: o" T; t
   }) F+ H! t5 R* W* q
   str = in.readLine();! f  ~: v. V* `8 B$ H# L/ R
   String tem[] = str.split("[\\t\\s]+");3 _9 @# F  i8 H
   xSize = Integer.valueOf(tem[0]).intValue();
- B1 I: m* Z% P7 w4 k$ c   ySize = Integer.valueOf(tem[1]).intValue();9 K' J& U/ _3 P; Z
   matrix = new String[xSize][ySize];
( ~. s) A3 J4 p3 C7 \& l/ T& V   int i = 0;; f; l" C$ |( }5 V- h' b
   str = "";
9 T8 H2 x6 ?; P4 L; I; {   String line = in.readLine();" t; _% V2 i- S& r
   while (line != null) {
8 m( X* u5 ]! a% x- }! [    String temp[] = line.split("[\\t\\s]+");4 I( B: o. m% S
    line = in.readLine();
; L  s* }( n! i1 w2 n% j$ s    for (int j = 0; j < ySize; j++) {
8 S0 C* e4 Q$ C% O; r! K     matrix[i][j] = temp[j];
8 E) Y9 U% x: x6 }    }9 ]4 B5 x) i4 c* A+ o% r
    i++;2 k: U9 y' ]: R; a
   }
3 f! Q- K! {1 ~  b+ |7 t   in.close();
7 I+ Q- J$ Y- o$ r  M5 m, m2 a  } catch (IOException ex) {+ K2 z, k& Z& `' d
   System.out.println("Error Reading file");
4 k! |9 F/ r8 w4 w   ex.printStackTrace();
; i( q9 ~4 o0 e* h   System.exit(0);
: ^) D  H. e0 v1 I2 Q  }" K2 i) Y7 W) X5 w+ K
}! E+ r) @+ n5 x
public String[][] getMatrix() {6 H/ S1 Q% g0 |* W
  return matrix;, K& Z' A; @4 b" I  g
}
  ^9 \5 B+ J* I9 O# z4 H" Q}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-16 10:03 , Processed in 0.012888 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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