设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8667|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;
9 S& U" x& y9 y' f4 G& Q- ximport java.io.BufferedReader;
& [9 l/ P3 ~& a3 b# Nimport java.io.FileInputStream;
/ C$ \5 G6 L8 M1 t7 k$ n! {import java.io.FileNotFoundException;
/ `" a9 }6 G  o" h8 aimport java.io.IOException;
: ]# X7 m/ t  n9 l2 n8 `" Pimport java.io.InputStreamReader;& @' `4 n5 {0 O- V5 H/ v: W
import java.io.UnsupportedEncodingException;
$ k  ]4 W, D9 f+ I2 ]! |import java.util.StringTokenizer;
0 p" H- `0 w) Kpublic class TXTReader {2 Y4 H) x1 o; @
protected String matrix[][];
8 l& b8 R5 i. n protected int xSize;
. T  y% ]- V0 T1 I, y protected int ySize;3 Q  |( V: w# N8 @# f
public TXTReader(String sugarFile) {
: Y( x! x9 s) |: n) e1 Z  C/ G  java.io.InputStream stream = null;
/ b& |  x8 |& P: M0 K7 W6 d( K  try {
; j. S9 V5 o3 Q   stream = new FileInputStream(sugarFile);
& ]* g' D/ S2 V! k9 W1 l  } catch (FileNotFoundException e) {
) j* K( H. m) c0 v: {% ~4 I- N! E   e.printStackTrace();0 g; Z! k" ~6 B& U) |
  }$ y5 z5 n- K0 f5 w6 E# O
  BufferedReader in = new BufferedReader(new InputStreamReader(stream));2 E2 e: X# `. r- }- z1 @* d
  init(in);) A" f/ {$ v; x/ F9 s4 W
}( |9 K3 |: m# A# g% ?4 Q
private void init(BufferedReader in) {9 V3 S7 A; H2 ?
  try {
* Y- E9 ]7 u) t; R   String str = in.readLine();
4 O6 k* I  q5 B5 k0 |3 j   if (!str.equals("b2")) {
6 l. V5 X6 \: I' Q- ]+ X    throw new UnsupportedEncodingException(6 o0 g7 p# j+ Q+ d8 h8 l; j! h
      "File is not in TXT ascii format");! g$ p7 A  l& I; F/ i
   }
% q( q' f: s* l+ j: Q: W   str = in.readLine();
9 F! u0 j) `, J8 B1 s1 P' H   String tem[] = str.split("[\\t\\s]+");- J' B5 |9 _4 [1 r) d
   xSize = Integer.valueOf(tem[0]).intValue();
7 [% }+ c/ [' o2 u& U   ySize = Integer.valueOf(tem[1]).intValue();5 U8 I2 _# `" T# q2 |7 m9 g) K! {
   matrix = new String[xSize][ySize];
8 j9 ?( J! e, ?5 h5 W) u   int i = 0;8 c+ }6 Z- R  H9 m; s/ g
   str = "";. P9 g% ~9 Q' c! Y+ K
   String line = in.readLine();  C  x  B/ c' n6 x5 ~( y$ X. H" h
   while (line != null) {1 f. b% a# j: L% D. I
    String temp[] = line.split("[\\t\\s]+");& H$ [' _4 D- @# `
    line = in.readLine();
8 w5 \' L  G% O5 y' F5 u& e    for (int j = 0; j < ySize; j++) {
6 q$ J' u) i( Z9 l     matrix[i][j] = temp[j];2 M/ s' o7 s% M' F) @
    }
# n5 d8 a+ b) A; g' j' {1 H    i++;% X8 d* ?4 i8 G! g( i
   }
8 Z5 g) w' m9 a8 Z) u   in.close();
9 m: F$ s9 c6 k& [  } catch (IOException ex) {% Q7 V+ k# G8 f
   System.out.println("Error Reading file");
0 X% v. g/ ]% A! G   ex.printStackTrace();! ]7 D, @6 A3 J7 M( A
   System.exit(0);
/ U0 q' w4 Z) I  n& A; e7 C  }; J3 H& q4 s5 L5 X0 z$ C+ `+ J8 T
}
. b" m$ E: k& S5 `% I public String[][] getMatrix() {: Z$ i6 `. w+ ]  P7 K
  return matrix;- A: m- E4 E1 U7 M% s$ {
}
* w- g% j8 B/ z- y  N) j" L7 `}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-6 00:51 , Processed in 0.016894 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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