设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8011|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;9 S. L7 A. k. t1 u# l0 Z! j( Q3 K6 U, k
import java.io.BufferedReader;
; W6 z9 ~; P( v0 N$ A  L$ l: Zimport java.io.FileInputStream;. T; B  N& J9 b0 x% X
import java.io.FileNotFoundException;9 n/ J( T2 q4 m7 U. g
import java.io.IOException;
; k: e# w) Z' |1 oimport java.io.InputStreamReader;# s9 [& I' b3 U' v& E: z
import java.io.UnsupportedEncodingException;
. j; F4 S8 T, t' e$ j8 H* Wimport java.util.StringTokenizer;2 u0 x( J+ r7 Z( w+ f
public class TXTReader {# q/ D2 e/ `4 m7 |
protected String matrix[][];
) e6 f# b$ K8 i8 d& g$ B9 } protected int xSize;
- H/ @, I: o4 g  X protected int ySize;
# \% P5 a9 _; {" _3 r/ ?: Y9 Z) n3 { public TXTReader(String sugarFile) {, B% }6 R( g! c" {
  java.io.InputStream stream = null;1 m# y' s/ S/ i  j. \! M
  try {8 @" `2 L# E3 c. f8 K% I  ]
   stream = new FileInputStream(sugarFile);
! J6 |1 i5 G8 s: N2 V2 n  } catch (FileNotFoundException e) {1 h3 q  |8 K8 o+ d
   e.printStackTrace();' |8 g7 S. ^$ T8 \) Z
  }
7 [. u, g7 N& _5 I, V0 c$ i  BufferedReader in = new BufferedReader(new InputStreamReader(stream));  j! }4 X4 Q% m) E  N2 M& h% l
  init(in);
" w  b7 X3 \9 s3 L1 W+ y }
9 v5 x  t; }! e private void init(BufferedReader in) {: O6 q2 Y. W( L' N
  try {
/ H) @$ _& h# H: J6 E' r6 }* a) [   String str = in.readLine();
( N2 E7 w. J" _   if (!str.equals("b2")) {& r3 l6 k/ ^5 D$ G
    throw new UnsupportedEncodingException(
1 W5 w+ j" N& `3 L* _7 |- p      "File is not in TXT ascii format");+ h# h$ D- y' g4 B7 N
   }
% o1 l* n8 w8 s% E( j1 D   str = in.readLine();- p. N* c% \; N4 O! \0 {
   String tem[] = str.split("[\\t\\s]+");0 ?1 q& H1 R5 u
   xSize = Integer.valueOf(tem[0]).intValue();
/ s7 t/ R2 I& g" C8 ^0 w) r2 h   ySize = Integer.valueOf(tem[1]).intValue();( b) H4 x* U3 L# b& @
   matrix = new String[xSize][ySize];3 b3 `' V- L/ U! }& E% ]3 r$ I
   int i = 0;
' M/ b+ `- c4 Q: U, a   str = "";
7 v8 X1 g% m. r7 D1 z9 ~   String line = in.readLine();
0 w1 y' u: A, a& A1 W; q   while (line != null) {/ d5 Y* r) J# I
    String temp[] = line.split("[\\t\\s]+");
9 R! D; B& b. H1 T9 y3 P    line = in.readLine();. P; _2 v3 U" M! v8 G& r
    for (int j = 0; j < ySize; j++) {, O+ P) _! Z. S+ {+ J
     matrix[i][j] = temp[j];8 {4 v4 X- b* _' z6 ?: m
    }4 Y5 [+ T- N; J5 p
    i++;
2 o$ F8 G* ]9 Z   }
- f% N& Z$ i; s2 J. a& ?   in.close();
8 A' H% @# w2 N  } catch (IOException ex) {3 e- D* }! C3 f& ~% g8 M" I: o2 \
   System.out.println("Error Reading file");; I% w+ R/ @/ T6 L! f
   ex.printStackTrace();
0 ~- @: X4 D0 B2 G$ Z   System.exit(0);
! e  k+ z0 H. ?- z  K+ _+ ^  }6 c* Q# U3 n. |$ c
}
' B/ v/ R& ~8 e* w: s public String[][] getMatrix() {
! }( n* b! w& `: h3 [. ^  return matrix;; X4 A8 r- ]9 o& s
}& Z- c4 x+ W! h; f7 \
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-4-27 20:38 , Processed in 0.016654 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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