设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7060|回复: 1

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

[复制链接]
发表于 2010-3-2 17:00:18 | 显示全部楼层 |阅读模式
package business;1 a& j  P- ?5 p6 M' n8 q4 M
import java.io.BufferedReader;
; I6 O' S. v! [2 }3 Y  cimport java.io.FileInputStream;' \6 ?, ^1 ~- B" m* p
import java.io.FileNotFoundException;
; k$ B/ u) V/ Q- L+ Aimport java.io.IOException;
1 w+ u  p2 K# e, ?+ j+ z+ Oimport java.io.InputStreamReader;
' W+ x4 ~  P$ j5 I8 J8 }import java.io.UnsupportedEncodingException;
! h' f, e  X% C9 v3 m) f0 t. `0 B1 Wimport java.util.StringTokenizer;' p  J& ^' T  k
public class TXTReader {
! u4 [# X) z% Q; \ protected String matrix[][];/ E$ @: U2 G" e5 d( l
protected int xSize;
" A& P9 N0 b! c protected int ySize;5 H& o; e2 A6 l$ ]* _, Y$ `
public TXTReader(String sugarFile) {3 N. M: M% [9 Q! g  p
  java.io.InputStream stream = null;/ X3 `" I6 D( }' b1 ]
  try {
" G; j, L3 e8 C9 u( H( {9 w   stream = new FileInputStream(sugarFile);
( B0 V3 z' k2 t" I1 G  } catch (FileNotFoundException e) {
  w$ Q1 r' C5 P% t! x. C5 N   e.printStackTrace();2 m8 U+ [" J. q- A0 r% {) B
  }
/ u2 }0 @7 W+ T# C9 r5 M  BufferedReader in = new BufferedReader(new InputStreamReader(stream));
* ^, W. r' H# ]' M2 U  init(in);5 P. x1 S' r1 H. P: h1 d( J& h
}
9 A6 i& Y! ]0 {6 V) T8 @: b private void init(BufferedReader in) {) R) b" a! F0 P3 k# L7 f) d6 d
  try {, }8 k% ]# B- J9 h
   String str = in.readLine();
8 s4 j" Y; x0 j3 J   if (!str.equals("b2")) {
& ~9 X$ j( }/ R; L    throw new UnsupportedEncodingException(; t# f6 J- ]3 l" {
      "File is not in TXT ascii format");
  R, S$ i3 x& b- J0 f   }2 o! s( A, G. P3 ~2 d( o3 E8 x
   str = in.readLine();
  o' U6 y# O; a# ]/ A2 n   String tem[] = str.split("[\\t\\s]+");, R! _3 z& J( _7 H
   xSize = Integer.valueOf(tem[0]).intValue();
6 z4 b" e  F$ z6 q   ySize = Integer.valueOf(tem[1]).intValue();
6 S% w4 B7 d* _, \   matrix = new String[xSize][ySize];
& f, d; C( C* h, X   int i = 0;
- E  l' @' T! }( I/ w* g   str = "";4 p  E+ \/ L7 K! Y1 e/ k7 m
   String line = in.readLine();- {4 f# O) F2 z
   while (line != null) {9 y$ X/ n" U& b: f
    String temp[] = line.split("[\\t\\s]+");
3 Z1 R) P& s8 T& j6 n    line = in.readLine();
1 G2 j) X  T: G    for (int j = 0; j < ySize; j++) {
, k' x6 ]' `2 z* S2 h( M/ o2 e     matrix[i][j] = temp[j];" ?5 y! B* W- G' ^7 P! i8 w8 x
    }
/ B5 [$ \8 m% d) G6 x) Y, j8 E, b    i++;+ _. i$ i5 ~, k
   }- n! @5 K7 n4 c* V/ r2 n0 r" Z
   in.close();' L! K. O3 l8 `
  } catch (IOException ex) {
1 l- ~5 P, H9 Z7 o# o   System.out.println("Error Reading file");
6 z- c9 C2 s! U2 a2 d   ex.printStackTrace();+ _) k$ u$ f9 l& r* Q
   System.exit(0);$ ]3 W, I. m$ r  B1 e7 ]
  }/ b. v+ s$ O* c- ^% }( e) o
}, s/ x6 t: c6 I
public String[][] getMatrix() {/ H/ l6 ]* S: i+ |2 R
  return matrix;
$ S4 A- a9 |+ m }
" @. l: q- L" w6 R- O}
发表于 2010-8-31 08:40:14 | 显示全部楼层
请问楼主,有没有能把repast中的一个变量写入到一个txt文档的程序呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-18 08:20 , Processed in 0.018403 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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