设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7446|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
, i# o7 W1 ?% Kimport java.io.BufferedReader;
0 Z- g3 [1 f, L- A8 |4 F1 t: Timport java.io.FileInputStream;
5 Z7 Z; r1 Y* b/ ~import java.io.FileNotFoundException;' B4 `% T3 M# \& T  j
import java.io.IOException;
* Z% i8 u3 J; ?( s8 Q$ Wimport java.io.InputStreamReader;- E# x" Q# C- y: ]( s" L0 c. S
import java.io.UnsupportedEncodingException;1 L. _' v, `: M. N9 |5 t4 [2 l: |2 p
import java.util.StringTokenizer;
7 H$ g. y2 T0 spublic class TXTReader {
& J% c2 C3 y/ ` protected String matrix[][];
5 c& e& V' X0 N( a. m- v protected int xSize;
4 d1 u, b" C- d protected int ySize;3 O2 M7 Z) w" _6 Q9 `0 e
public TXTReader(String sugarFile) {, G# r3 K8 S* N9 V
  java.io.InputStream stream = null;
9 j4 R. o+ j2 Q. Z8 F4 `# d( r  try {/ Q# U' W$ `( J- H3 I
   stream = new FileInputStream(sugarFile);$ `+ }8 R: Y2 Z3 Y% Q; Y& S
  } catch (FileNotFoundException e) {5 ~  z4 Y/ W7 B3 c' b) F0 x
   e.printStackTrace();0 I4 V5 q$ M9 v8 |
  }  i- Q: A$ Y# \. D; U
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));" ~: K2 I$ k6 c' y0 A, A, i
  init(in);1 G$ m, @( k6 p+ m" {
}
! V: H- v' n' I: u' a4 B9 |: W* h private void init(BufferedReader in) {
! D. q; N$ }6 ~3 O: Y8 f, r  try {
2 ?! [: F% F# w6 f- `4 {2 a   String str = in.readLine();
* |6 I- W% h1 d: v8 y   if (!str.equals("b2")) {# B7 }* v' g$ E9 m, P
    throw new UnsupportedEncodingException(
+ u4 D/ M& ]- y2 W! z$ @      "File is not in TXT ascii format");7 M( v- ^, g+ J0 b
   }, {  f* C5 ]: @9 h7 k
   str = in.readLine();
6 C, N, p# v8 ^  R2 H' J1 q# ^   String tem[] = str.split("[\\t\\s]+");
" _/ I7 L, a( {# w6 p   xSize = Integer.valueOf(tem[0]).intValue();4 n& X7 h5 B; F2 m
   ySize = Integer.valueOf(tem[1]).intValue();
+ m0 s2 I0 [- j5 D, Y3 [. S$ n   matrix = new String[xSize][ySize];, H* x) [4 T0 L4 @1 k1 @$ ]1 ?  o
   int i = 0;
+ e) E! l4 J$ B6 c   str = "";" D3 v+ |! f2 O9 B' V5 K8 s
   String line = in.readLine();0 H) O" O  z8 A3 y5 p" x
   while (line != null) {
5 t3 P: B8 C8 N# ~$ |0 Y    String temp[] = line.split("[\\t\\s]+");8 r8 t0 S  w9 E  N9 @. g  y
    line = in.readLine();
4 H, d$ |4 @* O    for (int j = 0; j < ySize; j++) {
7 h, o5 b+ y8 w7 m     matrix[i][j] = temp[j];/ c. A1 w! j/ f" p5 h$ c! F5 b
    }
# R# _: X$ a/ R    i++;1 J/ Y" V, H: S1 m# ~! H3 K- z
   }: D  a; r& K0 |9 I
   in.close();) x0 k3 C7 g! k: W* s
  } catch (IOException ex) {
  s* t; c8 ^$ g5 g   System.out.println("Error Reading file");  |' b6 Y9 R" I2 n
   ex.printStackTrace();
4 H( c7 b$ Z5 L1 O3 r/ S   System.exit(0);- N! H7 ]. F5 k0 {& g: k9 j
  }& Z. R" Z( `. l  ^4 j8 @2 g
}
! u, T& ?0 [9 J6 I) Y- `7 a' h public String[][] getMatrix() {
* R$ l& C/ B& s0 o* j* S  return matrix;' d$ `! a0 Q$ r- U2 m
}
" s* V% M. v( O& o( f}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-16 06:06 , Processed in 0.018540 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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