设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8496|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;1 a0 f" U/ `& X  k
import java.io.BufferedReader;3 Q2 c) [# j8 _6 x- K- E& b8 Z' r% E
import java.io.FileInputStream;% e0 m$ l0 V$ `5 ?/ \
import java.io.FileNotFoundException;6 A# ?7 n& q( n4 J2 b: @2 @
import java.io.IOException;" k/ U! b' E0 I* o! B
import java.io.InputStreamReader;
6 r- l1 y  |3 Q7 d2 M: ^% cimport java.io.UnsupportedEncodingException;' e& ?2 B1 ^* Z; S: M
import java.util.StringTokenizer;# x$ J  k7 G" X2 n: Q, M1 W
public class TXTReader {5 q" c3 w1 \5 Q5 ]- R5 w
protected String matrix[][];
; R* r% q4 ~: l8 u. g protected int xSize;  G% G; k, `/ _( G$ f! m/ s
protected int ySize;
* P. r: S6 G/ Y" ^' \ public TXTReader(String sugarFile) {1 z! ]/ g6 K4 j9 d& D  m' l5 `
  java.io.InputStream stream = null;  p$ y2 O/ a  O* @+ v6 d1 r0 W
  try {3 @% v( {, J( [$ B
   stream = new FileInputStream(sugarFile);
, i( d0 [: W' b# ~; R  } catch (FileNotFoundException e) {
' G* p, R7 x: J7 C# J   e.printStackTrace();6 R7 C) F5 K; {: {
  }
% {' D2 M$ _9 s1 c/ {  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
' z" @) F- E1 V  init(in);
% i; P" Y- Y& T6 @- @  l. w7 k; l3 ? }/ h7 z4 ~! q* s8 V' Y. T( x
private void init(BufferedReader in) {  ~: I0 j2 P) h, H/ r, w( S
  try {
$ C1 ]7 h& W9 `0 X   String str = in.readLine();
" T( a* B) k) l  p) d) u) m   if (!str.equals("b2")) {
# z8 K; C0 W6 w5 y+ l    throw new UnsupportedEncodingException(
0 e4 t3 q% G) ~* v' X2 y      "File is not in TXT ascii format");# n' O* i  Q; c1 M% w# o5 A) `. q
   }
5 Q) n0 B. G8 k/ P% _   str = in.readLine();
( V! q; A: v' F  t4 A   String tem[] = str.split("[\\t\\s]+");  \- l5 k9 _& o- X" Q! ]) F
   xSize = Integer.valueOf(tem[0]).intValue();
4 J# k9 z+ h( N   ySize = Integer.valueOf(tem[1]).intValue();
9 ^' @2 Q3 G7 ?. S2 x$ {   matrix = new String[xSize][ySize];& r1 k  G. |- ^
   int i = 0;' }2 ~" d$ P5 n; a
   str = "";
) {: {. P. U) U4 ]6 |' \   String line = in.readLine();- C9 q3 j2 ]6 y0 c( I2 a
   while (line != null) {4 @! I" B+ O- d- C6 @+ B2 u- T
    String temp[] = line.split("[\\t\\s]+");5 @* A: O4 P! Z4 h
    line = in.readLine();
' ^9 H. O' d. ~8 k    for (int j = 0; j < ySize; j++) {5 b: Y; i0 \9 ]  w3 X8 y  H+ J
     matrix[i][j] = temp[j];
2 B: q7 ^+ S4 Z! m) ~    }" a+ m3 y$ O1 W* C) T8 n) ~7 a
    i++;
2 ^) v! F! f2 `   }
3 W# M5 k8 Q  E9 {9 e4 [( {1 i9 z   in.close();
3 [0 }9 R# _' {, y. D  } catch (IOException ex) {+ G( F5 v9 S3 y% u' @
   System.out.println("Error Reading file");2 g! i6 R4 ^2 c2 G8 {
   ex.printStackTrace();
) N. @# h3 r  J% e1 V9 B   System.exit(0);, k" Q) y1 w: m$ Q$ S
  }
; c6 J0 Y. Q# O9 U }7 T  W7 f* z/ u- t% F
public String[][] getMatrix() {
, }& Y& c8 y/ w7 |* P- a  return matrix;) O4 ?. T- l  ]! D  W
}; K0 m7 [# J! r& G" ]1 T8 H
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-26 01:45 , Processed in 0.020685 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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