设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8124|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
! m, v& \' g* L; A2 Dimport java.io.BufferedReader;; n( g; n; {6 E; _
import java.io.FileInputStream;
1 _" v! L2 M( T5 aimport java.io.FileNotFoundException;: y8 v2 A6 a  a/ P
import java.io.IOException;
& j8 w$ H0 G% d7 n0 Cimport java.io.InputStreamReader;
" _; o, d4 ~6 qimport java.io.UnsupportedEncodingException;
; ~3 e6 A$ \+ N+ i5 ~) Gimport java.util.StringTokenizer;
' S8 S# @* H8 z6 Npublic class TXTReader {
' Z" t- u" [+ O9 g protected String matrix[][];
" K( l1 y' i1 E; w protected int xSize;
& u# E0 I: D4 y/ ~# | protected int ySize;4 U" y: }7 K, w, C" N6 a
public TXTReader(String sugarFile) {: f$ u% L, \4 F) S" O
  java.io.InputStream stream = null;
6 L+ Z  P) L* @4 c  try {
/ g% ~" |: H' f. D& n   stream = new FileInputStream(sugarFile);( o+ i& Y9 b* G6 p0 }4 R/ H' {$ e
  } catch (FileNotFoundException e) {
4 I8 F: {) y5 l  K# E; l) v   e.printStackTrace();
/ t8 |: W, u- R- h  }
" b* d+ s' [8 L$ ?+ }  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
$ z! a$ I2 K, q" w  init(in);/ g: r% o$ ?/ g/ N" B
}6 y' E$ J* ^) F1 ~9 u
private void init(BufferedReader in) {* u, l! K. z, V# R$ j
  try {, d* F6 @8 N! Y8 j+ F
   String str = in.readLine();
+ m, g' d0 H# u7 m7 L+ k   if (!str.equals("b2")) {) X, ^6 X2 l- i2 f% n. S
    throw new UnsupportedEncodingException(5 |' F5 {  n! B. t' q1 t
      "File is not in TXT ascii format");+ ]$ N- h& E0 P$ X7 j
   }
% s) j4 |* G4 c/ ]) W( [   str = in.readLine();
: q, {1 V# l6 m; Q. B   String tem[] = str.split("[\\t\\s]+");
& ?9 J  K+ z" h% L   xSize = Integer.valueOf(tem[0]).intValue();  o& I9 N* x6 }$ Y- C
   ySize = Integer.valueOf(tem[1]).intValue();
( W' t; t9 V7 G! |, N& u   matrix = new String[xSize][ySize];
, W( ^3 {6 X, W. a2 V) |   int i = 0;
1 f- i  L  _+ S5 H& B- a* Y   str = "";8 h4 ]+ x3 b* k7 K
   String line = in.readLine();# o6 |( o. R/ A6 f
   while (line != null) {
. H7 O* U9 S7 M    String temp[] = line.split("[\\t\\s]+");5 g* x( p% Z' X
    line = in.readLine();
- j, s" Q( y" v# Y8 b- D! `6 P    for (int j = 0; j < ySize; j++) {
4 Q/ p4 O# H: \: L  {, L" J     matrix[i][j] = temp[j];
; o0 P& g/ m* v" J- |! }1 y    }; Y/ m8 y' e# r3 I! u; i$ g0 ^
    i++;# M% c9 m! y3 q( T4 ~4 d
   }
6 `+ k" i/ G) A* M0 v& L   in.close();
9 Z' X7 C6 W, g) G  } catch (IOException ex) {
( y1 k4 z2 X& ?$ d" e   System.out.println("Error Reading file");
6 A. i( ^" W- S% G% N) g   ex.printStackTrace();
2 l6 r- N: M/ U' a1 h0 M. E1 d" _$ q   System.exit(0);
5 \3 F* D1 p, v  b1 y- G  }7 U0 ^! s5 _5 P6 `: h
}
! p  x7 A$ J+ a9 N% _* H public String[][] getMatrix() {% ^/ n8 C! U- L/ N+ q) F+ r
  return matrix;" g$ _. F. O7 x. U, T
}
9 b2 p/ g; N( U( b}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-3 11:11 , Processed in 0.015283 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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