设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9996|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;' @7 O2 I* |# l6 B) x$ J* T
import java.io.BufferedReader;
5 A% l, I/ w$ a2 }9 iimport java.io.FileInputStream;2 k) j. E; L1 y# w. F
import java.io.FileNotFoundException;% A7 c9 p+ W* _. K
import java.io.IOException;/ K7 W3 ?% O/ g0 t+ A
import java.io.InputStreamReader;* G/ q1 u$ l, `
import java.io.UnsupportedEncodingException;
6 l- q: t. _2 G, S* r& \! |7 g( ]+ vimport java.util.StringTokenizer;* d6 g3 g& P3 [. j8 B: ^8 d
public class TXTReader {
3 B! t& l  m* K3 s  C protected String matrix[][];
; {6 t: J  r) P' j% q protected int xSize;
7 n4 f7 m$ |1 Y8 w: [ protected int ySize;- z) \$ j: _+ b5 U* |/ u6 o
public TXTReader(String sugarFile) {4 }) t+ x- I. C# T$ T& ]! X
  java.io.InputStream stream = null;( [% ]: n- M$ k9 ]) J  G
  try {
' s& Q9 Q$ T& F   stream = new FileInputStream(sugarFile);7 p5 G' B6 _# K+ A; v1 [' N. C
  } catch (FileNotFoundException e) {
* b' r7 y) x( g7 L8 G9 l& D- A! q   e.printStackTrace();! I/ z' G2 k2 A; m
  }
4 X" @7 s9 D% h. ~! s. s! [  BufferedReader in = new BufferedReader(new InputStreamReader(stream));, C6 j0 `! L& R+ y1 n7 [
  init(in);
0 b6 a$ m7 P: w. E8 V, J/ i }0 ?8 ^  W/ A+ i7 M
private void init(BufferedReader in) {
& m. H, @" W0 p0 o  try {
6 s/ H9 i% y* O( k2 W8 X   String str = in.readLine();
8 }, V8 ~$ s# ]. x   if (!str.equals("b2")) {' v  \3 f' h0 y
    throw new UnsupportedEncodingException(
# u! b8 p- W; W7 Y# J/ }# n+ q      "File is not in TXT ascii format");
% i! N0 K% Y3 P% e: g   }
* d2 `& z. B. ~   str = in.readLine();% y' A1 i# p$ w! N" p( y
   String tem[] = str.split("[\\t\\s]+");" ]* J6 r! ^5 R/ y/ [  G/ v5 C% t
   xSize = Integer.valueOf(tem[0]).intValue();: {8 J8 i* r9 {" Q
   ySize = Integer.valueOf(tem[1]).intValue();
1 Y8 a8 @& r) ?" M0 i$ [& x   matrix = new String[xSize][ySize];
( I* y; s; `: H! A3 J! i, M; ~   int i = 0;! g& `- D- H: k, _3 L
   str = "";  o; S+ h  M1 I$ Z4 T
   String line = in.readLine();/ g+ Q( n$ c0 l
   while (line != null) {/ l  r4 b' @, B6 [
    String temp[] = line.split("[\\t\\s]+");
4 z$ d! H" s. A; u: g! z    line = in.readLine();; I+ @# W6 s7 f5 L" M: }
    for (int j = 0; j < ySize; j++) {
# }9 W( R4 s1 S     matrix[i][j] = temp[j];
8 N: m* }' c2 x& f4 y% f    }
' m0 c. b2 y% L6 R: a    i++;
. U/ E3 j2 b; r   }
1 ?& }- n2 j$ b- b3 G& |& G! w4 [  K   in.close();
. f' _  I! [: ~$ A# x/ b  } catch (IOException ex) {
" u, c# B% ?! T+ I   System.out.println("Error Reading file");
  E. P. E- o1 {$ n   ex.printStackTrace();& R3 |  O$ o! K* r' g" Z
   System.exit(0);7 u# E+ M! d0 O/ m5 P8 N6 g
  }
/ w" ^1 @7 C( E3 ?! ~; b- Y0 g }- P) k5 C& w0 i, i, L! S
public String[][] getMatrix() {
/ F) h3 P6 ~% y( U  d" G  return matrix;; ^9 U# k' a* s+ a
}7 }; C, R& f% I
}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-15 21:30 , Processed in 0.015031 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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